Skip to content

Commit 042d51d

Browse files
committed
Add constraints' method to Expr and refactor toSuperscript usage; create TutorialSheet5 example
1 parent 24f07c2 commit 042d51d

5 files changed

Lines changed: 77 additions & 16 deletions

File tree

ProgramAnalysis/ControlFlowAnalysis/Analysis.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public def Expr.constraints : Expr → ReaderM (List FnTerm) (Set Constraint)
9898
.ofList (fns.map (fun t => Constraint.conditional t (.cache t1.label) (.cache t2.label) (.env t.var))) ∪
9999
.ofList (fns.map (fun t => Constraint.conditional t (.cache t1.label) (.cache t.body.label) (.cache label)))
100100

101+
public def Expr.constraints' : Expr → (Set Constraint) := fun expr => expr.constraints.run (expr.allFns)
102+
101103
public abbrev Constraint.Node := ConcreteDomain
102104

103105
public def Constraint.nodes : Constraint → List Node

ProgramAnalysis/ControlFlowAnalysis/Basic.lean

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@ module
33
public import Std.Data.TreeSet
44
public import Std.Data.TreeMap
55

6-
public def Char.toSuperScript : Char → Char
7-
| '0' => '⁰'
8-
| '1' => '¹'
9-
| '2' => '²'
10-
| '3' => '³'
11-
| '4' => '⁴'
12-
| '5' => '⁵'
13-
| '6' => '⁶'
14-
| '7' => '⁷'
15-
| '8' => '⁸'
16-
| '9' => '⁹'
17-
| c => c
18-
19-
public def Nat.toSuperscript (n : Nat) : String := (toString n).map Char.toSuperScript
20-
216
public def Std.TreeSet.subset (s1 s2 : Std.TreeSet α cmp) : Bool :=
227
s1.all s2.contains
238

ProgramAnalysis/ControlFlowAnalysis/Fun.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public def Term.toString : Term → String
7878
| .letin x e1 e2 => s!"(let {x} = {e1.toString} in {e2.toString})"
7979

8080
public def Expr.toString : Expr → String
81-
| .e t l => s!"{t.toString}{l.toSuperscript}"
81+
| .e t l => s!"{t.toString}{l.toSuperscriptString}"
8282
end
8383
end
8484

ProgramAnalysis/Example.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module
22

33
import ProgramAnalysis.Example.Exam2425
4+
import ProgramAnalysis.Example.TutorialSheet5
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module
2+
import ProgramAnalysis.DataFlowAnalysis
3+
meta import ProgramAnalysis.DataFlowAnalysis
4+
import ProgramAnalysis.ControlFlowAnalysis
5+
meta import ProgramAnalysis.ControlFlowAnalysis
6+
7+
namespace ProgramAnalysis.Example
8+
9+
section Exercise2
10+
11+
open ControlFlowAnalysis
12+
13+
def expr : Expr := [Fun|
14+
(let g = (fn f => (if (f 3) then 10 else 5))
15+
in (g (fn y => (y > 2))))
16+
]
17+
18+
/--
19+
info: (let g = (fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶)⁷ in (g⁸ (fn y => (y⁹ > 2¹⁰)¹¹)¹²)¹³)¹⁴
20+
-/
21+
#guard_msgs in
22+
#eval IO.println expr
23+
24+
def constraints := expr.constraints'
25+
26+
/--
27+
info: C(4) ⊆ C(6)
28+
C(5) ⊆ C(6)
29+
C(7) ⊆ r(g)
30+
C(13) ⊆ C(14)
31+
r(f) ⊆ C(1)
32+
r(g) ⊆ C(8)
33+
r(y) ⊆ C(9)
34+
fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶ ⊆ C(7)
35+
fn y => (y⁹ > 2¹⁰)¹¹ ⊆ C(12)
36+
fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶ ⊆ C(1) => C(2) ⊆ r(f)
37+
fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶ ⊆ C(1) => C(6) ⊆ C(3)
38+
fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶ ⊆ C(8) => C(6) ⊆ C(13)
39+
fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶ ⊆ C(8) => C(12) ⊆ r(f)
40+
fn y => (y⁹ > 2¹⁰)¹¹ ⊆ C(1) => C(2) ⊆ r(y)
41+
fn y => (y⁹ > 2¹⁰)¹¹ ⊆ C(1) => C(11) ⊆ C(3)
42+
fn y => (y⁹ > 2¹⁰)¹¹ ⊆ C(8) => C(11) ⊆ C(13)
43+
fn y => (y⁹ > 2¹⁰)¹¹ ⊆ C(8) => C(12) ⊆ r(y)
44+
-/
45+
#guard_msgs in
46+
#eval constraints.forM IO.println
47+
48+
def solution := Constraint.solve constraints.toList
49+
50+
/--
51+
info: C(1) ↦ [fn y => (y⁹ > 2¹⁰)¹¹]
52+
C(2) ↦ []
53+
C(3) ↦ []
54+
C(4) ↦ []
55+
C(5) ↦ []
56+
C(6) ↦ []
57+
C(7) ↦ [fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶]
58+
C(8) ↦ [fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶]
59+
C(9) ↦ []
60+
C(11) ↦ []
61+
C(12) ↦ [fn y => (y⁹ > 2¹⁰)¹¹]
62+
C(13) ↦ []
63+
C(14) ↦ []
64+
r(f) ↦ [fn y => (y⁹ > 2¹⁰)¹¹]
65+
r(g) ↦ [fn f => (if (f¹ 3²)³ then 10⁴ else 5⁵)⁶]
66+
r(y) ↦ []
67+
-/
68+
#guard_msgs in
69+
#eval solution.toList.forM (fun (node, value) => IO.println s!"{node}{value.toList.map (fun t: FnTerm => t)}")
70+
71+
end Exercise2
72+
73+
end ProgramAnalysis.Example

0 commit comments

Comments
 (0)