Skip to content

Commit 22e10d7

Browse files
atombJosh CohenMikaelMayershigoelandrewmwells-amazon
authored
Strata language definition document (#186)
Adds a document describing the semantics of Strata Core (consisting of `Lambda` and `Imperative` components). The document is written in Verso and imports the Strata library to allow docstrings to appear directly in the text. Note that Strata Core is not a new dialect, but rather a new name for the combination of `Lambda` and `Imperative`. It does not yet have a concrete syntax. Concrete syntax will likely be provided through an evolution of #224, to assist in the goal of keeping Strata Core as close to B3 as possible. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Co-authored-by: Josh Cohen <cohenjo@amazon.com> Co-authored-by: Mikaël Mayer <MikaelMayer@users.noreply.github.com> Co-authored-by: Shilpi Goel <shigoel@gmail.com> Co-authored-by: Andrew Wells <130512013+andrewmwells-amazon@users.noreply.github.com>
1 parent ee0f0f9 commit 22e10d7

30 files changed

Lines changed: 688 additions & 149 deletions

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ jobs:
116116
run: .github/scripts/checkLeanImport.sh
117117

118118
build_doc:
119-
name: Build Documentation
119+
name: Build documentation
120120
runs-on: ubuntu-latest
121121
permissions:
122122
contents: read
@@ -126,10 +126,10 @@ jobs:
126126
uses: leanprover/lean-action@v1
127127
with:
128128
build-args: '--wfail'
129-
lake-package-directory: 'docs/ddm'
130-
- name: Build Documentation
131-
run: lake exe docs
132-
working-directory: docs/ddm
129+
lake-package-directory: 'docs/verso'
130+
- name: Build documentation
131+
run: ./generate.sh
132+
working-directory: docs/verso
133133

134134
build_python:
135135
name: Build and test Python

Strata/DL/Imperative/Cmd.lean

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ variable declaration and assignment, and assertions and assumptions.
2828
-/
2929

3030
/--
31-
A command in the Imperative dialect
31+
A an atomic command in the `Imperative` dialect.
32+
33+
Commands don't create local control flow, and are typically used as a parameter
34+
to `Imperative.Stmt` or other similar types.
3235
-/
3336
inductive Cmd (P : PureExpr) : Type where
34-
/-- `init` defines a variable called `name` with type `ty` and
35-
initial value `e`. -/
37+
/-- Define a variable called `name` with type `ty` and initial value `e`.
38+
Note: we may make the initial value optional. -/
3639
| init (name : P.Ident) (ty : P.Ty) (e : P.Expr) (md : (MetaData P) := .empty)
37-
/-- `set` assigns `e` to a pre-existing variable `name`. -/
40+
/-- Assign `e` to a pre-existing variable `name`. -/
3841
| set (name : P.Ident) (e : P.Expr) (md : (MetaData P) := .empty)
39-
/-- `havoc` assigns a pre-existing variable `name` a random value. -/
42+
/-- Assigns an arbitrary value to an existing variable `name`. -/
4043
| havoc (name : P.Ident) (md : (MetaData P) := .empty)
41-
/-- `assert` checks whether condition `b` is true. -/
44+
/-- Check whether condition `b` is true, failing if not. -/
4245
| assert (label : String) (b : P.Expr) (md : (MetaData P) := .empty)
43-
/-- `assume` constrains execution by adding assumption `b`. -/
46+
/-- Ignore any execution state in which `b` is not true. -/
4447
| assume (label : String) (b : P.Expr) (md : (MetaData P) := .empty)
4548

4649
abbrev Cmds (P : PureExpr) := List (Cmd P)

Strata/DL/Imperative/CmdSemantics.lean

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,15 @@ def WellFormedSemanticEvalVar {P : PureExpr} [HasFvar P] (δ : SemanticEval P)
234234

235235
def WellFormedSemanticEvalExprCongr {P : PureExpr} [HasVarsPure P P.Expr] (δ : SemanticEval P)
236236
: Prop := ∀ e σ σ', (∀ x ∈ HasVarsPure.getVars e, σ x = σ' x) → δ σ e = δ σ' e
237+
237238
/--
238-
An inductive rule for state update.
239+
Abstract variable update.
240+
241+
This does not specify how `σ` is represented, only what it maps each variable to.
239242
-/
240243
inductive UpdateState : SemanticStore P → P.Ident → P.Expr → SemanticStore P → Prop where
244+
/-- The state `σ'` is be equivalent to `σ` except at `x`, where it maps to
245+
`v`. Requires that `x` mapped to something beforehand. -/
241246
| update :
242247
σ x = .some v' →
243248
σ' x = .some v →
@@ -246,9 +251,13 @@ inductive UpdateState : SemanticStore P → P.Ident → P.Expr → SemanticStore
246251
UpdateState σ x v σ'
247252

