Skip to content

Commit 95665c8

Browse files
authored
Simplify K definitions (#38)
* Remove `calls.k`: can be integrated later with a corresponding target * Replace `variables.k` by `calc.k`: defines sort `Stmt`, no need to redefine variable rules that way * Rename files an modules: shorter names * Simplify `EXPR-SYNTAX`: reduce the number of concepts to be explained during a demo * Add target `imp-semantics.expr`
1 parent 8a21f32 commit 95665c8

File tree

11 files changed

+162
-275
lines changed

11 files changed

+162
-275
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ kdist: kdist-build
5252
KDIST_ARGS :=
5353

5454
kdist-build: poetry-install have-k
55-
$(POETRY_RUN) kdist --verbose build -j3 $(KDIST_ARGS)
55+
$(POETRY_RUN) kdist --verbose build -j4 $(KDIST_ARGS)
5656

5757
kdist-clean: poetry-install
5858
$(POETRY_RUN) kdist clean

src/kimp/kdist/imp-semantics/calc.k

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
requires "expr.k"
2+
3+
4+
module CALC-SYNTAX
5+
imports EXPR-SYNTAX
6+
imports ID-SYNTAX
7+
8+
syntax Expr ::= Id
9+
10+
syntax Stmt ::= Id "=" Expr ";" [group(stmt), strict(2), format(%1 %2 %3%4)]
11+
syntax Stmt ::= right:
12+
Stmt Stmt [group(seq-stmt), format(%1%n%2)]
13+
14+
syntax priority stmt > seq-stmt
15+
endmodule
16+
17+
18+
module CALC
19+
imports CALC-SYNTAX
20+
imports EXPR-RULES
21+
22+
configuration
23+
<k color="green"> $PGM:Stmt </k>
24+
<env color="yellow"> $ENV:Map </env>
25+
26+
rule [step]: <k> S1:Stmt S2:Stmt => S1 ~> S2 ... </k>
27+
28+
rule [var]:
29+
<k> X:Id => V ... </k>
30+
<env> X |-> V ... </env>
31+
32+
rule [assign]:
33+
<k> X = V:Value ; => .K ... </k>
34+
<env> E => E [ X <- V ] </env>
35+
endmodule

src/kimp/kdist/imp-semantics/calls.k

-112
This file was deleted.

src/kimp/kdist/imp-semantics/expr.k

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module EXPR-SYNTAX
2+
imports UNSIGNED-INT-SYNTAX
3+
imports BOOL-SYNTAX
4+
5+
syntax Value ::= Int
6+
| Bool
7+
8+
syntax KResult ::= Value
9+
10+
syntax Expr ::= Value
11+
| "(" Expr ")" [bracket, format(%1%2%3)]
12+
13+
syntax Expr ::= "-" Expr [strict, format(%1%2)]
14+
| "!" Expr [strict, format(%1%2)]
15+
> left:
16+
Expr "*" Expr [seqstrict]
17+
| Expr "/" Expr [seqstrict]
18+
> left:
19+
Expr "+" Expr [seqstrict]
20+
| Expr "-" Expr [seqstrict]
21+
> left:
22+
Expr ">=" Expr [seqstrict]
23+
| Expr ">" Expr [seqstrict]
24+
| Expr "<=" Expr [seqstrict]
25+
| Expr "<" Expr [seqstrict]
26+
> left:
27+
Expr "==" Expr [seqstrict]
28+
| Expr "!=" Expr [seqstrict]
29+
> left:
30+
Expr "&&" Expr [strict(1)]
31+
> left:
32+
Expr "||" Expr [strict(1)]
33+
endmodule
34+
35+
36+
module EXPR-RULES
37+
imports EXPR-SYNTAX
38+
imports INT
39+
imports BOOL
40+
41+
rule - X => 0 -Int X
42+
rule ! B => notBool B
43+
44+
rule X + Y => X +Int Y
45+
rule X - Y => X -Int Y
46+
rule X * Y => X *Int Y
47+
rule X / Y => X /Int Y
48+
49+
rule I1 >= I2 => I1 >=Int I2
50+
rule I1 > I2 => I1 >Int I2
51+
rule I1 <= I2 => I1 <=Int I2
52+
rule I1 < I2 => I1 <Int I2
53+
54+
rule B1 == B2 => B1 ==Bool B2
55+
rule I1 == I2 => I1 ==Int I2
56+
57+
rule B1 != B2 => B1 =/=Bool B2
58+
rule I1 != I2 => I1 =/=Int I2
59+
60+
rule true && B => B
61+
rule false && _ => false
62+
63+
rule true || _ => true
64+
rule false || B => B
65+
endmodule
66+
67+
68+
module EXPR
69+
imports EXPR-RULES
70+
71+
configuration <k> $PGM:Expr </k>
72+
endmodule

src/kimp/kdist/imp-semantics/expressions.k

-66
This file was deleted.

src/kimp/kdist/imp-semantics/imp-verification.k

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
requires "statements.k"
1+
requires "imp.k"
2+
23

34
module IMP-VERIFICATION-SYNTAX
45
imports ID-SYNTAX
@@ -9,7 +10,7 @@ endmodule
910

1011
module IMP-VERIFICATION
1112
imports IMP-VERIFICATION-SYNTAX
12-
imports STATEMENTS
13+
imports IMP
1314

1415
// inequality sign normalization
1516
rule A >Int B => B <Int A [simplification]

src/kimp/kdist/imp-semantics/imp.k

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
requires "calc.k"
2+
3+
4+
module IMP-SYNTAX
5+
imports CALC-SYNTAX
6+
7+
syntax Stmt ::= "if" "(" Expr ")" Stmt "else" Stmt [group(stmt), strict(1), avoid, format(%1 %2%3%4% %5 %6 %7)] // dangling else
8+
| "if" "(" Expr ")" Stmt [group(stmt), format(%1 %2%3%4 %5)]
9+
| "while" "(" Expr ")" Stmt [group(stmt), format(%1 %2%3%4 %5)] // not strict!
10+
| "{" Stmt "}" [group(stmt), format(%1%i%n%2%d%n%3)]
11+
| "{" "}" [group(stmt), format(%1%2)]
12+
endmodule
13+
14+
15+
module IMP
16+
imports IMP-SYNTAX
17+
imports CALC
18+
19+
rule [if-true]: <k> if ( true ) S1 else _ => S1 ... </k>
20+
rule [if-false]: <k> if ( false ) _ else S2 => S2 ... </k>
21+
22+
rule [if-else]: <k> if ( C ) S => if ( C ) S else {} ... </k>
23+
24+
rule [while]:
25+
<k>
26+
while ( C ) S
27+
=>
28+
if ( C ) {
29+
S
30+
while ( C ) S
31+
}
32+
...
33+
</k>
34+
35+
rule [block]: <k> { S } => S ~> { } ... </k>
36+
37+
rule [done]: <k> { } => .K ... </k>
38+
endmodule

0 commit comments

Comments
 (0)