forked from strata-org/Strata
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDDMDefinition.lean
More file actions
72 lines (53 loc) · 1.94 KB
/
DDMDefinition.lean
File metadata and controls
72 lines (53 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/-
Copyright Strata Contributors
SPDX-License-Identifier: Apache-2.0 OR MIT
-/
import Strata.DDM.Integration.Lean
---------------------------------------------------------------------
/-! # Getting Started with `ArithPrograms`
## Concrete Syntax Definition using Strata's DDM
Strata's Dialect Definition Mechanism (DDM) offers the ability to define a
dialect's concrete syntax in a declarative fashion, after which we get parsing
and preliminary type checking.
-/
#dialect
dialect ArithPrograms;
// Types
type bool;
type num;
// Literals
fn numLit (n : Num) : num => n;
fn btrue : bool => "true";
fn bfalse : bool => "false";
// Expressions
fn add_expr (a : num, b : num) : num => @[prec(25), leftassoc] a "+" b;
fn mul_expr (a : num, b : num) : num => @[prec(27), leftassoc] a "*" b;
fn eq_expr (tp : Type, a : tp, b : tp) : bool => @[prec(20)] a "==" b;
category Label;
op label (l : Ident) : Label => "[" l "]:";
@[declare(v, tp)]
op init (v : Ident, tp : Type, e : tp) : Command => "init " v " : " tp " := " e ";\n";
@[declare(v, tp)]
op var (v : Ident, tp : Type) : Command => "var " v " : " tp";\n";
op assign (v : Ident, e : Expr) : Command => v " := " e ";\n";
op assume (label : Label, c : bool) : Command => "assume " label c ";\n";
op assert (label : Label, c : bool) : Command => "assert " label c ";\n";
op havoc (v : Ident) : Command => "havoc " v ";\n";
#end
/-- Example: syntax of a program in the `ArithPrograms` dialect -/
private def testEnv :=
#strata
program ArithPrograms;
init x : num := 0;
assert [test]: (x == 0);
#end
---------------------------------------------------------------------
namespace ArithPrograms
/- Automatically generate Lean types from the DDM definitions above. -/
-- set_option trace.Strata.generator true
-- set_option trace.Strata.DDM.syntax true
#strata_gen ArithPrograms
-- #print Command.toAst
-- #print Command.ofAst
end ArithPrograms
---------------------------------------------------------------------