248253
/--
249-
An inductive rule for state init.
254+
Abtract variable initialization.
255+
256+
This does not specify how `σ` is represented, only what it maps each variable to.
250257
-/
251258
inductive InitState : SemanticStore P → P.Ident → P.Expr → SemanticStore P → Prop where
259+
/-- The state `σ'` is be equivalent to `σ` except at `x`, where it maps to
260+
`v`. Requires that `x` mapped to nothing beforehand. -/
252261
| init :
253262
σ x = none →
254263
σ' x = .some v →
@@ -257,37 +266,43 @@ inductive InitState : SemanticStore P → P.Ident → P.Expr → SemanticStore P
257266
InitState σ x v σ'
258267

259268
/--
260-
An inductively-defined operational semantics that depends on
261-
environment lookup and evaluation functions for expressions.
269+
An inductively-defined operational semantics for `Cmd` that depends on variable
270+
lookup (`σ`) and expression evaluation (`δ`) functions.
262271
-/
263272
inductive EvalCmd [HasFvar P] [HasBool P] [HasNot P] :
264273
SemanticEval P → SemanticStore P → Cmd P → SemanticStore P → Prop where
274+
/-- If `e` evaluates to a value `v`, initialize `x` according to `InitState`. -/
265275
| eval_init :
266276
δ σ e = .some v →
267277
InitState P σ x v σ' →
268278
WellFormedSemanticEvalVar δ →
269279
---
270280
EvalCmd δ σ (.init x _ e _) σ'
271281

282+
/-- If `e` evaluates to a value `v`, assign `x` according to `UpdateState`. -/
272283
| eval_set :
273284
δ σ e = .some v →
274285
UpdateState P σ x v σ' →
275286
WellFormedSemanticEvalVar δ →
276287
----
277288
EvalCmd δ σ (.set x e _) σ'
278289

290+
/-- Assign `x` an arbitrary value `v` according to `UpdateState`. -/
279291
| eval_havoc :
280292
UpdateState P σ x v σ' →
281293
WellFormedSemanticEvalVar δ →
282294
----
283295
EvalCmd δ σ (.havoc x _) σ'
284296

297+
/-- If `e` evaluates to true in `σ`, evaluate to the same `σ`. This semantics
298+
does not have a concept of an erroneous execution. -/
285299
| eval_assert :
286300
δ σ e = .some HasBool.tt →
287301
WellFormedSemanticEvalBool δ →
288302
----
289303
EvalCmd δ σ (.assert _ e _) σ
290304

305+
/-- If `e` evaluates to true in `σ`, evaluate to the same `σ`. -/
291306
| eval_assume :
292307
δ σ e = .some HasBool.tt →
293308
WellFormedSemanticEvalBool δ →

Strata/DL/Imperative/MetaData.lean

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ open Std (ToFormat Format format)
2424

2525
variable {Identifier : Type} [DecidableEq Identifier] [ToFormat Identifier] [Inhabited Identifier]
2626

27-
/-- A metadata field.
27+
/-- A metadata field, which can be either a variable or an arbitrary string label.
2828
2929
For now, we only track the variables modified by a construct, but we will expand
3030
this in the future.
3131
-/
3232
inductive MetaDataElem.Field (P : PureExpr) where
33+
/-- Metadata indexed by a Strata variable. -/
3334
| var (v : P.Ident)
35+
/-- Metadata indexed by an arbitrary label. -/
3436
| label (l : String)
3537

3638
@[grind]
@@ -61,9 +63,11 @@ instance [Repr P.Ident] : Repr (MetaDataElem.Field P) where
6163
| .label s => f!"MetaDataElem.Field.label {s}"
6264
Repr.addAppParen res prec
6365

64-
/-- A metadata value. -/
66+
/-- A metadata value, which can be either an expression or a message. -/
6567
inductive MetaDataElem.Value (P : PureExpr) where
68+
/-- Metadata value in the form of a structured expression. -/
6669
| expr (e : P.Expr)
70+
/-- Metadata value in the form of an arbitrary string. -/
6771
| msg (s : String)
6872

6973
instance [ToFormat P.Expr] : ToFormat (MetaDataElem.Value P) where
@@ -103,7 +107,9 @@ instance [DecidableEq P.Expr] : DecidableEq (MetaDataElem.Value P) :=
103107

