Skip to content

Commit eb62371

Browse files
committed
Better revision
1 parent 82fa3c7 commit eb62371

7 files changed

Lines changed: 803 additions & 134 deletions

File tree

Strata/Languages/B3/B3.lean

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/-
2+
Copyright Strata Contributors
3+
4+
SPDX-License-Identifier: Apache-2.0 OR MIT
5+
-/
6+
7+
import Strata.Languages.B3.Expression
8+
import Strata.Languages.B3.Stmt
9+
import Strata.Languages.B3.Identifiers
10+
11+
---------------------------------------------------------------------
12+
13+
namespace B3
14+
15+
/-!
16+
## B3 Language
17+
18+
B3 is a simplified imperative verification language with:
19+
- Basic types (bool, int, string)
20+
- Expressions with binary/unary operators
21+
- Statements including assignments, assertions, loops
22+
- Procedure calls with in/out/inout parameters
23+
- Quantifiers with optional patterns
24+
- Control flow (if, loop, choose, exit)
25+
-/
26+
27+
end B3
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/-
2+
Copyright Strata Contributors
3+
4+
SPDX-License-Identifier: Apache-2.0 OR MIT
5+
-/
6+
7+
import Strata.DDM.Integration.Lean
8+
import Strata.DDM.Util.Format
9+
import Strata.Languages.B3.Stmt
10+
11+
---------------------------------------------------------------------
12+
13+
namespace Strata
14+
15+
---------------------------------------------------------------------
16+
---------------------------------------------------------------------
17+
18+
/- DDM support for parsing and pretty-printing B3 -/
19+
20+
#dialect
21+
dialect B3;
22+
23+
type Expression;
24+
25+
fn not (e : Expr) : Expression => @[prec(35)] "!" e;
26+
27+
fn natLit (n : Num) : Expression => n;
28+
fn strLit (s : Str) : Expression => s;
29+
30+
fn btrue : Expression => "true";
31+
fn bfalse : Expression => "false";
32+
33+
fn id (name : Ident) : Expression => name;
34+
35+
fn ite (c : Expr, t : Expr, f : Expr) : Expression => @[prec(3)] "if " c:0 " then " t:3 " else " f:3;
36+
fn iff (a : Expr, b : Expr) : Expression => @[prec(4)] a " <==> " b;
37+
fn implies (a : Expr, b : Expr) : Expression => @[prec(5), rightassoc] a " ==> " b;
38+
fn impliedBy (a : Expr, b : Expr) : Expression => @[prec(5), rightassoc] a " <== " b;
39+
fn and (a : Expr, b : Expr) : Expression => @[prec(10), leftassoc] a " && " b;
40+
fn or (a : Expr, b : Expr) : Expression => @[prec(8), leftassoc] a " || " b;
41+
42+
fn equal (a : Expr, b : Expr) : Expression => @[prec(15)] a " == " b;
43+
fn not_equal (a : Expr, b : Expr) : Expression => @[prec(15)] a " != " b;
44+
fn le (a : Expr, b : Expr) : Expression => @[prec(15)] a " <= " b;
45+
fn lt (a : Expr, b : Expr) : Expression => @[prec(15)] a " < " b;
46+
fn ge (a : Expr, b : Expr) : Expression => @[prec(15)] a " >= " b;
47+
fn gt (a : Expr, b : Expr) : Expression => @[prec(15)] a " > " b;
48+
49+
fn neg (e : Expr) : Expression => "-" e;
50+
fn add (a : Expr, b : Expr) : Expression => @[prec(25), leftassoc] a " + " b;
51+
fn sub (a : Expr, b : Expr) : Expression => @[prec(25), leftassoc] a " - " b;
52+
fn mul (a : Expr, b : Expr) : Expression => @[prec(30), leftassoc] a " * " b;
53+
fn div (a : Expr, b : Expr) : Expression => @[prec(30), leftassoc] a " div " b;
54+
fn mod (a : Expr, b : Expr) : Expression => @[prec(30), leftassoc] a " mod " b;
55+
56+
category Statement;
57+
58+
op assign (v : Ident, e : Expr) : Statement => v:0 " := " e "\n";
59+
60+
op check (c : Expr) : Statement => "check " c "\n";
61+
op assume (c : Expr) : Statement => "assume " c "\n";
62+
op reach (c : Expr) : Statement => "reach " c "\n";
63+
op assert (c : Expr) : Statement => "assert " c "\n";
64+
65+
category Else;
66+
op else_none () : Else => "";
67+
op else_some (s : Statement) : Else => " else " s;
68+
69+
op if_statement (c : Expr, t : Statement, f : Else) : Statement =>
70+
"if " c " " t f;
71+
72+
category Invariant;
73+
op invariant (e : Expr) : Invariant => "\n invariant " e;
74+
75+
op loop_statement (invs : Seq Invariant, body : Statement) : Statement =>
76+
"loop" invs " " body;
77+
78+
op exit_statement (label : Option Ident) : Statement => "exit " label "\n";
79+
op return_statement () : Statement => "return";
80+
81+
op block (c : Seq Statement) : Statement => "{\n" indent(2, c:40) "}\n";
82+
83+
#end
84+
85+
namespace B3DDM
86+
87+
#strata_gen B3
88+
89+
end B3DDM
90+
91+
---------------------------------------------------------------------
92+
93+
end Strata
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# B3 DDM Transform
2+
3+
This directory contains the DDM (Dialect Definition Mechanism) support for the B3 language, providing parser and pretty-printer functionality.
4+
5+
## Files
6+
7+
### Parse.lean
8+
Defines the B3 dialect using DDM syntax. This includes:
9+
- Type declarations (bool, int, string)
10+
- Expression operators (binary, unary, logical)
11+
- Statement operators (assign, check, assume, assert, etc.)
12+
- Control flow constructs (if, loop, exit, return)
13+
14+
The dialect definition uses DDM's declarative syntax to specify:
15+
- Operator precedence and associativity
16+
- Pretty-printing format
17+
- Parsing rules
18+
19+
### Translate.lean
20+
Provides translation from DDM's concrete syntax tree to B3's abstract syntax tree. This includes:
21+
- Expression translation (literals, operators, variables)
22+
- Statement translation (assignments, assertions, control flow)
23+
- Type translation
24+
- Binding management for scoped variables
25+
26+
## Usage
27+
28+
The DDM dialect can be used with `#strata` blocks (similar to Boogie) to parse B3 programs directly in Lean files.
29+
30+
## Comparison with Boogie
31+
32+
This implementation follows the same pattern as `Strata/Languages/Boogie/DDMTransform/`:
33+
- `Parse.lean` defines the dialect syntax
34+
- `Translate.lean` converts DDM AST to language-specific AST
35+
- The structure is simplified for B3's smaller feature set
36+
37+
## Current Limitations
38+
39+
The current implementation provides a minimal working dialect with:
40+
- Basic expression operators
41+
- Core statement types
42+
- Simple control flow
43+
44+
Additional features from B3 (quantifiers, patterns, procedure calls, etc.) can be added incrementally by extending both Parse.lean and Translate.lean.

0 commit comments

Comments
 (0)