generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 28
Formal big-step operational semantics for CBMC's GOTO IL #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tautschnig
wants to merge
17
commits into
main
Choose a base branch
from
tautschnig/goto-semantics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f5d692e
feat(Imperative): Add loop evaluation rules to EvalStmt
tautschnig 45f3e03
fix(DetToNondet): Restructure noFuncDecl_preserves_δ for loop rules
tautschnig 5842edf
feat(Imperative): Add nondet loop semantics and prove DetToNondet loo…
tautschnig b32c49f
fix(DetToNondet): Remove all sorry's using unsafe+axiom for termination
tautschnig 7e5083e
feat(GOTO): Add formal big-step operational semantics for CBMC's GOTO IL
tautschnig 9286e13
feat(GOTO): Add determinism proofs, concrete evaluator, and simulatio…
tautschnig bfca66f
docs(GOTO): Update TODO list in Semantics.lean to reflect progress
tautschnig 4375e5e
feat(GOTO): Prove all command-level simulation lemmas (0 sorry's)
tautschnig f468cd9
feat(GOTO): Prove ExecProg determinism for nondet-free programs
tautschnig 86bf8a7
feat(GOTO): Add ExecRange, block simulation, and progress statement
tautschnig 3992e9a
feat(GOTO): Prove all progress lemmas and add old() two-state evaluator
tautschnig a488229
feat(GOTO): Prove well-formed program progress (0 sorry's)
tautschnig 52d8eb3
feat(GOTO): Add loop semantics via compiled GOTO pattern and old() ev…
tautschnig 40a1298
docs(GOTO): Update TODO — loop rules now in Imperative dialect
tautschnig 3ec187d
feat(GOTO): End-to-end simulation theorem (all TODO items complete)
tautschnig f50d8a0
fix: Address Copilot review comments
tautschnig c351389
fix: Remove trailing whitespace in DetToNondetCorrect.lean
tautschnig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| /- | ||
| Copyright Strata Contributors | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| -/ | ||
|
|
||
| import Strata.Backends.CBMC.GOTO.Semantics | ||
|
|
||
| /-! | ||
| # Concrete Expression Evaluator for GOTO Expressions | ||
|
|
||
| This file defines a concrete evaluator for the GOTO expression AST (`Expr`), | ||
| instantiating the abstract `ExprEval` parameter from `Semantics.lean`. | ||
|
|
||
| ## Scope | ||
|
|
||
| Covers the expression forms produced by Strata's Lambda-to-GOTO translation | ||
| (`LambdaToCProverGOTO.lean`): | ||
|
|
||
| ### Fully handled | ||
| - **Nullary**: `symbol` (variable lookup), `constant` (literal parsing) | ||
| - **Unary**: `Not`, `UnaryMinus`, `Typecast` (bool↔int, int↔bv) | ||
| - **Binary**: arithmetic (`Div`, `Mod`, `Minus`), comparison (`Lt`, `Le`, `Gt`, | ||
| `Ge`, `Equal`, `NotEqual`), logical (`Implies`), map (`Index`) | ||
| - **Ternary**: `ite` (if-then-else), `with` (map update) | ||
| - **Multiary**: `And`, `Or`, `Plus`, `Mult` | ||
|
|
||
| ### Not handled (returns `none`) | ||
| - **Side effects**: `Nondet`, `Assign` (handled at instruction level) | ||
| - **Function application**: `functionApplication` (uninterpreted) | ||
| - **`Old`**: handled by `concreteEval₂` (two-state evaluator) | ||
| - **Quantifiers**: `Forall`, `Exists` (specification-only) | ||
| - **Bitvector bit-ops**: `Shl`, `Ashr`, `Lshr`, `Bitand`, `Bitor`, `Bitxor`, | ||
| `Concatenation`, `Extractbits` | ||
| - **Real arithmetic**: `vReal` values are parsed but arithmetic/comparison | ||
| operations on reals are not yet implemented (returns `none`) | ||
|
|
||
| ## TODO | ||
| - [ ] Bitvector bit-level operations (width-aware) | ||
| - [ ] Bitvector width normalization (truncate results modulo `2^w`) | ||
| - [ ] Real arithmetic (rational operations on `vReal`) | ||
| - [ ] Prove correspondence with Lambda expression evaluation | ||
| -/ | ||
|
|
||
| namespace CProverGOTO.Semantics | ||
|
|
||
| open CProverGOTO | ||
|
|
||
| /-! ## Constant Parsing -/ | ||
|
|
||
| /-- Parse a GOTO constant string into a Value, given its type. -/ | ||
| def parseConstant (val : String) (ty : Ty) : Option Value := | ||
| match ty.id with | ||
| | .primitive .bool => | ||
| if val == "true" then some (.vBool true) | ||
| else if val == "false" then some (.vBool false) | ||
| else none | ||
| | .primitive .integer => val.toInt?.map .vInt | ||
| | .primitive .string => some (.vString val) | ||
| | .primitive .real => val.toInt?.map (fun n => .vReal n 1) | ||
| | .bitVector (.signedbv w) => val.toInt?.map (.vBV w) | ||
| | .bitVector (.unsignedbv w) => val.toInt?.map (.vBV w) | ||
| | _ => none | ||
|
|
||
| /-- Type cast between values. -/ | ||
| def typeCast (v : Value) (targetTy : Ty) : Option Value := | ||
| match v, targetTy.id with | ||
| | .vBool b, .primitive .integer => some (.vInt (if b then 1 else 0)) | ||
| | .vInt n, .primitive .bool => some (.vBool (n != 0)) | ||
| | .vBV _ n, .bitVector (.signedbv w) => some (.vBV w n) | ||
| | .vBV _ n, .bitVector (.unsignedbv w) => some (.vBV w n) | ||
| | .vInt n, .bitVector (.signedbv w) => some (.vBV w n) | ||
| | .vInt n, .bitVector (.unsignedbv w) => some (.vBV w n) | ||
| | _, _ => none | ||
tautschnig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /-! ## Concrete Evaluator -/ | ||
|
|
||
| /-- Concrete expression evaluator for GOTO expressions. | ||
|
|
||
| Uses `partial` because GOTO expressions are tree-structured with | ||
| arbitrary nesting depth, and proving termination over the `Expr` | ||
| structure (which uses `List Expr` for operands) requires well-founded | ||
| recursion on a custom measure. -/ | ||
| partial def concreteEval : ExprEval := fun σ e => | ||
| match e.id, e.operands with | ||
| -- Nullary | ||
| | .nullary (.symbol name), [] => σ name | ||
| | .nullary (.constant val), [] => parseConstant val e.type | ||
| | .nullary .nil, [] => some .vEmpty | ||
|
|
||
| -- Unary | ||
| | .unary .Not, [op] => do | ||
| let .vBool b ← concreteEval σ op | none | ||
| some (.vBool !b) | ||
| | .unary .UnaryMinus, [op] => do | ||
| match ← concreteEval σ op with | ||
| | .vInt n => some (.vInt (-n)) | ||
| | .vBV w n => some (.vBV w (-n)) | ||
| | _ => none | ||
| | .unary .Typecast, [op] => do | ||
| let v ← concreteEval σ op | ||
| typeCast v e.type | ||
|
|
||
| -- Binary arithmetic | ||
| | .binary .Div, [l, r] => intBinOp (· / ·) σ l r | ||
| | .binary .Mod, [l, r] => intBinOp (· % ·) σ l r | ||
| | .binary .Minus, [l, r] => intBinOp (· - ·) σ l r | ||
|
|
||
| -- Binary comparison | ||
| | .binary .Lt, [l, r] => intCmp (· < ·) σ l r | ||
| | .binary .Le, [l, r] => intCmp (· ≤ ·) σ l r | ||
| | .binary .Gt, [l, r] => intCmp (· > ·) σ l r | ||
| | .binary .Ge, [l, r] => intCmp (· ≥ ·) σ l r | ||
| | .binary .Equal, [l, r] => do | ||
| some (.vBool ((← concreteEval σ l) == (← concreteEval σ r))) | ||
| | .binary .NotEqual, [l, r] => do | ||
| some (.vBool ((← concreteEval σ l) != (← concreteEval σ r))) | ||
|
|
||
| -- Binary logical | ||
| | .binary .Implies, [l, r] => do | ||
| let .vBool a ← concreteEval σ l | none | ||
| let .vBool b ← concreteEval σ r | none | ||
| some (.vBool (!a || b)) | ||
|
|
||
tautschnig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| -- Map/array operations | ||
| | .binary .Index, [arr, idx] => do | ||
| let .vArray elems ← concreteEval σ arr | none | ||
| let .vInt i ← concreteEval σ idx | none | ||
| if i < 0 then none else elems[i.toNat]? | ||
|
|
||
| -- Ternary | ||
| | .ternary .ite, [c, t, el] => do | ||
| let .vBool b ← concreteEval σ c | none | ||
| if b then concreteEval σ t else concreteEval σ el | ||
| | .ternary .«with», [arr, idx, val] => do | ||
| let .vArray elems ← concreteEval σ arr | none | ||
| let .vInt i ← concreteEval σ idx | none | ||
| let v ← concreteEval σ val | ||
| if i < 0 then none else | ||
| some (.vArray (elems.set i.toNat v)) | ||
tautschnig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| -- Multiary | ||
| | .multiary .And, ops => do | ||
| let vals ← ops.mapM (fun op => do | ||
| match ← concreteEval σ op with | ||
| | .vBool b => pure b | ||
| | _ => none) | ||
| some (.vBool (vals.all id)) | ||
| | .multiary .Or, ops => do | ||
| let vals ← ops.mapM (fun op => do | ||
| match ← concreteEval σ op with | ||
| | .vBool b => pure b | ||
| | _ => none) | ||
| some (.vBool (vals.any id)) | ||
| | .multiary .Plus, ops => do | ||
| let vals ← ops.mapM (fun op => do | ||
| match ← concreteEval σ op with | ||
| | .vInt n => pure n | ||
| | _ => none) | ||
| some (.vInt (vals.foldl (· + ·) 0)) | ||
| | .multiary .Mult, ops => do | ||
| let vals ← ops.mapM (fun op => do | ||
| match ← concreteEval σ op with | ||
| | .vInt n => pure n | ||
| | _ => none) | ||
| some (.vInt (vals.foldl (· * ·) 1)) | ||
|
|
||
| -- Unsupported | ||
| | _, _ => none | ||
| where | ||
tautschnig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| intBinOp (f : Int → Int → Int) (σ : Store) (l r : Expr) : Option Value := do | ||
| match ← concreteEval σ l, ← concreteEval σ r with | ||
| | .vInt a, .vInt b => some (.vInt (f a b)) | ||
| | .vBV w a, .vBV _ b => some (.vBV w (f a b)) | ||
| | _, _ => none | ||
| intCmp (f : Int → Int → Bool) (σ : Store) (l r : Expr) : Option Value := do | ||
| match ← concreteEval σ l, ← concreteEval σ r with | ||
| | .vInt a, .vInt b => some (.vBool (f a b)) | ||
| | .vBV _ a, .vBV _ b => some (.vBool (f a b)) | ||
| | _, _ => none | ||
|
|
||
| /-- Two-state concrete evaluator that handles `old()` expressions. | ||
| `old(e)` evaluates `e` in `σ_old`; everything else uses `σ_cur`. -/ | ||
| partial def concreteEval₂ : ExprEval₂ := fun σ_old σ_cur e => | ||
| match e.id, e.operands with | ||
| | .unary .Old, [inner] => concreteEval σ_old inner | ||
| | _, _ => concreteEval σ_cur e | ||
|
|
||
| end CProverGOTO.Semantics | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.