104108
/-- A metadata element -/
105109
structure MetaDataElem (P : PureExpr) where
110+
/-- The field or key used to identify the metadata. -/
106111
fld : MetaDataElem.Field P
112+
/-- The value of the metadata. -/
107113
value : MetaDataElem.Value P
108114

109115
/-- Metadata is an array of tagged elements. -/

Strata/DL/Imperative/NondetStmt.lean

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@ Comamnds](https://en.wikipedia.org/wiki/Guarded_Command_Language), and in
2222
[Kleene Algebra with Tests](https://www.cs.cornell.edu/~kozen/Papers/kat.pdf).
2323
-/
2424

25+
/--
26+
A non-deterministic statement, parameterized by a type of pure expressions (`P`)
27+
and a type of commands (`Cmd`).
28+
29+
This encodes the same types of control flow as `Stmt`, but using only
30+
non-deterministic choices: arbitrarily choosing one of two sub-statements to
31+
execute or executing a sub-statement an arbitrary number of times. Conditions
32+
can be encoded if the command type includes assumptions.
33+
-/
2534
inductive NondetStmt (P : PureExpr) (Cmd : Type) : Type where
35+
/-- An atomic command, of an arbitrary type. -/
2636
| cmd (cmd : Cmd)
37+
/-- Execute `s1` followed by `s2`. -/
2738
| seq (s1 s2 : NondetStmt P Cmd)
39+
/-- Execute either `s1` or `s2`, arbitrarily. -/
2840
| choice (s1 s2 : NondetStmt P Cmd)
41+
/-- Execute `s` an arbitrary number of times (possibly zero). -/
2942
| loop (s : NondetStmt P Cmd)
3043
deriving Inhabited
3144

Strata/DL/Imperative/NondetStmtSemantics.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ namespace Imperative
1414
mutual
1515

1616
/-- An inductively-defined operational semantics for non-deterministic
17-
statements that depends on environment lookup and evaluation functions
18-
for expressions. -/
17+
statements that depends on environment lookup and evaluation functions for
18+
expressions. **NOTE:** This will probably be replaced with a small-step
19+
semantics. -/
1920
inductive EvalNondetStmt (P : PureExpr) (Cmd : Type) (EvalCmd : EvalCmdParam P Cmd)
2021
[HasVarsImp P (List (Stmt P Cmd))] [HasVarsImp P Cmd] [HasFvar P] [HasBool P] [HasNot P] :
2122
SemanticEval P → SemanticStore P → NondetStmt P Cmd → SemanticStore P → Prop where

Strata/DL/Imperative/Stmt.lean

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,30 @@ Imperative's Statements include commands and add constructs like structured and
1717
unstructured control-flow.
1818
-/
1919

20+
/-- Imperative statements focused on control flow.
21+
22+
The `P` parameter specifies the type of expressions that appear in conditional
23+
and loop guards. The `Cmd` parameter specifies the type of atomic command
24+
contained within the `.cmd` constructor.
25+
-/
2026
inductive Stmt (P : PureExpr) (Cmd : Type) : Type where
27+
/-- An atomic command. -/
2128
| cmd (cmd : Cmd)
29+
/-- An block containing a `List` of `Stmt`. -/
2230
| block (label : String) (b : List (Stmt P Cmd)) (md : MetaData P := .empty)
23-
/-- `ite` (if-then-else) statement provides structured control flow. -/
31+
/-- A conditional execution statement. -/
2432
| ite (cond : P.Expr) (thenb : List (Stmt P Cmd)) (elseb : List (Stmt P Cmd)) (md : MetaData P := .empty)
25-
/-- `loop` Loop statement with optional measure (for termination) and invariant. -/
33+
/-- An iterated execution statement. Includes an optional measure (for
34+
termination) and invariant. -/
2635
| loop (guard : P.Expr) (measure : Option P.Expr) (invariant : Option P.Expr) (body : List (Stmt P Cmd)) (md : MetaData P := .empty)
27-
/-- `goto` provides unstructured control flow. -/
36+
/-- A semi-structured control flow statement transferring control to the given
37+
label. The control flow induced by `goto` must not create cycles. **NOTE:**
38+
This will likely be removed, in favor of an alternative view of imperative
39+
programs that is purely untructured. -/
2840
| goto (label : String) (md : MetaData P := .empty)
2941
deriving Inhabited
3042

43+
/-- A block is simply an abbreviation for a list of commands. -/
3144
abbrev Block (P : PureExpr) (Cmd : Type) := List (Stmt P Cmd)
3245

3346
def Stmt.isCmd {P : PureExpr} {Cmd : Type} (s : Stmt P Cmd) : Bool :=

Strata/DL/Imperative/StmtSemanticsSmallStep.lean

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ dialect's statement constructs.
2020
/--
2121
Configuration for small-step semantics, representing the current execution
2222
state. A configuration consists of:
23-
- The current statement being executed
23+
- The current statement (or list of statements) being executed
2424
- The current store
2525
-/
2626
inductive Config (P : PureExpr) (CmdT : Type) : Type where
27+
/-- A single statement to execute next. -/
2728
| stmt : Stmt P CmdT → SemanticStore P → Config P CmdT
29+
/-- A list of statements to execute next, in order. -/
2830
| stmts : List (Stmt P CmdT) → SemanticStore P → Config P CmdT
31+
/-- A terminal configuration, indicating that execution has finished. -/
2932
| terminal : SemanticStore P → Config P CmdT
3033

3134
/--
@@ -41,22 +44,22 @@ inductive StepStmt
4144
[HasBool P] [HasNot P] :
4245
SemanticEval P → SemanticStore P → Config P CmdT → Config P CmdT → Prop where
4346

44-
/-- Command: a command steps to terminal configuration if it
45-
evaluates successfully -/
47+
/-- A command steps to terminal configuration if it evaluates successfully -/
4648
| step_cmd :
4749
EvalCmd δ σ c σ' →
4850
----
4951
StepStmt P EvalCmd δ σ
5052
(.stmt (.cmd c) σ)
5153
(.terminal σ')
5254

53-
/-- Block: a labeled block steps to its statement list -/
55+
/-- A labeled block steps to its statement list. -/
5456
| step_block :
5557
StepStmt P EvalCmd δ σ
5658
(.stmt (.block _ ss _) σ)
5759
(.stmts ss σ)
5860

59-
/-- Conditional (true): if condition evaluates to true, step to then-branch -/
61+
/-- If the condition of an `ite` statement evaluates to true, step to the then
62+
branch. -/
6063
| step_ite_true :
6164
δ σ c = .some HasBool.tt →
6265
WellFormedSemanticEvalBool δ →
@@ -65,7 +68,8 @@ inductive StepStmt
6568
(.stmt (.ite c tss ess _) σ)
6669
(.stmts tss σ)
6770

68-
/-- Conditional (false): if condition evaluates to false, step to else-branch -/
71+
/-- If the condition of an `ite` statement evaluates to false, step to the else
72+
branch. -/
6973
| step_ite_false :
7074
δ σ c = .some HasBool.ff →
7175
WellFormedSemanticEvalBool δ →
@@ -74,7 +78,7 @@ inductive StepStmt
7478
(.stmt (.ite c tss ess _) σ)
7579
(.stmts ess σ)
7680

77-
/-- Loop (guard true): if guard is true, execute body then loop again -/
81+
/-- If a loop guard is true, execute the body and then loop again. -/
7882
| step_loop_enter :
7983
δ σ g = .some HasBool.tt →
8084
WellFormedSemanticEvalBool δ →
@@ -83,7 +87,7 @@ inductive StepStmt
8387
(.stmt (.loop g m inv body md) σ)
8488
(.stmts (body ++ [.loop g m inv body md]) σ)
8589

86-
/-- Loop (guard false): if guard is false, terminate the loop -/
90+
/-- If a loop guard is false, terminate the loop. -/
8791
| step_loop_exit :
8892
δ σ g = .some HasBool.ff →
8993
WellFormedSemanticEvalBool δ →
@@ -94,14 +98,14 @@ inductive StepStmt
9498

9599
/- Goto: not implemented, because we plan to remove it. -/
96100

97-
/-- Empty statement list: no statements left to execute -/
101+
/-- An empty list of statements steps to `.terminal` with no state changes. -/
98102
| step_stmts_nil :
99103
StepStmt P EvalCmd δ σ
100104
(.stmts [] σ)
101105
(.terminal σ)
102106

103-
/-- Statement composition: after executing a statement, continue with
104-
remaining statements -/
107+
/-- To evaluate a sequence of statements, evaluate the first statement and
108+
then evaluate the remaining statements in the resulting state. -/
105109
| step_stmt_cons :
106110
StepStmt P EvalCmd δ σ (.stmt s σ) (.terminal σ') →
107111
----

Strata/DL/Lambda/Identifiers.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ section Identifiers
2020
Identifiers with a name and additional metadata
2121
-/
2222
structure Identifier (IDMeta : Type) : Type where
23+
/-- A unique name. -/
2324
name : String
25+
/-- Any additional metadata that it would be useful to attach to an
26+
identifier. -/
2427
metadata : IDMeta
2528
deriving Repr, DecidableEq, Inhabited
2629

0 commit comments

Comments
 (0)