author | berghofe |
Thu, 23 Sep 2004 10:20:52 +0200 | |
changeset 15204 | d15f85347fcb |
parent 15126 | 54ae6c6ef3b1 |
child 15257 | 19dcdea98649 |
permissions | -rw-r--r-- |
12453 | 1 |
(* Title: HOL/inductive_codegen.ML |
11537 | 2 |
ID: $Id$ |
11539 | 3 |
Author: Stefan Berghofer, TU Muenchen |
11537 | 4 |
|
11539 | 5 |
Code generator for inductive predicates. |
11537 | 6 |
*) |
7 |
||
8 |
signature INDUCTIVE_CODEGEN = |
|
9 |
sig |
|
12557 | 10 |
val add : theory attribute |
11537 | 11 |
val setup : (theory -> theory) list |
12 |
end; |
|
13 |
||
14 |
structure InductiveCodegen : INDUCTIVE_CODEGEN = |
|
15 |
struct |
|
16 |
||
17 |
open Codegen; |
|
18 |
||
12557 | 19 |
(**** theory data ****) |
20 |
||
21 |
structure CodegenArgs = |
|
22 |
struct |
|
23 |
val name = "HOL/inductive_codegen"; |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
24 |
type T = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
25 |
{intros : thm list Symtab.table, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
26 |
graph : unit Graph.T, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
27 |
eqns : thm list Symtab.table}; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
28 |
val empty = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
29 |
{intros = Symtab.empty, graph = Graph.empty, eqns = Symtab.empty}; |
12557 | 30 |
val copy = I; |
31 |
val prep_ext = I; |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
32 |
fun merge ({intros=intros1, graph=graph1, eqns=eqns1}, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
33 |
{intros=intros2, graph=graph2, eqns=eqns2}) = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
34 |
{intros = Symtab.merge_multi Drule.eq_thm_prop (intros1, intros2), |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
35 |
graph = Graph.merge (K true) (graph1, graph2), |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
36 |
eqns = Symtab.merge_multi Drule.eq_thm_prop (eqns1, eqns2)}; |
12557 | 37 |
fun print _ _ = (); |
38 |
end; |
|
39 |
||
40 |
structure CodegenData = TheoryDataFun(CodegenArgs); |
|
41 |
||
42 |
fun warn thm = warning ("InductiveCodegen: Not a proper clause:\n" ^ |
|
43 |
string_of_thm thm); |
|
11537 | 44 |
|
14162
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
45 |
fun add_node (g, x) = Graph.new_node (x, ()) g handle Graph.DUP _ => g; |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
46 |
|
12557 | 47 |
fun add (p as (thy, thm)) = |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
48 |
let val {intros, graph, eqns} = CodegenData.get thy; |
12557 | 49 |
in (case concl_of thm of |
50 |
_ $ (Const ("op :", _) $ _ $ t) => (case head_of t of |
|
14162
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
51 |
Const (s, _) => |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
52 |
let val cs = foldr add_term_consts (prems_of thm, []) |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
53 |
in (CodegenData.put |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
54 |
{intros = Symtab.update ((s, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
55 |
if_none (Symtab.lookup (intros, s)) [] @ [thm]), intros), |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
56 |
graph = foldr (uncurry (Graph.add_edge o pair s)) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
57 |
(cs, foldl add_node (graph, s :: cs)), |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
58 |
eqns = eqns} thy, thm) |
14162
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
59 |
end |
12557 | 60 |
| _ => (warn thm; p)) |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
61 |
| _ $ (Const ("op =", _) $ t $ _) => (case head_of t of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
62 |
Const (s, _) => |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
63 |
(CodegenData.put {intros = intros, graph = graph, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
64 |
eqns = Symtab.update ((s, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
65 |
if_none (Symtab.lookup (eqns, s)) [] @ [thm]), eqns)} thy, thm) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
66 |
| _ => (warn thm; p)) |
12557 | 67 |
| _ => (warn thm; p)) |
12562 | 68 |
end; |
12557 | 69 |
|
70 |
fun get_clauses thy s = |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
71 |
let val {intros, graph, ...} = CodegenData.get thy |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
72 |
in case Symtab.lookup (intros, s) of |
14162
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
73 |
None => (case InductivePackage.get_inductive thy s of |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
74 |
None => None |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
75 |
| Some ({names, ...}, {intrs, ...}) => Some (names, intrs)) |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
76 |
| Some _ => |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
77 |
let val Some names = find_first |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
78 |
(fn xs => s mem xs) (Graph.strong_conn graph) |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
79 |
in Some (names, |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
80 |
flat (map (fn s => the (Symtab.lookup (intros, s))) names)) |
14162
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
81 |
end |
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents:
13105
diff
changeset
|
82 |
end; |
12557 | 83 |
|
84 |
||
85 |
(**** improper tuples ****) |
|
11537 | 86 |
|
87 |
fun prod_factors p (Const ("Pair", _) $ t $ u) = |
|
88 |
p :: prod_factors (1::p) t @ prod_factors (2::p) u |
|
89 |
| prod_factors p _ = []; |
|
90 |
||
91 |
fun split_prod p ps t = if p mem ps then (case t of |
|
92 |
Const ("Pair", _) $ t $ u => |
|
93 |
split_prod (1::p) ps t @ split_prod (2::p) ps u |
|
94 |
| _ => error "Inconsistent use of products") else [t]; |
|
95 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
96 |
fun full_split_prod (Const ("Pair", _) $ t $ u) = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
97 |
full_split_prod t @ full_split_prod u |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
98 |
| full_split_prod t = [t]; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
99 |
|
12557 | 100 |
datatype factors = FVar of int list list | FFix of int list list; |
101 |
||
102 |
exception Factors; |
|
103 |
||
104 |
fun mg_factor (FVar f) (FVar f') = FVar (f inter f') |
|
105 |
| mg_factor (FVar f) (FFix f') = |
|
106 |
if f' subset f then FFix f' else raise Factors |
|
107 |
| mg_factor (FFix f) (FVar f') = |
|
108 |
if f subset f' then FFix f else raise Factors |
|
109 |
| mg_factor (FFix f) (FFix f') = |
|
110 |
if f subset f' andalso f' subset f then FFix f else raise Factors; |
|
111 |
||
112 |
fun dest_factors (FVar f) = f |
|
113 |
| dest_factors (FFix f) = f; |
|
114 |
||
115 |
fun infer_factors sg extra_fs (fs, (optf, t)) = |
|
116 |
let fun err s = error (s ^ "\n" ^ Sign.string_of_term sg t) |
|
117 |
in (case (optf, strip_comb t) of |
|
118 |
(Some f, (Const (name, _), args)) => |
|
119 |
(case assoc (extra_fs, name) of |
|
120 |
None => overwrite (fs, (name, if_none |
|
121 |
(apsome (mg_factor f) (assoc (fs, name))) f)) |
|
122 |
| Some (fs', f') => (mg_factor f (FFix f'); |
|
123 |
foldl (infer_factors sg extra_fs) |
|
124 |
(fs, map (apsome FFix) fs' ~~ args))) |
|
125 |
| (Some f, (Var ((name, _), _), [])) => |
|
126 |
overwrite (fs, (name, if_none |
|
127 |
(apsome (mg_factor f) (assoc (fs, name))) f)) |
|
128 |
| (None, _) => fs |
|
129 |
| _ => err "Illegal term") |
|
130 |
handle Factors => err "Product factor mismatch in" |
|
131 |
end; |
|
132 |
||
11537 | 133 |
fun string_of_factors p ps = if p mem ps then |
134 |
"(" ^ string_of_factors (1::p) ps ^ ", " ^ string_of_factors (2::p) ps ^ ")" |
|
135 |
else "_"; |
|
136 |
||
12557 | 137 |
|
11537 | 138 |
(**** check if a term contains only constructor functions ****) |
139 |
||
140 |
fun is_constrt thy = |
|
141 |
let |
|
142 |
val cnstrs = flat (flat (map |
|
143 |
(map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd) |
|
144 |
(Symtab.dest (DatatypePackage.get_datatypes thy)))); |
|
145 |
fun check t = (case strip_comb t of |
|
146 |
(Var _, []) => true |
|
147 |
| (Const (s, _), ts) => (case assoc (cnstrs, s) of |
|
148 |
None => false |
|
149 |
| Some i => length ts = i andalso forall check ts) |
|
150 |
| _ => false) |
|
151 |
in check end; |
|
152 |
||
153 |
(**** check if a type is an equality type (i.e. doesn't contain fun) ****) |
|
154 |
||
155 |
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts |
|
156 |
| is_eqT _ = true; |
|
157 |
||
158 |
(**** mode inference ****) |
|
159 |
||
15204
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
160 |
fun string_of_mode (iss, is) = space_implode " -> " (map |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
161 |
(fn None => "X" |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
162 |
| Some js => enclose "[" "]" (commas (map string_of_int js))) |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
163 |
(iss @ [Some is])); |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
164 |
|
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
165 |
fun print_modes modes = message ("Inferred modes:\n" ^ |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
166 |
space_implode "\n" (map (fn (s, ms) => s ^ ": " ^ commas (map |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
167 |
string_of_mode ms)) modes)); |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
168 |
|
11537 | 169 |
val term_vs = map (fst o fst o dest_Var) o term_vars; |
170 |
val terms_vs = distinct o flat o (map term_vs); |
|
171 |
||
172 |
(** collect all Vars in a term (with duplicates!) **) |
|
173 |
fun term_vTs t = map (apfst fst o dest_Var) |
|
174 |
(filter is_Var (foldl_aterms (op :: o Library.swap) ([], t))); |
|
175 |
||
176 |
fun get_args _ _ [] = ([], []) |
|
177 |
| get_args is i (x::xs) = (if i mem is then apfst else apsnd) (cons x) |
|
178 |
(get_args is (i+1) xs); |
|
179 |
||
180 |
fun merge xs [] = xs |
|
181 |
| merge [] ys = ys |
|
182 |
| merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys) |
|
183 |
else y::merge (x::xs) ys; |
|
184 |
||
185 |
fun subsets i j = if i <= j then |
|
186 |
let val is = subsets (i+1) j |
|
187 |
in merge (map (fn ks => i::ks) is) is end |
|
188 |
else [[]]; |
|
189 |
||
12557 | 190 |
fun cprod ([], ys) = [] |
191 |
| cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys); |
|
192 |
||
193 |
fun cprods xss = foldr (map op :: o cprod) (xss, [[]]); |
|
194 |
||
195 |
datatype mode = Mode of (int list option list * int list) * mode option list; |
|
196 |
||
197 |
fun modes_of modes t = |
|
198 |
let |
|
199 |
fun mk_modes name args = flat |
|
200 |
(map (fn (m as (iss, is)) => map (Mode o pair m) (cprods (map |
|
201 |
(fn (None, _) => [None] |
|
202 |
| (Some js, arg) => map Some |
|
203 |
(filter (fn Mode ((_, js'), _) => js=js') (modes_of modes arg))) |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
204 |
(iss ~~ args)))) (the (assoc (modes, name)))) |
12557 | 205 |
|
206 |
in (case strip_comb t of |
|
14163
5ffa75ce4919
Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents:
14162
diff
changeset
|
207 |
(Const ("op =", Type (_, [T, _])), _) => |
5ffa75ce4919
Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents:
14162
diff
changeset
|
208 |
[Mode (([], [1]), []), Mode (([], [2]), [])] @ |
5ffa75ce4919
Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents:
14162
diff
changeset
|
209 |
(if is_eqT T then [Mode (([], [1, 2]), [])] else []) |
5ffa75ce4919
Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents:
14162
diff
changeset
|
210 |
| (Const (name, _), args) => mk_modes name args |
13038 | 211 |
| (Var ((name, _), _), args) => mk_modes name args |
212 |
| (Free (name, _), args) => mk_modes name args) |
|
12557 | 213 |
end; |
214 |
||
215 |
datatype indprem = Prem of term list * term | Sidecond of term; |
|
216 |
||
11537 | 217 |
fun select_mode_prem thy modes vs ps = |
218 |
find_first (is_some o snd) (ps ~~ map |
|
12557 | 219 |
(fn Prem (us, t) => find_first (fn Mode ((_, is), _) => |
11537 | 220 |
let |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
221 |
val (in_ts, out_ts) = get_args is 1 us; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
222 |
val (out_ts', in_ts') = partition (is_constrt thy) out_ts; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
223 |
val vTs = flat (map term_vTs out_ts'); |
11537 | 224 |
val dupTs = map snd (duplicates vTs) @ |
225 |
mapfilter (curry assoc vTs) vs; |
|
226 |
in |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
227 |
terms_vs (in_ts @ in_ts') subset vs andalso |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
228 |
forall (is_eqT o fastype_of) in_ts' andalso |
12557 | 229 |
term_vs t subset vs andalso |
11537 | 230 |
forall is_eqT dupTs |
231 |
end) |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
232 |
(modes_of modes t handle OPTION => [Mode (([], []), [])]) |
12557 | 233 |
| Sidecond t => if term_vs t subset vs then Some (Mode (([], []), [])) |
234 |
else None) ps); |
|
11537 | 235 |
|
12557 | 236 |
fun check_mode_clause thy arg_vs modes (iss, is) (ts, ps) = |
11537 | 237 |
let |
12557 | 238 |
val modes' = modes @ mapfilter |
239 |
(fn (_, None) => None | (v, Some js) => Some (v, [([], js)])) |
|
240 |
(arg_vs ~~ iss); |
|
11537 | 241 |
fun check_mode_prems vs [] = Some vs |
12557 | 242 |
| check_mode_prems vs ps = (case select_mode_prem thy modes' vs ps of |
11537 | 243 |
None => None |
244 |
| Some (x, _) => check_mode_prems |
|
12557 | 245 |
(case x of Prem (us, _) => vs union terms_vs us | _ => vs) |
11537 | 246 |
(filter_out (equal x) ps)); |
15204
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
247 |
val (in_ts, in_ts') = partition (is_constrt thy) (fst (get_args is 1 ts)); |
11537 | 248 |
val in_vs = terms_vs in_ts; |
249 |
val concl_vs = terms_vs ts |
|
250 |
in |
|
14560 | 251 |
forall is_eqT (map snd (duplicates (flat (map term_vTs in_ts)))) andalso |
15204
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
252 |
forall (is_eqT o fastype_of) in_ts' andalso |
11537 | 253 |
(case check_mode_prems (arg_vs union in_vs) ps of |
254 |
None => false |
|
255 |
| Some vs => concl_vs subset vs) |
|
256 |
end; |
|
257 |
||
258 |
fun check_modes_pred thy arg_vs preds modes (p, ms) = |
|
259 |
let val Some rs = assoc (preds, p) |
|
15204
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
260 |
in (p, filter (fn m => case find_index |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
261 |
(not o check_mode_clause thy arg_vs modes m) rs of |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
262 |
~1 => true |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
263 |
| i => (message ("Clause " ^ string_of_int (i+1) ^ " of " ^ |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
264 |
p ^ " violates mode " ^ string_of_mode m); false)) ms) |
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
berghofe
parents:
15126
diff
changeset
|
265 |
end; |
11537 | 266 |
|
267 |
fun fixp f x = |
|
268 |
let val y = f x |
|
269 |
in if x = y then x else fixp f y end; |
|
270 |
||
12557 | 271 |
fun infer_modes thy extra_modes factors arg_vs preds = fixp (fn modes => |
11537 | 272 |
map (check_modes_pred thy arg_vs preds (modes @ extra_modes)) modes) |
12557 | 273 |
(map (fn (s, (fs, f)) => (s, cprod (cprods (map |
274 |
(fn None => [None] |
|
275 |
| Some f' => map Some (subsets 1 (length f' + 1))) fs), |
|
276 |
subsets 1 (length f + 1)))) factors); |
|
11537 | 277 |
|
278 |
(**** code generation ****) |
|
279 |
||
280 |
fun mk_eq (x::xs) = |
|
281 |
let fun mk_eqs _ [] = [] |
|
282 |
| mk_eqs a (b::cs) = Pretty.str (a ^ " = " ^ b) :: mk_eqs b cs |
|
283 |
in mk_eqs x xs end; |
|
284 |
||
285 |
fun mk_tuple xs = Pretty.block (Pretty.str "(" :: |
|
286 |
flat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @ |
|
287 |
[Pretty.str ")"]); |
|
288 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
289 |
(* convert nested pairs to n-tuple *) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
290 |
|
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
291 |
fun conv_ntuple [_] t ps = ps |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
292 |
| conv_ntuple [_, _] t ps = ps |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
293 |
| conv_ntuple us t ps = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
294 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
295 |
val xs = map (fn i => Pretty.str ("x" ^ string_of_int i)) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
296 |
(1 upto length us); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
297 |
fun ntuple (ys as (x, T) :: xs) U = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
298 |
if T = U then (x, xs) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
299 |
else |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
300 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
301 |
val Type ("*", [U1, U2]) = U; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
302 |
val (p1, ys1) = ntuple ys U1; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
303 |
val (p2, ys2) = ntuple ys1 U2 |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
304 |
in (mk_tuple [p1, p2], ys2) end |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
305 |
in |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
306 |
[Pretty.str "Seq.map (fn", Pretty.brk 1, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
307 |
fst (ntuple (xs ~~ map fastype_of us) (HOLogic.dest_setT (fastype_of t))), |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
308 |
Pretty.str " =>", Pretty.brk 1, mk_tuple xs, Pretty.str ")", |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
309 |
Pretty.brk 1, parens (Pretty.block ps)] |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
310 |
end; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
311 |
|
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
312 |
(* convert n-tuple to nested pairs *) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
313 |
|
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
314 |
fun conv_ntuple' fs T ps = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
315 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
316 |
fun mk_x i = Pretty.str ("x" ^ string_of_int i); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
317 |
fun conv i js (Type ("*", [T, U])) = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
318 |
if js mem fs then |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
319 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
320 |
val (p, i') = conv i (1::js) T; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
321 |
val (q, i'') = conv i' (2::js) U |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
322 |
in (mk_tuple [p, q], i'') end |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
323 |
else (mk_x i, i+1) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
324 |
| conv i js _ = (mk_x i, i+1) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
325 |
val (p, i) = conv 1 [] T |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
326 |
in |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
327 |
if i > 3 then |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
328 |
[Pretty.str "Seq.map (fn", Pretty.brk 1, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
329 |
mk_tuple (map mk_x (1 upto i-1)), Pretty.str " =>", Pretty.brk 1, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
330 |
p, Pretty.str ")", Pretty.brk 1, parens (Pretty.block ps)] |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
331 |
else ps |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
332 |
end; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
333 |
|
11537 | 334 |
fun mk_v ((names, vs), s) = (case assoc (vs, s) of |
335 |
None => ((names, (s, [s])::vs), s) |
|
336 |
| Some xs => let val s' = variant names s in |
|
337 |
((s'::names, overwrite (vs, (s, s'::xs))), s') end); |
|
338 |
||
339 |
fun distinct_v (nvs, Var ((s, 0), T)) = |
|
340 |
apsnd (Var o rpair T o rpair 0) (mk_v (nvs, s)) |
|
341 |
| distinct_v (nvs, t $ u) = |
|
342 |
let |
|
343 |
val (nvs', t') = distinct_v (nvs, t); |
|
344 |
val (nvs'', u') = distinct_v (nvs', u); |
|
345 |
in (nvs'', t' $ u') end |
|
346 |
| distinct_v x = x; |
|
347 |
||
15061
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
348 |
fun is_exhaustive (Var _) = true |
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
349 |
| is_exhaustive (Const ("Pair", _) $ t $ u) = |
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
350 |
is_exhaustive t andalso is_exhaustive u |
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
351 |
| is_exhaustive _ = false; |
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
352 |
|
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
353 |
fun compile_match nvs eq_ps out_ps success_p can_fail = |
11537 | 354 |
let val eqs = flat (separate [Pretty.str " andalso", Pretty.brk 1] |
355 |
(map single (flat (map (mk_eq o snd) nvs) @ eq_ps))); |
|
356 |
in |
|
357 |
Pretty.block |
|
358 |
([Pretty.str "(fn ", mk_tuple out_ps, Pretty.str " =>", Pretty.brk 1] @ |
|
359 |
(Pretty.block ((if eqs=[] then [] else Pretty.str "if " :: |
|
360 |
[Pretty.block eqs, Pretty.brk 1, Pretty.str "then "]) @ |
|
361 |
(success_p :: |
|
15061
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
362 |
(if eqs=[] then [] else [Pretty.brk 1, Pretty.str "else Seq.empty"]))) :: |
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
363 |
(if can_fail then |
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
364 |
[Pretty.brk 1, Pretty.str "| _ => Seq.empty)"] |
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
365 |
else [Pretty.str ")"]))) |
11537 | 366 |
end; |
367 |
||
12557 | 368 |
fun modename thy s (iss, is) = space_implode "__" |
369 |
(mk_const_id (sign_of thy) s :: |
|
370 |
map (space_implode "_" o map string_of_int) (mapfilter I iss @ [is])); |
|
11537 | 371 |
|
12557 | 372 |
fun compile_expr thy dep brack (gr, (None, t)) = |
373 |
apsnd single (invoke_codegen thy dep brack (gr, t)) |
|
374 |
| compile_expr _ _ _ (gr, (Some _, Var ((name, _), _))) = |
|
375 |
(gr, [Pretty.str name]) |
|
376 |
| compile_expr thy dep brack (gr, (Some (Mode (mode, ms)), t)) = |
|
377 |
let |
|
378 |
val (Const (name, _), args) = strip_comb t; |
|
379 |
val (gr', ps) = foldl_map |
|
380 |
(compile_expr thy dep true) (gr, ms ~~ args); |
|
381 |
in (gr', (if brack andalso not (null ps) then |
|
382 |
single o parens o Pretty.block else I) |
|
383 |
(flat (separate [Pretty.brk 1] |
|
384 |
([Pretty.str (modename thy name mode)] :: ps)))) |
|
385 |
end; |
|
386 |
||
387 |
fun compile_clause thy gr dep all_vs arg_vs modes (iss, is) (ts, ps) = |
|
11537 | 388 |
let |
12557 | 389 |
val modes' = modes @ mapfilter |
390 |
(fn (_, None) => None | (v, Some js) => Some (v, [([], js)])) |
|
391 |
(arg_vs ~~ iss); |
|
392 |
||
11537 | 393 |
fun check_constrt ((names, eqs), t) = |
394 |
if is_constrt thy t then ((names, eqs), t) else |
|
395 |
let val s = variant names "x"; |
|
396 |
in ((s::names, (s, t)::eqs), Var ((s, 0), fastype_of t)) end; |
|
397 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
398 |
fun compile_eq (gr, (s, t)) = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
399 |
apsnd (Pretty.block o cons (Pretty.str (s ^ " = ")) o single) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
400 |
(invoke_codegen thy dep false (gr, t)); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
401 |
|
12557 | 402 |
val (in_ts, out_ts) = get_args is 1 ts; |
11537 | 403 |
val ((all_vs', eqs), in_ts') = |
404 |
foldl_map check_constrt ((all_vs, []), in_ts); |
|
405 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
406 |
fun is_ind t = (case head_of t of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
407 |
Const (s, _) => s = "op =" orelse is_some (assoc (modes, s)) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
408 |
| Var ((s, _), _) => s mem arg_vs); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
409 |
|
11537 | 410 |
fun compile_prems out_ts' vs names gr [] = |
411 |
let |
|
12453 | 412 |
val (gr2, out_ps) = foldl_map |
413 |
(invoke_codegen thy dep false) (gr, out_ts); |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
414 |
val (gr3, eq_ps) = foldl_map compile_eq (gr2, eqs); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
415 |
val ((names', eqs'), out_ts'') = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
416 |
foldl_map check_constrt ((names, []), out_ts'); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
417 |
val (nvs, out_ts''') = foldl_map distinct_v |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
418 |
((names', map (fn x => (x, [x])) vs), out_ts''); |
12453 | 419 |
val (gr4, out_ps') = foldl_map |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
420 |
(invoke_codegen thy dep false) (gr3, out_ts'''); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
421 |
val (gr5, eq_ps') = foldl_map compile_eq (gr4, eqs') |
11537 | 422 |
in |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
423 |
(gr5, compile_match (snd nvs) (eq_ps @ eq_ps') out_ps' |
11537 | 424 |
(Pretty.block [Pretty.str "Seq.single", Pretty.brk 1, mk_tuple out_ps]) |
15061
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
425 |
(exists (not o is_exhaustive) out_ts''')) |
11537 | 426 |
end |
427 |
| compile_prems out_ts vs names gr ps = |
|
428 |
let |
|
429 |
val vs' = distinct (flat (vs :: map term_vs out_ts)); |
|
12557 | 430 |
val Some (p, mode as Some (Mode ((_, js), _))) = |
15126
54ae6c6ef3b1
Fixed bug in compile_clause that caused equality constraints
berghofe
parents:
15061
diff
changeset
|
431 |
select_mode_prem thy modes' vs' ps; |
11537 | 432 |
val ps' = filter_out (equal p) ps; |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
433 |
val ((names', eqs), out_ts') = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
434 |
foldl_map check_constrt ((names, []), out_ts); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
435 |
val (nvs, out_ts'') = foldl_map distinct_v |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
436 |
((names', map (fn x => (x, [x])) vs), out_ts'); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
437 |
val (gr0, out_ps) = foldl_map |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
438 |
(invoke_codegen thy dep false) (gr, out_ts''); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
439 |
val (gr1, eq_ps) = foldl_map compile_eq (gr0, eqs) |
11537 | 440 |
in |
441 |
(case p of |
|
12557 | 442 |
Prem (us, t) => |
11537 | 443 |
let |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
444 |
val (in_ts, out_ts''') = get_args js 1 us; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
445 |
val (gr2, in_ps) = foldl_map |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
446 |
(invoke_codegen thy dep false) (gr1, in_ts); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
447 |
val (gr3, ps) = if is_ind t then |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
448 |
apsnd (fn ps => ps @ [Pretty.brk 1, mk_tuple in_ps]) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
449 |
(compile_expr thy dep false (gr2, (mode, t))) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
450 |
else |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
451 |
apsnd (fn p => conv_ntuple us t |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
452 |
[Pretty.str "Seq.of_list", Pretty.brk 1, p]) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
453 |
(invoke_codegen thy dep true (gr2, t)); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
454 |
val (gr4, rest) = compile_prems out_ts''' vs' (fst nvs) gr3 ps'; |
11537 | 455 |
in |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
456 |
(gr4, compile_match (snd nvs) eq_ps out_ps |
12557 | 457 |
(Pretty.block (ps @ |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
458 |
[Pretty.str " :->", Pretty.brk 1, rest])) |
15061
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
459 |
(exists (not o is_exhaustive) out_ts'')) |
11537 | 460 |
end |
461 |
| Sidecond t => |
|
462 |
let |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
463 |
val (gr2, side_p) = invoke_codegen thy dep true (gr1, t); |
11537 | 464 |
val (gr3, rest) = compile_prems [] vs' (fst nvs) gr2 ps'; |
465 |
in |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
466 |
(gr3, compile_match (snd nvs) eq_ps out_ps |
11537 | 467 |
(Pretty.block [Pretty.str "?? ", side_p, |
468 |
Pretty.str " :->", Pretty.brk 1, rest]) |
|
15061
61a52739cd82
Added simple check that allows code generator to produce code containing
berghofe
parents:
15031
diff
changeset
|
469 |
(exists (not o is_exhaustive) out_ts'')) |
11537 | 470 |
end) |
471 |
end; |
|
472 |
||
15126
54ae6c6ef3b1
Fixed bug in compile_clause that caused equality constraints
berghofe
parents:
15061
diff
changeset
|
473 |
val (gr', prem_p) = compile_prems in_ts' arg_vs all_vs' gr ps; |
11537 | 474 |
in |
475 |
(gr', Pretty.block [Pretty.str "Seq.single inp :->", Pretty.brk 1, prem_p]) |
|
476 |
end; |
|
477 |
||
478 |
fun compile_pred thy gr dep prfx all_vs arg_vs modes s cls mode = |
|
479 |
let val (gr', cl_ps) = foldl_map (fn (gr, cl) => |
|
480 |
compile_clause thy gr dep all_vs arg_vs modes mode cl) (gr, cls) |
|
481 |
in |
|
482 |
((gr', "and "), Pretty.block |
|
483 |
([Pretty.block (separate (Pretty.brk 1) |
|
484 |
(Pretty.str (prfx ^ modename thy s mode) :: map Pretty.str arg_vs) @ |
|
485 |
[Pretty.str " inp ="]), |
|
486 |
Pretty.brk 1] @ |
|
487 |
flat (separate [Pretty.str " ++", Pretty.brk 1] (map single cl_ps)))) |
|
488 |
end; |
|
489 |
||
490 |
fun compile_preds thy gr dep all_vs arg_vs modes preds = |
|
491 |
let val ((gr', _), prs) = foldl_map (fn ((gr, prfx), (s, cls)) => |
|
492 |
foldl_map (fn ((gr', prfx'), mode) => |
|
493 |
compile_pred thy gr' dep prfx' all_vs arg_vs modes s cls mode) |
|
494 |
((gr, prfx), the (assoc (modes, s)))) ((gr, "fun "), preds) |
|
495 |
in |
|
496 |
(gr', space_implode "\n\n" (map Pretty.string_of (flat prs)) ^ ";\n\n") |
|
497 |
end; |
|
498 |
||
499 |
(**** processing of introduction rules ****) |
|
500 |
||
12557 | 501 |
exception Modes of |
502 |
(string * (int list option list * int list) list) list * |
|
503 |
(string * (int list list option list * int list list)) list; |
|
504 |
||
505 |
fun lookup_modes gr dep = apfst flat (apsnd flat (ListPair.unzip |
|
506 |
(map ((fn (Some (Modes x), _) => x | _ => ([], [])) o Graph.get_node gr) |
|
507 |
(Graph.all_preds gr [dep])))); |
|
508 |
||
11537 | 509 |
fun print_factors factors = message ("Factors:\n" ^ |
12557 | 510 |
space_implode "\n" (map (fn (s, (fs, f)) => s ^ ": " ^ |
511 |
space_implode " -> " (map |
|
512 |
(fn None => "X" | Some f' => string_of_factors [] f') |
|
513 |
(fs @ [Some f]))) factors)); |
|
11537 | 514 |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
515 |
fun prep_intrs intrs = map (rename_term o #prop o rep_thm o standard) intrs; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
516 |
|
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
517 |
fun constrain cs [] = [] |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
518 |
| constrain cs ((s, xs) :: ys) = (s, case assoc (cs, s) of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
519 |
None => xs |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
520 |
| Some xs' => xs inter xs') :: constrain cs ys; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
521 |
|
12557 | 522 |
fun mk_extra_defs thy gr dep names ts = |
523 |
foldl (fn (gr, name) => |
|
524 |
if name mem names then gr |
|
525 |
else (case get_clauses thy name of |
|
526 |
None => gr |
|
527 |
| Some (names, intrs) => |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
528 |
mk_ind_def thy gr dep names [] [] (prep_intrs intrs))) |
12557 | 529 |
(gr, foldr add_term_consts (ts, [])) |
530 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
531 |
and mk_ind_def thy gr dep names modecs factorcs intrs = |
11537 | 532 |
let val ids = map (mk_const_id (sign_of thy)) names |
533 |
in Graph.add_edge (hd ids, dep) gr handle Graph.UNDEF _ => |
|
534 |
let |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
535 |
val _ $ (_ $ _ $ u) = Logic.strip_imp_concl (hd intrs); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
536 |
val (_, args) = strip_comb u; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
537 |
val arg_vs = flat (map term_vs args); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
538 |
|
14250
d09e92e7c2bf
Inserted additional checks in functions dest_prem and add_prod_factors, to
berghofe
parents:
14195
diff
changeset
|
539 |
fun dest_prem factors (_ $ (p as (Const ("op :", _) $ t $ u))) = |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
540 |
(case assoc (factors, case head_of u of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
541 |
Const (name, _) => name | Var ((name, _), _) => name) of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
542 |
None => Prem (full_split_prod t, u) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
543 |
| Some f => Prem (split_prod [] f t, u)) |
12557 | 544 |
| dest_prem factors (_ $ ((eq as Const ("op =", _)) $ t $ u)) = |
545 |
Prem ([t, u], eq) |
|
546 |
| dest_prem factors (_ $ t) = Sidecond t; |
|
11537 | 547 |
|
12557 | 548 |
fun add_clause factors (clauses, intr) = |
11537 | 549 |
let |
550 |
val _ $ (_ $ t $ u) = Logic.strip_imp_concl intr; |
|
12557 | 551 |
val Const (name, _) = head_of u; |
552 |
val prems = map (dest_prem factors) (Logic.strip_imp_prems intr); |
|
11537 | 553 |
in |
554 |
(overwrite (clauses, (name, if_none (assoc (clauses, name)) [] @ |
|
12557 | 555 |
[(split_prod [] (the (assoc (factors, name))) t, prems)]))) |
11537 | 556 |
end; |
557 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
558 |
fun check_set (Const (s, _)) = s mem names orelse is_some (get_clauses thy s) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
559 |
| check_set (Var ((s, _), _)) = s mem arg_vs |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
560 |
| check_set _ = false; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
561 |
|
12557 | 562 |
fun add_prod_factors extra_fs (fs, _ $ (Const ("op :", _) $ t $ u)) = |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
563 |
if check_set (head_of u) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
564 |
then infer_factors (sign_of thy) extra_fs |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
565 |
(fs, (Some (FVar (prod_factors [] t)), u)) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
566 |
else fs |
12557 | 567 |
| add_prod_factors _ (fs, _) = fs; |
11537 | 568 |
|
12557 | 569 |
val gr' = mk_extra_defs thy |
570 |
(Graph.add_edge (hd ids, dep) |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
571 |
(Graph.new_node (hd ids, (None, "")) gr)) (hd ids) names intrs; |
14163
5ffa75ce4919
Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents:
14162
diff
changeset
|
572 |
val (extra_modes, extra_factors) = lookup_modes gr' (hd ids); |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
573 |
val fs = constrain factorcs (map (apsnd dest_factors) |
12557 | 574 |
(foldl (add_prod_factors extra_factors) ([], flat (map (fn t => |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
575 |
Logic.strip_imp_concl t :: Logic.strip_imp_prems t) intrs)))); |
12557 | 576 |
val factors = mapfilter (fn (name, f) => |
577 |
if name mem arg_vs then None |
|
578 |
else Some (name, (map (curry assoc fs) arg_vs, f))) fs; |
|
579 |
val clauses = |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
580 |
foldl (add_clause (fs @ map (apsnd snd) extra_factors)) ([], intrs); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
581 |
val modes = constrain modecs |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
582 |
(infer_modes thy extra_modes factors arg_vs clauses); |
12557 | 583 |
val _ = print_factors factors; |
11537 | 584 |
val _ = print_modes modes; |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
585 |
val (gr'', s) = compile_preds thy gr' (hd ids) (terms_vs intrs) arg_vs |
11537 | 586 |
(modes @ extra_modes) clauses; |
587 |
in |
|
588 |
(Graph.map_node (hd ids) (K (Some (Modes (modes, factors)), s)) gr'') |
|
589 |
end |
|
590 |
end; |
|
591 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
592 |
fun find_mode s u modes is = (case find_first (fn Mode ((_, js), _) => is=js) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
593 |
(modes_of modes u handle OPTION => []) of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
594 |
None => error ("No such mode for " ^ s ^ ": " ^ string_of_mode ([], is)) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
595 |
| mode => mode); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
596 |
|
12557 | 597 |
fun mk_ind_call thy gr dep t u is_query = (case head_of u of |
13038 | 598 |
Const (s, T) => (case (get_clauses thy s, get_assoc_code thy s T) of |
599 |
(None, _) => None |
|
600 |
| (Some (names, intrs), None) => |
|
11537 | 601 |
let |
12565 | 602 |
fun mk_mode (((ts, mode), i), Const ("dummy_pattern", _)) = |
603 |
((ts, mode), i+1) |
|
11537 | 604 |
| mk_mode (((ts, mode), i), t) = ((ts @ [t], mode @ [i]), i+1); |
605 |
||
12557 | 606 |
val gr1 = mk_extra_defs thy |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
607 |
(mk_ind_def thy gr dep names [] [] (prep_intrs intrs)) dep names [u]; |
12557 | 608 |
val (modes, factors) = lookup_modes gr1 dep; |
609 |
val ts = split_prod [] (snd (the (assoc (factors, s)))) t; |
|
610 |
val (ts', is) = if is_query then |
|
11537 | 611 |
fst (foldl mk_mode ((([], []), 1), ts)) |
612 |
else (ts, 1 upto length ts); |
|
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
613 |
val mode = find_mode s u modes is; |
12453 | 614 |
val (gr2, in_ps) = foldl_map |
615 |
(invoke_codegen thy dep false) (gr1, ts'); |
|
12557 | 616 |
val (gr3, ps) = compile_expr thy dep false (gr2, (mode, u)) |
11537 | 617 |
in |
12557 | 618 |
Some (gr3, Pretty.block |
619 |
(ps @ [Pretty.brk 1, mk_tuple in_ps])) |
|
13038 | 620 |
end |
621 |
| _ => None) |
|
11537 | 622 |
| _ => None); |
623 |
||
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
624 |
fun list_of_indset thy gr dep brack u = (case head_of u of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
625 |
Const (s, T) => (case (get_clauses thy s, get_assoc_code thy s T) of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
626 |
(None, _) => None |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
627 |
| (Some (names, intrs), None) => |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
628 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
629 |
val gr1 = mk_extra_defs thy |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
630 |
(mk_ind_def thy gr dep names [] [] (prep_intrs intrs)) dep names [u]; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
631 |
val (modes, factors) = lookup_modes gr1 dep; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
632 |
val mode = find_mode s u modes []; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
633 |
val (gr2, ps) = compile_expr thy dep false (gr1, (mode, u)) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
634 |
in |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
635 |
Some (gr2, (if brack then parens else I) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
636 |
(Pretty.block ([Pretty.str "Seq.list_of", Pretty.brk 1, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
637 |
Pretty.str "("] @ |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
638 |
conv_ntuple' (snd (the (assoc (factors, s)))) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
639 |
(HOLogic.dest_setT (fastype_of u)) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
640 |
(ps @ [Pretty.brk 1, Pretty.str "()"]) @ |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
641 |
[Pretty.str ")"]))) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
642 |
end |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
643 |
| _ => None) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
644 |
| _ => None); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
645 |
|
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
646 |
fun clause_of_eqn eqn = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
647 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
648 |
val (t, u) = HOLogic.dest_eq (HOLogic.dest_Trueprop (concl_of eqn)); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
649 |
val (Const (s, T), ts) = strip_comb t; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
650 |
val (Ts, U) = strip_type T |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
651 |
in |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
652 |
rename_term |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
653 |
(Logic.list_implies (prems_of eqn, HOLogic.mk_Trueprop (HOLogic.mk_mem |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
654 |
(foldr1 HOLogic.mk_prod (ts @ [u]), Const (Sign.base_name s ^ "_aux", |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
655 |
HOLogic.mk_setT (foldr1 HOLogic.mk_prodT (Ts @ [U]))))))) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
656 |
end; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
657 |
|
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
658 |
fun mk_fun thy name eqns dep gr = |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
659 |
let val id = mk_const_id (sign_of thy) name |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
660 |
in Graph.add_edge (id, dep) gr handle Graph.UNDEF _ => |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
661 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
662 |
val clauses = map clause_of_eqn eqns; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
663 |
val pname = mk_const_id (sign_of thy) (Sign.base_name name ^ "_aux"); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
664 |
val arity = length (snd (strip_comb (fst (HOLogic.dest_eq |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
665 |
(HOLogic.dest_Trueprop (concl_of (hd eqns))))))); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
666 |
val mode = 1 upto arity; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
667 |
val vars = map (fn i => Pretty.str ("x" ^ string_of_int i)) mode; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
668 |
val s = Pretty.string_of (Pretty.block |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
669 |
[mk_app false (Pretty.str ("fun " ^ id)) vars, Pretty.str " =", |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
670 |
Pretty.brk 1, Pretty.str "Seq.hd", Pretty.brk 1, |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
671 |
parens (Pretty.block [Pretty.str (modename thy pname ([], mode)), |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
672 |
Pretty.brk 1, mk_tuple vars])]) ^ ";\n\n"; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
673 |
val gr' = mk_ind_def thy (Graph.add_edge (id, dep) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
674 |
(Graph.new_node (id, (None, s)) gr)) id [pname] |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
675 |
[(pname, [([], mode)])] |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
676 |
[(pname, map (fn i => replicate i 2) (0 upto arity-1))] |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
677 |
clauses; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
678 |
val (modes, _) = lookup_modes gr' dep; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
679 |
val _ = find_mode pname (snd (HOLogic.dest_mem (HOLogic.dest_Trueprop |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
680 |
(Logic.strip_imp_concl (hd clauses))))) modes mode |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
681 |
in gr' end |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
682 |
end; |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
683 |
|
11537 | 684 |
fun inductive_codegen thy gr dep brack (Const ("op :", _) $ t $ u) = |
12565 | 685 |
((case mk_ind_call thy gr dep (Term.no_dummy_patterns t) u false of |
11537 | 686 |
None => None |
687 |
| Some (gr', call_p) => Some (gr', (if brack then parens else I) |
|
12453 | 688 |
(Pretty.block [Pretty.str "?! (", call_p, Pretty.str ")"]))) |
12565 | 689 |
handle TERM _ => mk_ind_call thy gr dep t u true) |
15031
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
690 |
| inductive_codegen thy gr dep brack t = (case strip_comb t of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
691 |
(Const (s, _), ts) => (case Symtab.lookup (#eqns (CodegenData.get thy), s) of |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
692 |
None => list_of_indset thy gr dep brack t |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
693 |
| Some eqns => |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
694 |
let |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
695 |
val gr' = mk_fun thy s eqns dep gr |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
696 |
val (gr'', ps) = foldl_map (invoke_codegen thy dep true) (gr', ts); |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
697 |
in Some (gr'', mk_app brack (Pretty.str (mk_const_id |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
698 |
(sign_of thy) s)) ps) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
699 |
end) |
726dc9146bb1
- Added support for conditional equations whose premises involve
berghofe
parents:
14981
diff
changeset
|
700 |
| _ => None); |
11537 | 701 |
|
12557 | 702 |
val setup = |
703 |
[add_codegen "inductive" inductive_codegen, |
|
704 |
CodegenData.init, |
|
14195 | 705 |
add_attribute "ind" (Scan.succeed add)]; |
11537 | 706 |
|
707 |
end; |
|
12453 | 708 |
|
709 |
||
710 |
(**** combinators for code generated from inductive predicates ****) |
|
711 |
||
712 |
infix 5 :->; |
|
713 |
infix 3 ++; |
|
714 |
||
715 |
fun s :-> f = Seq.flat (Seq.map f s); |
|
716 |
||
717 |
fun s1 ++ s2 = Seq.append (s1, s2); |
|
718 |
||
719 |
fun ?? b = if b then Seq.single () else Seq.empty; |
|
720 |
||
721 |
fun ?! s = is_some (Seq.pull s); |
|
722 |
||
14859 | 723 |
fun op_61__1 x = Seq.single x; |
12453 | 724 |
|
14859 | 725 |
val op_61__2 = op_61__1; |
12557 | 726 |
|
14859 | 727 |
fun op_61__1_2 (x, y) = ?? (x = y); |