Formal Definitions of Programming Languages: K Semantics Dorel Lucanu Faculty of Computer Science Alexandru Ioan Cuza University, Iaşi, Romania [email protected] FMSE 2014 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 1 / 24 Outline D. Lucanu (FII - UAIC) K Semantics FMSE 2014 2 / 24 Outline K Project Started in 2003 by Grigore Rosu at UIUC, motivated mainly by teaching programming languages and noticing that the existing semantic frameworks have limitations Project thesis: Rewriting gives an appropriate environment to formally define the semantics of real-life programming languages and to test and analyze programs written in those languages. Joint work between Formal Systems Laboratory (FSL) from University of Illinois at Urbana-Champaign (UIUC) and Formal Methods in Software Engineering (FMSE) group from Al. I. Cuza University (UAIC) web page: http://kframework.org/ D. Lucanu (FII - UAIC) K Semantics FMSE 2014 3 / 24 Outline K Ingredients Computations Sequences of tasks, including syntax Capture the sequential fragment of programming languages Syntax annotations specify order of evaluation Configurations Multisets (bags) of nested cells High potential for concurrency and modularity K rules Specify only what needed, precisely identify what changes More concise, modular, and concurrent than regular rewrite rule D. Lucanu (FII - UAIC) K Semantics FMSE 2014 4 / 24 Outline Computations1/2 syntax Exp ::= Int | Bool | Id | Exp "+" Exp | Id "=" Exp syntax Stmt ::= Exp ";" syntax Stmts ::= Stmt > Stmts Stmts [left, strict] [strict(2)] [strict(2)] [left, seqstrict] Generated heating rules E1 + E2 ⇒ E1 y + E2 E1 + E2 ⇒ E2 y E1 + X = E ⇒ E y X = E ; ⇒ E y ; S1 S2 ⇒ S1 y S2 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 5 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 y = x + 2; x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 y = x + 2; x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 y = x + 2; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 y = x + 2; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 y = x + 2; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 x + 2 y y = ; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 x + 2 y y = ; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 x + 2 y y = ; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 x y + 2 y y = ; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline Computations 2/2 E1 E1 X S1 + E2 ⇒ E1 y + E2 + E2 ⇒ E2 y E1 + = E ; ⇒ E y X = ; S2 ⇒ S1 y S2 x y + 2 y y = ; y x = x + y; D. Lucanu (FII - UAIC) K Semantics FMSE 2014 6 / 24 Outline K Configuration Specification, Graphically configuration: T k $PGM:K env .Map store .Map ASCII: configuration <T color="yellow"> <k color="green"> $PGM:K </k> <env color="blue"> .Map </env> <store color="red"> .Map </store> </T> D. Lucanu (FII - UAIC) K Semantics FMSE 2014 7 / 24 Outline A Cocrete K Configuration T k x y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 ASCII: <T> <k> x ∼> HOLE + 2 ∼> y = HOLE; ∼> x = x + y; </k> <env> a |-> 1 x |-> 2 y |-> 3 z |-> 4 </env> <store> 1 |-> 5 2 |-> 23 3 |-> 16 4 |-> -12 </store> </T> D. Lucanu (FII - UAIC) K Semantics FMSE 2014 8 / 24 Outline K Rules T k x y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 ⇒ T k 23 y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 9 / 24 Outline Abstraction 1/3 T k x y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 ⇒ T k 23 y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 10 / 24 Outline Abstraction 2/3 T k x 23 env x 7→ 2 store 2 7→ 23 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 11 / 24 Outline Abstraction 3/3 rule k env X :Id X 7→ L store L 7→ I I D. Lucanu (FII - UAIC) K Semantics FMSE 2014 12 / 24 Outline Concretization ASCII: rule I1 + I2 => I1 +Int I2 Graphically: rule I1 + I2 I1 +Int I2 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 13 / 24 Outline Concretization k I1 + I2 I1 +Int I2 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 13 / 24 Outline Concretization T k I1 + I2 T I1 +Int I2 k — env env — store — — store — D. Lucanu (FII - UAIC) K Semantics FMSE 2014 13 / 24 Outline Concretization T k I1 + I2 y κ env — store — ⇒ T k I1 +Int I2 y κ env — rule <T> <k> I1:Int + I2:Int ∼> kappa:K </k> <env> sigma:Map </env> <store> rho:Map </store> </T> => <T> <k> I1 +Int I2 ∼> kappa </k> <env> sigma </env> <store> rho </store> </T> store — D. Lucanu (FII - UAIC) K Semantics FMSE 2014 13 / 24 Outline Rules at Work rule k state X 7→ I X :Id I T k x y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline Rules at Work rule k state X 7→ I X :Id I T k 23 y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline Rules at Work rule I y + E2 I + E2 T k 23 y + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline Rules at Work rule I y + E2 I + E2 T k 23 + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline Rules at Work rule I1 + I2 I1 +Int I2 T k 23 + 2 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline Rules at Work rule I1 + I2 I1 +Int I2 T k 25 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline Rules at Work rule I yX =; X =I ; T k 25 y y = ; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline Rules at Work rule I yX =; X =I ; T k y = 25; y x = x + y; env a 7→ 1 x 7→ 2 y 7→ 3 z 7→ 4 store 1 7→ 5 2 7→ 23 3 7→ 16 4 7→ −12 D. Lucanu (FII - UAIC) K Semantics FMSE 2014 14 / 24 Outline K Syntax of CinK: Expressions syntax Exp ::= Int | Bool | Id | "(" Exp ")" | Exp "/" Exp | Exp "*" Exp | Exp "%" Exp > left: Exp "+" Exp | Exp "-" Exp > left: Exp "==" Exp | Exp "<=" Exp | Exp "<" Exp > "!" Exp > Exp "&&" Exp > Id "=" Exp D. Lucanu (FII - UAIC) K Semantics [bracket] [left, strict] [left, strict] [left, strict] [left, strict] [left, strict] [left, strict] [left, strict] [left, strict] [strict] [left, strict(1)] [strict(2)] FMSE 2014 15 / 24 Outline K Syntax of CinK: Statements syntax Stmt ::= Decl | Exp ";" [strict] | "{" "}" | "{" Stmts "}" | "while" "(" Exp ")" Stmt | "if" "(" Exp ")" Stmt "else" Stmt [strict(1)] > "if" "(" Exp ")" Stmt syntax Stmts ::= Stmt | Stmts Stmts [left] D. Lucanu (FII - UAIC) K Semantics FMSE 2014 16 / 24 Outline K Semantics of CinK: Declarations syntax Type ::= "int" syntax Decl ::= Type Ids ";" syntax Ids ::= List{Id,","} syntax Pgm ::= Stmts D. Lucanu (FII - UAIC) K Semantics FMSE 2014 17 / 24 Outline K Semantics of CinK: Configuration configuration <T color="yellow"> <k color="green"> $PGM:K </k> <env color="blue"> .Map </env> <store color="red"> .Map </store> </T> D. Lucanu (FII - UAIC) K Semantics FMSE 2014 18 / 24 Outline K Semantics of CinK: Expressions 1/2 syntax KResult ::= Int | Bool rule <k> X:Id => I ...</k> <env>... X |-> L ...</env> <store>... L |-> I ...</store> rule rule rule rule rule I1 I1 I1 I1 I1 / * % + - I2 I2 I2 I2 I2 => => => => => D. Lucanu (FII - UAIC) I1 I1 I1 I1 I1 /Int *Int %Int +Int -Int I2 I2 I2 I2 I2 requires I2 =/=Int 0 K Semantics FMSE 2014 19 / 24 Outline K Semantics of CinK: Expressions 2/2 rule rule rule rule rule rule I1 == I2 => I1 ==Int I2 I1 <= I2 => I1 <=Int I2 I1 < I2 => I1 <Int I2 ! B => notBool B true && B => B false && _ => false D. Lucanu (FII - UAIC) K Semantics FMSE 2014 20 / 24 Outline K Semantics of CinK: Statements rule {} => . [structural] rule { Ss } => Ss [structural] rule I; => .K rule <k> X = I:Int => I ...</k> <env>... X |-> L ...</env> <store>... L |-> (_ => I) ...</store> rule rule rule rule rule S1:Stmts S2:Stmts => S1 ~> S2 [structural] if (E) S => if (E) S else {} [structural] if (true) S else _ => S if (false) _ else S => S while (B) S => if (B) {S while (B) S} else {} D. Lucanu (FII - UAIC) K Semantics [structur FMSE 2014 21 / 24 Outline K Semantics of CinK: rule <k> int (X,Xs => Xs); ...</k> <env> Rho:Map (.Map => X|-> !L:Int) </env> <store>... .Map => !L:Int |-> 0 </store> requires notBool (X in keys(Rho)) rule int .Ids; => .K D. Lucanu (FII - UAIC) [structural] K Semantics FMSE 2014 22 / 24 Outline K Semantics vs Big-Step SOS on the blackboard D. Lucanu (FII - UAIC) K Semantics FMSE 2014 23 / 24 Outline K Semantics vs Small-Step SOS on the blackboard D. Lucanu (FII - UAIC) K Semantics FMSE 2014 24 / 24
© Copyright 2026 Paperzz