Skip to content

Commit 05697da

Browse files
committed
Refactor PWhile module namespace and improve Stmt.toString implementation for clarity
1 parent 70baf85 commit 05697da

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

ProgramAnalysis/ProbabilisticPrograms/PWhile.lean

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module
22

3-
namespace ProgramAnalysis.DataFlowAnalysis.PWhile
3+
namespace ProgramAnalysis.ProbabilisticPrograms
44

55
public abbrev Var := String
66

@@ -19,8 +19,7 @@ public def Op_a.toString : Op_a → String
1919
| .times => "*"
2020
| .div => "/"
2121

22-
public instance : ToString Op_a where
23-
toString := Op_a.toString
22+
public instance : ToString Op_a := ⟨Op_a.toString⟩
2423

2524
public inductive Op_b
2625
| and
@@ -31,8 +30,7 @@ public def Op_b.toString : Op_b → String
3130
| .and => ""
3231
| .or => ""
3332

34-
public instance : ToString Op_b where
35-
toString := Op_b.toString
33+
public instance : ToString Op_b := ⟨Op_b.toString⟩
3634

3735
public inductive Op_r
3836
| eq
@@ -51,8 +49,7 @@ public def Op_r.toString : Op_r → String
5149
| .ge => ""
5250
| .neq => ""
5351

54-
public instance : ToString Op_r where
55-
toString := Op_r.toString
52+
public instance : ToString Op_r := ⟨Op_r.toString⟩
5653

5754
public inductive ArithAtom
5855
| var : Var → ArithAtom
@@ -83,18 +80,29 @@ public def BoolAtom.toString : BoolAtom → String
8380
| .op o b1 b2 => s!"({b1.toString} {o.toString} {b2.toString})"
8481
| .rel o a1 a2 => s!"({a1.toString} {o.toString} {a2.toString})"
8582

86-
public instance : ToString BoolAtom where
87-
toString := BoolAtom.toString
83+
public instance : ToString BoolAtom := ⟨BoolAtom.toString⟩
8884

8985
public inductive Stmt
9086
| stop : Label → Stmt
9187
| skip : Label → Stmt
9288
| assign : Var → ArithAtom → Label → Stmt
93-
| assign? : Var → List ArithAtom → Label → Stmt -- probabilistic assignment
89+
| assign? : Var → List ArithAtom → Label → Stmt
9490
| seq : Stmt → Stmt → Stmt
95-
| choose : Stmt → Stmt → Stmt -- probabilistic choice
91+
| choose : Nat → Stmt → Nat → Stmt → Stmt
9692
| sif : BoolAtom → Label → Stmt → Stmt → Stmt
9793
| swhile : BoolAtom → Label → Stmt → Stmt
9894
deriving Repr, Ord, DecidableEq
9995

100-
end ProgramAnalysis.DataFlowAnalysis.PWhile
96+
public def Stmt.toString : Stmt → String
97+
| .stop l => s!"[stop]{l.toSuperscriptString}"
98+
| .skip l => s!"[skip]{l.toSuperscriptString}"
99+
| .assign var arith l => s!"[{var} := {arith}]{l.toSuperscriptString}"
100+
| .assign? var ariths l => s!"[{var} ?= {ariths}]{l.toSuperscriptString}"
101+
| .seq s1 s2 => s!"{s1.toString}\n{s2.toString}"
102+
| .choose p1 s1 p2 s2 => s!"choose {p1}:{s1.toString} or {p2}:{s2.toString} ro"
103+
| .sif b l s1 s2 => s!"if [{b}]{l.toSuperscriptString} then {s1.toString} else {s2.toString} fi"
104+
| .swhile b l s => s!"while [{b}]{l.toSuperscriptString} do {s.toString} od"
105+
106+
instance : ToString Stmt := ⟨Stmt.toString⟩
107+
108+
end ProgramAnalysis.ProbabilisticPrograms

0 commit comments

Comments
 (0)