forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_extrasScript.sml
154 lines (142 loc) · 4.34 KB
/
ast_extrasScript.sml
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
(*
Useful predicates on the CakeML ast.
*)
open preamble astTheory semanticPrimitivesPropsTheory;
val _ = new_theory "ast_extras";
Definition every_exp_def[simp]:
(every_exp f (Raise e) <=>
f (Raise e) /\ every_exp f e) /\
(every_exp f (Handle e pes) <=>
f (Handle e pes) /\
every_exp f e /\
EVERY (every_exp f) (MAP SND pes)) /\
(every_exp f (Con ts es) <=>
f (Con ts es) /\
EVERY (every_exp f) es) /\
(every_exp f (Fun n e) <=>
f (Fun n e) /\
every_exp f e) /\
(every_exp f (App op es) <=>
f (App op es) /\
EVERY (every_exp f) es) /\
(every_exp f (Log lop e1 e2) <=>
f (Log lop e1 e2) /\
every_exp f e1 /\
every_exp f e2) /\
(every_exp f (If e1 e2 e3) <=>
f (If e1 e2 e3) /\
every_exp f e1 /\
every_exp f e2 /\
every_exp f e3) /\
(every_exp f (Mat e pes) <=>
f (Mat e pes) /\
every_exp f e /\
EVERY (every_exp f) (MAP SND pes)) /\
(every_exp f (Let n x e) <=>
f (Let n x e) /\
every_exp f x /\
every_exp f e) /\
(every_exp f (Letrec funs e) <=>
f (Letrec funs e) /\
every_exp f e /\
EVERY (every_exp f) (MAP (SND o SND) funs)) /\
(every_exp f (Tannot e t) <=>
f (Tannot e t) /\
every_exp f e) /\
(every_exp f (Lannot e l) <=>
f (Lannot e l) /\
every_exp f e) /\
(every_exp f (FpOptimise sc e) <=>
f (FpOptimise sc e) /\
every_exp f e) /\
(every_exp f e <=> f e)
Termination
WF_REL_TAC `measure (exp_size o SND)`
\\ rw [exp_size_eq, MEM_MAP, EXISTS_PROD]
\\ gvs [MEM_SPLIT, list_size_append, list_size_def,
basicSizeTheory.pair_size_def]
End
Theorem every_exp_the[simp]:
!P e. every_exp P e ==> P e
Proof
gen_tac \\ Induct \\ rw []
QED
Theorem every_exp_mono:
!P e P'.
(!e. P e ==> P' e) ==> every_exp P e ==> every_exp P' e
Proof
ho_match_mp_tac every_exp_ind
\\ fsrw_tac [SATISFY_ss] [EVERY_MEM]
QED
Definition every_pat_def[simp]:
every_pat f (Pcon opt ps) = (f (Pcon opt ps) ∧ EVERY (every_pat f) ps) ∧
every_pat f (Pref p) = (f (Pref p) ∧ every_pat f p) ∧
every_pat f (Pas p s) = (f (Pas p s) ∧ every_pat f p) ∧
every_pat f (Ptannot p t) = (f (Ptannot p t) ∧ every_pat f p) ∧
every_pat f p = f p
Termination
wf_rel_tac ‘measure (pat_size o SND)’
End
Theorem every_pat_the[simp]:
∀P p. every_pat P p ⇒ P p
Proof
gen_tac \\ Induct \\ rw []
QED
Theorem every_pat_mono:
∀P p P'.
(∀p. P p ⇒ P' p) ⇒ every_pat P p ⇒ every_pat P' p
Proof
ho_match_mp_tac every_pat_ind
\\ rw [] \\ gs [EVERY_MEM]
QED
Definition every_dec_def[simp]:
(every_dec f (Dmod n ds) <=>
f (Dmod n ds) /\
EVERY (every_dec f) ds) /\
(every_dec f (Dlocal ds1 ds2) <=>
f (Dlocal ds1 ds2) /\
EVERY (every_dec f) ds1 /\
EVERY (every_dec f) ds2) /\
(every_dec f d <=>
f d)
Termination
WF_REL_TAC `measure (dec_size o SND)`
End
Definition freevars_def[simp]:
freevars (Lit l) = {} ∧
freevars (Raise e) = freevars e ∧
freevars (Handle e pes) =
(freevars e ∪ BIGUNION (set (MAP (λ(p,e). freevars e DIFF
set (MAP Short (pat_bindings p []))) pes))) ∧
freevars (Con cn es) = BIGUNION (set (MAP freevars es)) ∧
freevars (Var n) = {n} ∧
freevars (Fun n e) = freevars e DIFF {Short n} ∧
freevars (App op xs) = BIGUNION (set (MAP freevars xs)) ∧
freevars (Log lop x y) = (freevars x ∪ freevars y) ∧
freevars (If x y z) = (freevars x ∪ freevars y ∪ freevars z) ∧
freevars (Mat e pes) =
(freevars e ∪ BIGUNION (set (MAP (λ(p,e). freevars e DIFF
set (MAP Short (pat_bindings p []))) pes))) ∧
freevars (Let opt x y) =
(freevars x ∪
(freevars y DIFF (case opt of SOME n => {Short n} | _ => {}))) ∧
freevars (Letrec f x) =
(BIGUNION (set (MAP (λ(fn,vn,x). freevars x DIFF
{Short fn; Short vn}) f)) ∪
(freevars x DIFF set (MAP (Short o FST) f))) ∧
freevars (Tannot x t) = freevars x ∧
freevars (Lannot x l) = freevars x ∧
freevars (FpOptimise sc e) = freevars e
Termination
wf_rel_tac ‘measure exp_size’
End
(* Partial applications of closures.
*)
Definition do_partial_app_def:
do_partial_app f v =
case f of
Closure env n (Fun n' e) =>
SOME (Closure (env with v := nsBind n v env.v) n' e)
| _ => NONE
End
val _ = export_theory ();