FM FBK
About Contact Download Documentation Publications Applications Links

The K2 Language

Kratos2 works on an intermediate verifiation language called K2, with a semantic based on first-order logic and Satisfiability Modulo Theories.

K2 uses S-expressions for its concrete syntax.

Types

Expressions

Statements

      (seq
        (jump (label then) (label else))
        (label then)
        (assume COND)
        (jump TARGET)
        (label else)
        (assume (not COND)))

where (label then) and (label else) are fresh.

Functions

    (function NAME (PARAM_1 ... PARAM_N) (return RET_1 ... RET_N)
       (locals VAR_1 ... VAR_N)
     BODY)

where:

Programs

A program is a list of function definitions, plus an optional list of global variables, an entry point defining the main function, an optional initial constraint, and an optional list of type definitions. The syntax is the following:

    (type TYPENAME_1 TYPE_1)
    ...
    (type TYPENAME_N TYPE_N)

    (entry NAME)
    (init CONSTR)
    (globals VAR_1 ... VAR_N)

    FUNCTION_1
    ...
    FUNCTION_N

where:

Annotations and Properties

Expressions, statements and function definitions can be annotated with metadata. The syntax is the following:

  (! SEXP :KEY VALUE)

where:

Properties can be defined by annotating label statements as follows:

Example

Here is a simple C program and its equivalent formulation in K2:

C version

     var glbl;

     int f(int x)
     {
         if (glbl > 0) {
             return x - 1;
         } else {
             glbl = 0;
             return x;
         }
     }

     void main(void)
     {
         int y;
         y = f(23);
     }

K2 version

     (type cint (sbv 32))
     (entry main)
     (globals (var glbl cint))

     (function f ((var x cint)) (return (var ret cint))
       (locals)
       (seq
         (jump (label then) (label else))
         (label then)
         (assume (op gt glbl (const 0 cint)))
         (assign ret (op sub x (const 1 cint)))
         (jump (label end))
         (label else)
         (assume (op not (op gt glbl (const 0 cint))))
         (assign glbl (const 0 cint))
         (assign ret x)
         (label end)))

     (function main () (return)
        (locals (var y cint))
        (call f (const 23 cint) y))