-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathnumBoolRangeEncoderProgScript.sml
222 lines (198 loc) · 7.83 KB
/
numBoolRangeEncoderProgScript.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(*
Encoding program for the numBoolRange datatype
*)
open preamble basis miscTheory set_sepTheory listTheory cnfTheory;
open boolExpToCnfTheory quantifierExpTheory orderEncodingBoolTheory;
open numBoolExpTheory numBoolExtendedTheory numBoolRangeTheory;
open (* for parsing: *) parsingTheory source_valuesTheory mlstringTheory;
open toCnfHelperTheory sat_encodersProgTheory;
val _ = new_theory "numBoolRangeEncoderProg";
val _ = translation_extends "sat_encodersProg";
val _ = show_assums := true;
Definition from_v_to_num_def:
from_v_to_num (Num n) = n ∧
from_v_to_num (Pair _ _) = 0
End
Definition from_v_to_numBoolRange_def:
from_v_to_numBoolRange (Num n) = (RBoolVar n) ∧
from_v_to_numBoolRange (Pair v1 v2) =
if v1 = Name "and" then
(RAnd (from_v_to_numBoolRange (head v2)) (from_v_to_numBoolRange (head (tail v2))))
else if v1 = Name "or" then
(ROr (from_v_to_numBoolRange (head v2)) (from_v_to_numBoolRange (head (tail v2))))
else if v1 = Name "not" then
(RNot (from_v_to_numBoolRange (head v2)))
else if v1 = Name "impl" then
(RImpl (from_v_to_numBoolRange (head v2)) (from_v_to_numBoolRange (head (tail v2))))
else if v1 = Name "iff" then
(RIff (from_v_to_numBoolRange (head v2)) (from_v_to_numBoolRange (head (tail v2))))
else if v1 = Name "add" then
(RAdd (from_v_to_num (head v2))
(from_v_to_num (head (tail v2)))
(from_v_to_num (head (tail (tail v2)))))
else if v1 = Name "eq" then
(REq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "neq" then
(RNeq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "lt" then
(RLt (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "leq" then
(RLeq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "gt" then
(RGt (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "geq" then
(RGeq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "eqConst" then
(REqConst (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "neqConst" then
(RNeqConst (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "ltConst" then
(RLtConst (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "leqConst" then
(RLeqConst (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "gtConst" then
(RGtConst (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "geqConst" then
(RGeqConst (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "constEq" then
(RConstEq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "constNeq" then
(RConstNeq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "constLt" then
(RConstLt (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "constLeq" then
(RConstLeq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "constGt" then
(RConstGt (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else if v1 = Name "constGeq" then
(RConstGeq (from_v_to_num (head v2))
(from_v_to_num (head (tail v2))))
else (RBoolVar 0)
Termination
WF_REL_TAC ‘measure v_size’ \\ rpt strip_tac
\\ rpt (goal_term (fn tm => tmCases_on (find_term is_var (rator tm)) [] \\ fs []))
End
Definition from_v_to_rangeList_inner_def:
from_v_to_rangeList_inner (Pair (Num x) (Pair (Num min) (Pair (Num max) (_)))) =
(x, (min, max)) ∧
from_v_to_rangeList_inner _ = (0, (0, 0))
End
Definition from_v_to_rangeList_def:
from_v_to_rangeList rangelist = ((MAP from_v_to_rangeList_inner (v2list rangelist)):rangeList)
End
Definition parse_to_numBoolRange_def:
parse_to_numBoolRange s =
(from_v_to_numBoolRange (head s), (from_v_to_rangeList(tail s)))
End
Definition sat_to_assignment_def:
sat_to_assignment w' [] = w' ∧
sat_to_assignment w' (l::ls) = case l of
| (Pair _ _) => sat_to_assignment w' ls
| (Num n) => sat_to_assignment w'⦇n ↦ T⦈ ls
End
Definition numVarAssignment_to_output_def:
numVarAssignment_to_output (w':numVarAssignment) [] = List [] ∧
numVarAssignment_to_output w' (rangeListVariable::rangeList) =
Append (List [num_to_str (FST rangeListVariable); strlit " = " ;num_to_str (w' (FST rangeListVariable)); strlit "\n"])
(numVarAssignment_to_output w' rangeList)
End
Definition bool_to_str_def:
bool_to_str T = strlit "T" ∧
bool_to_str F = strlit "F"
End
Definition assignment_to_output_def:
assignment_to_output (w:assignment) [] = List [] ∧
assignment_to_output w (boolVar::boolVarList) = Append
(List [num_to_str (boolVar); strlit " = ";
bool_to_str (w boolVar) ; strlit "\n"])
(assignment_to_output w boolVarList)
End
Definition main_function2_def:
main_function2 v_exp sat sat_exists =
let numBoolRange = parse_to_numBoolRange v_exp in
case (rangeList_ok (SND numBoolRange)) of
| F => (List [strlit "Invalid rangeList input"])
| T =>
case (exp_rangeList_ok
(SND numBoolRange) (FST numBoolRange)) of
| F => (List [strlit "Invalid expression and rangeList"])
| T =>
let cnf_exp =
(numBoolRange_to_cnf
(SND numBoolRange) (FST numBoolRange)) in
let (max_var, clauses) =
get_max_var_and_clauses cnf_exp in
if sat_exists then
let w = (sat_to_assignment (λ _. F) (v2list sat)) in
let w' =
(assignment_to_numVarAssignment_numBoolRange w
(SND numBoolRange)
(FST numBoolRange))
in if numVarAssignment_range_ok w' (SND numBoolRange) then
Append (assignment_to_output w (get_boolVar_list (FST numBoolRange))) (numVarAssignment_to_output w' (SND numBoolRange))
else List [strlit "Invalid numVarAssignment"]
else
Append
(List [strlit "p cnf "; num_to_str (max_var); strlit " "; num_to_str clauses; strlit "\n"])
(cnf_to_output cnf_exp)
End
Definition main_function_def:
main_function (s:mlstring) =
let v_exp = from_str (explode s) in
case v_exp of
| (Pair _ (Pair (Pair (Num 7561588) _) _)) =>
main_function2 (head v_exp) (tail (head(tail v_exp))) T
| _ => main_function2 v_exp (Num 0) F
End
val res = translate from_v_to_rangeList_inner_def;
val res = translate from_v_to_num_def;
val res = translate from_v_to_rangeList_def;
val res = translate from_v_to_numBoolRange_def;
val res = translate parse_to_numBoolRange_def;
val res = translate literal_to_output_def;
val res = translate clause_to_output_def;
val res = translate cnf_to_output_def;
val res = translate get_max_var_def;
val res = translate get_max_var_and_clauses_def;
val res = translate sat_to_assignment_def;
val res = translate bool_to_str_def;
val res = translate assignment_to_output_def;
val res = translate numVarAssignment_to_output_def;
val res = translate boolExp_to_constFree_def;
val res = translate get_fresh_name_constFree_def;
val res = translate main_function2_def;
val res = translate main_function_def;
val _ = type_of “main_function” = “:mlstring -> mlstring app_list”
orelse failwith "The main_function has the wrong type.";
val main = process_topdecs
`print_app_list (main_function (TextIO.inputAll TextIO.stdIn));`;
val prog =
get_ml_prog_state ()
|> ml_progLib.clean_state
|> ml_progLib.remove_snocs
|> ml_progLib.get_thm
|> REWRITE_RULE [ml_progTheory.ML_code_def]
|> concl |> rator |> rator |> rand
|> (fn tm => “^tm ++ ^main”)
|> EVAL |> concl |> rand
Definition numBoolRange_encoder_prog_def:
numBoolRange_encoder_prog = ^prog
End
val _ = export_theory();