| author | wenzelm | 
| Wed, 31 Dec 2008 00:01:51 +0100 | |
| changeset 29263 | bf99ccf71b7c | 
| parent 28537 | 1e84256d1a8a | 
| child 29265 | 5b4247055bd7 | 
| permissions | -rw-r--r-- | 
| 24584 | 1 | (* Title: HOL/Tools/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 | |
| 22271 | 10 | val add : string option -> int option -> attribute | 
| 18708 | 11 | val setup : theory -> theory | 
| 11537 | 12 | end; | 
| 13 | ||
| 14 | structure InductiveCodegen : INDUCTIVE_CODEGEN = | |
| 15 | struct | |
| 16 | ||
| 17 | open Codegen; | |
| 18 | ||
| 12557 | 19 | (**** theory data ****) | 
| 20 | ||
| 18930 
29d39c10822e
replaced Symtab.merge_multi by local merge_rules;
 wenzelm parents: 
18928diff
changeset | 21 | fun merge_rules tabs = | 
| 22642 | 22 | Symtab.join (fn _ => AList.merge (Thm.eq_thm_prop) (K true)) tabs; | 
| 18930 
29d39c10822e
replaced Symtab.merge_multi by local merge_rules;
 wenzelm parents: 
18928diff
changeset | 23 | |
| 16424 | 24 | structure CodegenData = TheoryDataFun | 
| 22846 | 25 | ( | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 26 | type T = | 
| 22271 | 27 |     {intros : (thm * (string * int)) list Symtab.table,
 | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 28 | graph : unit Graph.T, | 
| 16645 | 29 | eqns : (thm * string) list Symtab.table}; | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 30 | val empty = | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 31 |     {intros = Symtab.empty, graph = Graph.empty, eqns = Symtab.empty};
 | 
| 12557 | 32 | val copy = I; | 
| 16424 | 33 | val extend = I; | 
| 34 |   fun merge _ ({intros=intros1, graph=graph1, eqns=eqns1},
 | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 35 |     {intros=intros2, graph=graph2, eqns=eqns2}) =
 | 
| 18930 
29d39c10822e
replaced Symtab.merge_multi by local merge_rules;
 wenzelm parents: 
18928diff
changeset | 36 |     {intros = merge_rules (intros1, intros2),
 | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 37 | graph = Graph.merge (K true) (graph1, graph2), | 
| 18930 
29d39c10822e
replaced Symtab.merge_multi by local merge_rules;
 wenzelm parents: 
18928diff
changeset | 38 | eqns = merge_rules (eqns1, eqns2)}; | 
| 22846 | 39 | ); | 
| 12557 | 40 | |
| 41 | ||
| 42 | fun warn thm = warning ("InductiveCodegen: Not a proper clause:\n" ^
 | |
| 26928 | 43 | Display.string_of_thm thm); | 
| 11537 | 44 | |
| 14162 
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
 berghofe parents: 
13105diff
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: 
13105diff
changeset | 46 | |
| 22271 | 47 | fun add optmod optnparms = Thm.declaration_attribute (fn thm => Context.mapping (fn thy => | 
| 16645 | 48 | let | 
| 49 |     val {intros, graph, eqns} = CodegenData.get thy;
 | |
| 50 | fun thyname_of s = (case optmod of | |
| 27398 
768da1da59d6
simplified retrieval of theory names of consts and types
 haftmann parents: 
26975diff
changeset | 51 | NONE => Codegen.thyname_of_const thy s | SOME s => s); | 
| 22271 | 52 | in (case Option.map strip_comb (try HOLogic.dest_Trueprop (concl_of thm)) of | 
| 53 |       SOME (Const ("op =", _), [t, _]) => (case head_of t of
 | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 54 | Const (s, _) => | 
| 18728 | 55 |           CodegenData.put {intros = intros, graph = graph,
 | 
| 22642 | 56 | eqns = eqns |> Symtab.map_default (s, []) | 
| 57 | (AList.update Thm.eq_thm_prop (thm, thyname_of s))} thy | |
| 18728 | 58 | | _ => (warn thm; thy)) | 
| 22271 | 59 | | SOME (Const (s, _), _) => | 
| 60 | let | |
| 61 | val cs = foldr add_term_consts [] (prems_of thm); | |
| 62 | val rules = Symtab.lookup_list intros s; | |
| 63 | val nparms = (case optnparms of | |
| 64 | SOME k => k | |
| 65 | | NONE => (case rules of | |
| 66 | [] => (case try (InductivePackage.the_inductive (ProofContext.init thy)) s of | |
| 22791 
992222f3d2eb
Moved function params_of to inductive_package.ML.
 berghofe parents: 
22661diff
changeset | 67 |                  SOME (_, {raw_induct, ...}) =>
 | 
| 
992222f3d2eb
Moved function params_of to inductive_package.ML.
 berghofe parents: 
22661diff
changeset | 68 | length (InductivePackage.params_of raw_induct) | 
| 22271 | 69 | | NONE => 0) | 
| 70 | | xs => snd (snd (snd (split_last xs))))) | |
| 71 | in CodegenData.put | |
| 72 |           {intros = intros |>
 | |
| 22642 | 73 | Symtab.update (s, (AList.update Thm.eq_thm_prop | 
| 74 | (thm, (thyname_of s, nparms)) rules)), | |
| 22271 | 75 | graph = foldr (uncurry (Graph.add_edge o pair s)) | 
| 76 | (Library.foldl add_node (graph, s :: cs)) cs, | |
| 77 | eqns = eqns} thy | |
| 78 | end | |
| 18728 | 79 | | _ => (warn thm; thy)) | 
| 20897 | 80 | end) I); | 
| 12557 | 81 | |
| 82 | fun get_clauses thy s = | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 83 |   let val {intros, graph, ...} = CodegenData.get thy
 | 
| 17412 | 84 | in case Symtab.lookup intros s of | 
| 22271 | 85 | NONE => (case try (InductivePackage.the_inductive (ProofContext.init thy)) s of | 
| 15531 | 86 | NONE => NONE | 
| 22271 | 87 |       | SOME ({names, ...}, {intrs, raw_induct, ...}) =>
 | 
| 27398 
768da1da59d6
simplified retrieval of theory names of consts and types
 haftmann parents: 
26975diff
changeset | 88 | SOME (names, Codegen.thyname_of_const thy s, | 
| 22791 
992222f3d2eb
Moved function params_of to inductive_package.ML.
 berghofe parents: 
22661diff
changeset | 89 | length (InductivePackage.params_of raw_induct), | 
| 22661 
f3ba63a2663e
Removed erroneous application of rev in get_clauses that caused
 berghofe parents: 
22642diff
changeset | 90 | preprocess thy intrs)) | 
| 15531 | 91 | | SOME _ => | 
| 16645 | 92 | let | 
| 93 | val SOME names = find_first | |
| 22642 | 94 | (fn xs => member (op =) xs s) (Graph.strong_conn graph); | 
| 95 | val intrs as (_, (thyname, nparms)) :: _ = | |
| 96 | maps (the o Symtab.lookup intros) names; | |
| 97 | in SOME (names, thyname, nparms, preprocess thy (map fst (rev intrs))) end | |
| 14162 
f05f299512e9
Fixed problem with "code ind" attribute that caused code generator to
 berghofe parents: 
13105diff
changeset | 98 | end; | 
| 12557 | 99 | |
| 100 | ||
| 11537 | 101 | (**** check if a term contains only constructor functions ****) | 
| 102 | ||
| 103 | fun is_constrt thy = | |
| 104 | let | |
| 15570 | 105 | val cnstrs = List.concat (List.concat (map | 
| 11537 | 106 | (map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd) | 
| 107 | (Symtab.dest (DatatypePackage.get_datatypes thy)))); | |
| 108 | fun check t = (case strip_comb t of | |
| 109 | (Var _, []) => true | |
| 17521 | 110 | | (Const (s, _), ts) => (case AList.lookup (op =) cnstrs s of | 
| 15531 | 111 | NONE => false | 
| 112 | | SOME i => length ts = i andalso forall check ts) | |
| 11537 | 113 | | _ => false) | 
| 114 | in check end; | |
| 115 | ||
| 116 | (**** check if a type is an equality type (i.e. doesn't contain fun) ****) | |
| 117 | ||
| 118 | fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts | |
| 119 | | is_eqT _ = true; | |
| 120 | ||
| 121 | (**** mode inference ****) | |
| 122 | ||
| 15204 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 123 | fun string_of_mode (iss, is) = space_implode " -> " (map | 
| 15531 | 124 | (fn NONE => "X" | 
| 125 | | SOME js => enclose "[" "]" (commas (map string_of_int js))) | |
| 126 | (iss @ [SOME is])); | |
| 15204 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 127 | |
| 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 128 | fun print_modes modes = message ("Inferred modes:\n" ^
 | 
| 26931 | 129 | cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map | 
| 15204 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 130 | string_of_mode ms)) modes)); | 
| 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 131 | |
| 11537 | 132 | val term_vs = map (fst o fst o dest_Var) o term_vars; | 
| 19046 
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
 wenzelm parents: 
19025diff
changeset | 133 | val terms_vs = distinct (op =) o List.concat o (map term_vs); | 
| 11537 | 134 | |
| 135 | (** collect all Vars in a term (with duplicates!) **) | |
| 16861 | 136 | fun term_vTs tm = | 
| 137 | fold_aterms (fn Var ((x, _), T) => cons (x, T) | _ => I) tm []; | |
| 11537 | 138 | |
| 139 | fun get_args _ _ [] = ([], []) | |
| 140 | | get_args is i (x::xs) = (if i mem is then apfst else apsnd) (cons x) | |
| 141 | (get_args is (i+1) xs); | |
| 142 | ||
| 143 | fun merge xs [] = xs | |
| 144 | | merge [] ys = ys | |
| 145 | | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys) | |
| 146 | else y::merge (x::xs) ys; | |
| 147 | ||
| 148 | fun subsets i j = if i <= j then | |
| 149 | let val is = subsets (i+1) j | |
| 150 | in merge (map (fn ks => i::ks) is) is end | |
| 151 | else [[]]; | |
| 152 | ||
| 12557 | 153 | fun cprod ([], ys) = [] | 
| 154 | | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys); | |
| 155 | ||
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 156 | fun cprods xss = foldr (map op :: o cprod) [[]] xss; | 
| 12557 | 157 | |
| 22271 | 158 | datatype mode = Mode of (int list option list * int list) * int list * mode option list; | 
| 12557 | 159 | |
| 160 | fun modes_of modes t = | |
| 161 | let | |
| 22271 | 162 | val ks = 1 upto length (binder_types (fastype_of t)); | 
| 163 | val default = [Mode (([], ks), ks, [])]; | |
| 164 | fun mk_modes name args = Option.map (List.concat o | |
| 165 | map (fn (m as (iss, is)) => | |
| 166 | let | |
| 167 | val (args1, args2) = | |
| 168 | if length args < length iss then | |
| 169 |               error ("Too few arguments for inductive predicate " ^ name)
 | |
| 170 | else chop (length iss) args; | |
| 171 | val k = length args2; | |
| 172 | val prfx = 1 upto k | |
| 173 | in | |
| 174 | if not (is_prefix op = prfx is) then [] else | |
| 175 | let val is' = map (fn i => i - k) (List.drop (is, k)) | |
| 176 | in map (fn x => Mode (m, is', x)) (cprods (map | |
| 177 | (fn (NONE, _) => [NONE] | |
| 178 | | (SOME js, arg) => map SOME (List.filter | |
| 179 | (fn Mode (_, js', _) => js=js') (modes_of modes arg))) | |
| 180 | (iss ~~ args1))) | |
| 181 | end | |
| 182 | end)) (AList.lookup op = modes name) | |
| 12557 | 183 | |
| 184 | in (case strip_comb t of | |
| 14163 
5ffa75ce4919
Improved handling of modes for equality predicate, to avoid ill-typed
 berghofe parents: 
14162diff
changeset | 185 |       (Const ("op =", Type (_, [T, _])), _) =>
 | 
| 22271 | 186 | [Mode (([], [1]), [1], []), Mode (([], [2]), [2], [])] @ | 
| 187 | (if is_eqT T then [Mode (([], [1, 2]), [1, 2], [])] else []) | |
| 188 | | (Const (name, _), args) => the_default default (mk_modes name args) | |
| 189 | | (Var ((name, _), _), args) => the (mk_modes name args) | |
| 190 | | (Free (name, _), args) => the (mk_modes name args) | |
| 191 | | _ => default) | |
| 12557 | 192 | end; | 
| 193 | ||
| 26806 | 194 | datatype indprem = Prem of term list * term * bool | Sidecond of term; | 
| 12557 | 195 | |
| 11537 | 196 | fun select_mode_prem thy modes vs ps = | 
| 19861 | 197 | find_first (is_some o snd) (ps ~~ map | 
| 26806 | 198 | (fn Prem (us, t, is_set) => find_first (fn Mode (_, is, _) => | 
| 11537 | 199 | let | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 200 | val (in_ts, out_ts) = get_args is 1 us; | 
| 15570 | 201 | val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts; | 
| 202 | val vTs = List.concat (map term_vTs out_ts'); | |
| 18964 | 203 | val dupTs = map snd (duplicates (op =) vTs) @ | 
| 17521 | 204 | List.mapPartial (AList.lookup (op =) vTs) vs; | 
| 11537 | 205 | in | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 206 | terms_vs (in_ts @ in_ts') subset vs andalso | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 207 | forall (is_eqT o fastype_of) in_ts' andalso | 
| 12557 | 208 | term_vs t subset vs andalso | 
| 11537 | 209 | forall is_eqT dupTs | 
| 210 | end) | |
| 26806 | 211 | (if is_set then [Mode (([], []), [], [])] | 
| 212 | else modes_of modes t handle Option => | |
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26931diff
changeset | 213 |                error ("Bad predicate: " ^ Syntax.string_of_term_global thy t))
 | 
| 22271 | 214 | | Sidecond t => if term_vs t subset vs then SOME (Mode (([], []), [], [])) | 
| 15531 | 215 | else NONE) ps); | 
| 11537 | 216 | |
| 12557 | 217 | fun check_mode_clause thy arg_vs modes (iss, is) (ts, ps) = | 
| 11537 | 218 | let | 
| 15570 | 219 | val modes' = modes @ List.mapPartial | 
| 15531 | 220 | (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) | 
| 12557 | 221 | (arg_vs ~~ iss); | 
| 15531 | 222 | fun check_mode_prems vs [] = SOME vs | 
| 12557 | 223 | | check_mode_prems vs ps = (case select_mode_prem thy modes' vs ps of | 
| 15531 | 224 | NONE => NONE | 
| 225 | | SOME (x, _) => check_mode_prems | |
| 26806 | 226 | (case x of Prem (us, _, _) => vs union terms_vs us | _ => vs) | 
| 11537 | 227 | (filter_out (equal x) ps)); | 
| 15570 | 228 | val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (get_args is 1 ts)); | 
| 11537 | 229 | val in_vs = terms_vs in_ts; | 
| 230 | val concl_vs = terms_vs ts | |
| 231 | in | |
| 18964 | 232 | forall is_eqT (map snd (duplicates (op =) (List.concat (map term_vTs in_ts)))) andalso | 
| 15204 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 233 | forall (is_eqT o fastype_of) in_ts' andalso | 
| 11537 | 234 | (case check_mode_prems (arg_vs union in_vs) ps of | 
| 15531 | 235 | NONE => false | 
| 236 | | SOME vs => concl_vs subset vs) | |
| 11537 | 237 | end; | 
| 238 | ||
| 239 | fun check_modes_pred thy arg_vs preds modes (p, ms) = | |
| 17521 | 240 | let val SOME rs = AList.lookup (op =) preds p | 
| 15570 | 241 | in (p, List.filter (fn m => case find_index | 
| 15204 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 242 | (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: 
15126diff
changeset | 243 | ~1 => true | 
| 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 244 |     | i => (message ("Clause " ^ string_of_int (i+1) ^ " of " ^
 | 
| 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 245 | p ^ " violates mode " ^ string_of_mode m); false)) ms) | 
| 
d15f85347fcb
- Inserted additional check for equality types in check_mode_clause that
 berghofe parents: 
15126diff
changeset | 246 | end; | 
| 11537 | 247 | |
| 22642 | 248 | fun fixp f (x : (string * (int list option list * int list) list) list) = | 
| 11537 | 249 | let val y = f x | 
| 250 | in if x = y then x else fixp f y end; | |
| 251 | ||
| 22271 | 252 | fun infer_modes thy extra_modes arities arg_vs preds = fixp (fn modes => | 
| 11537 | 253 | map (check_modes_pred thy arg_vs preds (modes @ extra_modes)) modes) | 
| 22271 | 254 | (map (fn (s, (ks, k)) => (s, cprod (cprods (map | 
| 15531 | 255 | (fn NONE => [NONE] | 
| 22271 | 256 | | SOME k' => map SOME (subsets 1 k')) ks), | 
| 257 | subsets 1 k))) arities); | |
| 11537 | 258 | |
| 259 | (**** code generation ****) | |
| 260 | ||
| 261 | fun mk_eq (x::xs) = | |
| 262 | let fun mk_eqs _ [] = [] | |
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 263 | | mk_eqs a (b::cs) = str (a ^ " = " ^ b) :: mk_eqs b cs | 
| 11537 | 264 | in mk_eqs x xs end; | 
| 265 | ||
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 266 | fun mk_tuple xs = Pretty.block (str "(" ::
 | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 267 | List.concat (separate [str ",", Pretty.brk 1] (map single xs)) @ | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 268 | [str ")"]); | 
| 11537 | 269 | |
| 17521 | 270 | fun mk_v ((names, vs), s) = (case AList.lookup (op =) vs s of | 
| 15531 | 271 | NONE => ((names, (s, [s])::vs), s) | 
| 20071 
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
19861diff
changeset | 272 | | SOME xs => let val s' = Name.variant names s in | 
| 17521 | 273 | ((s'::names, AList.update (op =) (s, s'::xs) vs), s') end); | 
| 11537 | 274 | |
| 275 | fun distinct_v (nvs, Var ((s, 0), T)) = | |
| 276 | apsnd (Var o rpair T o rpair 0) (mk_v (nvs, s)) | |
| 277 | | distinct_v (nvs, t $ u) = | |
| 278 | let | |
| 279 | val (nvs', t') = distinct_v (nvs, t); | |
| 280 | val (nvs'', u') = distinct_v (nvs', u); | |
| 281 | in (nvs'', t' $ u') end | |
| 282 | | distinct_v x = x; | |
| 283 | ||
| 15061 
61a52739cd82
Added simple check that allows code generator to produce code containing
 berghofe parents: 
15031diff
changeset | 284 | fun is_exhaustive (Var _) = true | 
| 
61a52739cd82
Added simple check that allows code generator to produce code containing
 berghofe parents: 
15031diff
changeset | 285 |   | is_exhaustive (Const ("Pair", _) $ t $ u) =
 | 
| 
61a52739cd82
Added simple check that allows code generator to produce code containing
 berghofe parents: 
15031diff
changeset | 286 | is_exhaustive t andalso is_exhaustive u | 
| 
61a52739cd82
Added simple check that allows code generator to produce code containing
 berghofe parents: 
15031diff
changeset | 287 | | is_exhaustive _ = false; | 
| 
61a52739cd82
Added simple check that allows code generator to produce code containing
 berghofe parents: 
15031diff
changeset | 288 | |
| 
61a52739cd82
Added simple check that allows code generator to produce code containing
 berghofe parents: 
15031diff
changeset | 289 | fun compile_match nvs eq_ps out_ps success_p can_fail = | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 290 | let val eqs = List.concat (separate [str " andalso", Pretty.brk 1] | 
| 15570 | 291 | (map single (List.concat (map (mk_eq o snd) nvs) @ eq_ps))); | 
| 11537 | 292 | in | 
| 293 | Pretty.block | |
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 294 | ([str "(fn ", mk_tuple out_ps, str " =>", Pretty.brk 1] @ | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 295 | (Pretty.block ((if eqs=[] then [] else str "if " :: | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 296 | [Pretty.block eqs, Pretty.brk 1, str "then "]) @ | 
| 11537 | 297 | (success_p :: | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 298 | (if eqs=[] then [] else [Pretty.brk 1, str "else DSeq.empty"]))) :: | 
| 15061 
61a52739cd82
Added simple check that allows code generator to produce code containing
 berghofe parents: 
15031diff
changeset | 299 | (if can_fail then | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 300 | [Pretty.brk 1, str "| _ => DSeq.empty)"] | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 301 | else [str ")"]))) | 
| 11537 | 302 | end; | 
| 303 | ||
| 17144 | 304 | fun modename module s (iss, is) gr = | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 305 |   let val (id, gr') = if s = @{const_name "op ="} then (("", "equal"), gr)
 | 
| 17144 | 306 | else mk_const_id module s gr | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 307 | in (space_implode "__" | 
| 17144 | 308 | (mk_qual_id module id :: | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 309 | map (space_implode "_" o map string_of_int) (List.mapPartial I iss @ [is])), gr') | 
| 17144 | 310 | end; | 
| 11537 | 311 | |
| 22271 | 312 | fun mk_funcomp brack s k p = (if brack then parens else I) | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 313 |   (Pretty.block [Pretty.block ((if k = 0 then [] else [str "("]) @
 | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 314 | separate (Pretty.brk 1) (str s :: replicate k (str "|> ???")) @ | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 315 | (if k = 0 then [] else [str ")"])), Pretty.brk 1, p]); | 
| 22271 | 316 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 317 | fun compile_expr thy defs dep module brack modes (NONE, t) gr = | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 318 | apfst single (invoke_codegen thy defs dep module brack t gr) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 319 | | compile_expr _ _ _ _ _ _ (SOME _, Var ((name, _), _)) gr = | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 320 | ([str name], gr) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 321 | | compile_expr thy defs dep module brack modes (SOME (Mode (mode, _, ms)), t) gr = | 
| 22271 | 322 | (case strip_comb t of | 
| 323 | (Const (name, _), args) => | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 324 |            if name = @{const_name "op ="} orelse AList.defined op = modes name then
 | 
| 22271 | 325 | let | 
| 326 | val (args1, args2) = chop (length ms) args; | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 327 | val ((ps, mode_id), gr') = gr |> fold_map | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 328 | (compile_expr thy defs dep module true modes) (ms ~~ args1) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 329 | ||>> modename module name mode; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 330 | val (ps', gr'') = (case mode of | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 331 | ([], []) => ([str "()"], gr') | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 332 | | _ => fold_map | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 333 | (invoke_codegen thy defs dep module true) args2 gr') | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 334 | in ((if brack andalso not (null ps andalso null ps') then | 
| 22271 | 335 | single o parens o Pretty.block else I) | 
| 336 | (List.concat (separate [Pretty.brk 1] | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 337 | ([str mode_id] :: ps @ map single ps'))), gr') | 
| 22271 | 338 | end | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 339 | else apfst (single o mk_funcomp brack "??" (length (binder_types (fastype_of t)))) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 340 | (invoke_codegen thy defs dep module true t gr) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 341 | | _ => apfst (single o mk_funcomp brack "??" (length (binder_types (fastype_of t)))) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 342 | (invoke_codegen thy defs dep module true t gr)); | 
| 12557 | 343 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 344 | fun compile_clause thy defs dep module all_vs arg_vs modes (iss, is) (ts, ps) inp gr = | 
| 11537 | 345 | let | 
| 15570 | 346 | val modes' = modes @ List.mapPartial | 
| 15531 | 347 | (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) | 
| 12557 | 348 | (arg_vs ~~ iss); | 
| 349 | ||
| 11537 | 350 | fun check_constrt ((names, eqs), t) = | 
| 351 | if is_constrt thy t then ((names, eqs), t) else | |
| 20071 
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
19861diff
changeset | 352 | let val s = Name.variant names "x"; | 
| 11537 | 353 | in ((s::names, (s, t)::eqs), Var ((s, 0), fastype_of t)) end; | 
| 354 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 355 | fun compile_eq (s, t) gr = | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 356 | apfst (Pretty.block o cons (str (s ^ " = ")) o single) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 357 | (invoke_codegen thy defs dep module false t gr); | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 358 | |
| 12557 | 359 | val (in_ts, out_ts) = get_args is 1 ts; | 
| 11537 | 360 | val ((all_vs', eqs), in_ts') = | 
| 361 | foldl_map check_constrt ((all_vs, []), in_ts); | |
| 362 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 363 | fun compile_prems out_ts' vs names [] gr = | 
| 11537 | 364 | let | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 365 | val (out_ps, gr2) = fold_map | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 366 | (invoke_codegen thy defs dep module false) out_ts gr; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 367 | val (eq_ps, gr3) = fold_map compile_eq eqs gr2; | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 368 | val ((names', eqs'), out_ts'') = | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 369 | foldl_map check_constrt ((names, []), out_ts'); | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 370 | val (nvs, out_ts''') = foldl_map distinct_v | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 371 | ((names', map (fn x => (x, [x])) vs), out_ts''); | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 372 | val (out_ps', gr4) = fold_map | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 373 | (invoke_codegen thy defs dep module false) (out_ts''') gr3; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 374 | val (eq_ps', gr5) = fold_map compile_eq eqs' gr4; | 
| 11537 | 375 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 376 | (compile_match (snd nvs) (eq_ps @ eq_ps') out_ps' | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 377 | (Pretty.block [str "DSeq.single", Pretty.brk 1, mk_tuple out_ps]) | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 378 | (exists (not o is_exhaustive) out_ts'''), gr5) | 
| 11537 | 379 | end | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 380 | | compile_prems out_ts vs names ps gr = | 
| 11537 | 381 | let | 
| 19046 
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
 wenzelm parents: 
19025diff
changeset | 382 | val vs' = distinct (op =) (List.concat (vs :: map term_vs out_ts)); | 
| 22271 | 383 | val SOME (p, mode as SOME (Mode (_, js, _))) = | 
| 15126 
54ae6c6ef3b1
Fixed bug in compile_clause that caused equality constraints
 berghofe parents: 
15061diff
changeset | 384 | select_mode_prem thy modes' vs' ps; | 
| 11537 | 385 | val ps' = filter_out (equal p) ps; | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 386 | val ((names', eqs), out_ts') = | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 387 | foldl_map check_constrt ((names, []), out_ts); | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 388 | val (nvs, out_ts'') = foldl_map distinct_v | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 389 | ((names', map (fn x => (x, [x])) vs), out_ts'); | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 390 | val (out_ps, gr0) = fold_map | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 391 | (invoke_codegen thy defs dep module false) out_ts'' gr; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 392 | val (eq_ps, gr1) = fold_map compile_eq eqs gr0; | 
| 11537 | 393 | in | 
| 394 | (case p of | |
| 26806 | 395 | Prem (us, t, is_set) => | 
| 11537 | 396 | let | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 397 | val (in_ts, out_ts''') = get_args js 1 us; | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 398 | val (in_ps, gr2) = fold_map | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 399 | (invoke_codegen thy defs dep module true) in_ts gr1; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 400 | val (ps, gr3) = | 
| 26806 | 401 | if not is_set then | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 402 | apfst (fn ps => ps @ | 
| 24129 
591394d9ee66
- Added cycle test to function mk_ind_def to avoid non-termination
 berghofe parents: 
23761diff
changeset | 403 | (if null in_ps then [] else [Pretty.brk 1]) @ | 
| 22271 | 404 | separate (Pretty.brk 1) in_ps) | 
| 405 | (compile_expr thy defs dep module false modes | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 406 | (mode, t) gr2) | 
| 26806 | 407 | else | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 408 | apfst (fn p => [str "DSeq.of_list", Pretty.brk 1, p]) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 409 | (invoke_codegen thy defs dep module true t gr2); | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 410 | val (rest, gr4) = compile_prems out_ts''' vs' (fst nvs) ps' gr3; | 
| 11537 | 411 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 412 | (compile_match (snd nvs) eq_ps out_ps | 
| 12557 | 413 | (Pretty.block (ps @ | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 414 | [str " :->", Pretty.brk 1, rest])) | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 415 | (exists (not o is_exhaustive) out_ts''), gr4) | 
| 11537 | 416 | end | 
| 417 | | Sidecond t => | |
| 418 | let | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 419 | val (side_p, gr2) = invoke_codegen thy defs dep module true t gr1; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 420 | val (rest, gr3) = compile_prems [] vs' (fst nvs) ps' gr2; | 
| 11537 | 421 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 422 | (compile_match (snd nvs) eq_ps out_ps | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 423 | (Pretty.block [str "?? ", side_p, | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 424 | str " :->", Pretty.brk 1, rest]) | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 425 | (exists (not o is_exhaustive) out_ts''), gr3) | 
| 11537 | 426 | end) | 
| 427 | end; | |
| 428 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 429 | val (prem_p, gr') = compile_prems in_ts' arg_vs all_vs' ps gr ; | 
| 11537 | 430 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 431 | (Pretty.block [str "DSeq.single", Pretty.brk 1, inp, | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 432 | str " :->", Pretty.brk 1, prem_p], gr') | 
| 11537 | 433 | end; | 
| 434 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 435 | fun compile_pred thy defs dep module prfx all_vs arg_vs modes s cls mode gr = | 
| 22271 | 436 | let | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 437 | val xs = map str (Name.variant_list arg_vs | 
| 22271 | 438 | (map (fn i => "x" ^ string_of_int i) (snd mode))); | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 439 | val ((cl_ps, mode_id), gr') = gr |> | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 440 | fold_map (fn cl => compile_clause thy defs | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 441 | dep module all_vs arg_vs modes mode cl (mk_tuple xs)) cls ||>> | 
| 22271 | 442 | modename module s mode | 
| 11537 | 443 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 444 | (Pretty.block | 
| 11537 | 445 | ([Pretty.block (separate (Pretty.brk 1) | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 446 | (str (prfx ^ mode_id) :: | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 447 | map str arg_vs @ | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 448 | (case mode of ([], []) => [str "()"] | _ => xs)) @ | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 449 | [str " ="]), | 
| 11537 | 450 | Pretty.brk 1] @ | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 451 | List.concat (separate [str " ++", Pretty.brk 1] (map single cl_ps))), (gr', "and ")) | 
| 11537 | 452 | end; | 
| 453 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 454 | fun compile_preds thy defs dep module all_vs arg_vs modes preds gr = | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 455 | let val (prs, (gr', _)) = fold_map (fn (s, cls) => | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 456 | fold_map (fn mode => fn (gr', prfx') => compile_pred thy defs | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 457 | dep module prfx' all_vs arg_vs modes s cls mode gr') | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 458 | (((the o AList.lookup (op =) modes) s))) preds (gr, "fun ") | 
| 11537 | 459 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 460 | (space_implode "\n\n" (map string_of (List.concat prs)) ^ ";\n\n", gr') | 
| 11537 | 461 | end; | 
| 462 | ||
| 463 | (**** processing of introduction rules ****) | |
| 464 | ||
| 12557 | 465 | exception Modes of | 
| 466 | (string * (int list option list * int list) list) list * | |
| 22271 | 467 | (string * (int option list * int)) list; | 
| 12557 | 468 | |
| 17144 | 469 | fun lookup_modes gr dep = apfst List.concat (apsnd List.concat (ListPair.unzip | 
| 470 | (map ((fn (SOME (Modes x), _, _) => x | _ => ([], [])) o get_node gr) | |
| 471 | (Graph.all_preds (fst gr) [dep])))); | |
| 12557 | 472 | |
| 22271 | 473 | fun print_arities arities = message ("Arities:\n" ^
 | 
| 26931 | 474 | cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^ | 
| 12557 | 475 | space_implode " -> " (map | 
| 22271 | 476 | (fn NONE => "X" | SOME k' => string_of_int k') | 
| 477 | (ks @ [SOME k]))) arities)); | |
| 11537 | 478 | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 479 | 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: 
14981diff
changeset | 480 | |
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 481 | fun constrain cs [] = [] | 
| 22642 | 482 | | constrain cs ((s, xs) :: ys) = (s, case AList.lookup (op =) cs (s : string) of | 
| 15531 | 483 | NONE => xs | 
| 484 | | SOME xs' => xs inter xs') :: constrain cs ys; | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 485 | |
| 17144 | 486 | fun mk_extra_defs thy defs gr dep names module ts = | 
| 15570 | 487 | Library.foldl (fn (gr, name) => | 
| 12557 | 488 | if name mem names then gr | 
| 489 | else (case get_clauses thy name of | |
| 15531 | 490 | NONE => gr | 
| 22271 | 491 | | SOME (names, thyname, nparms, intrs) => | 
| 17144 | 492 | mk_ind_def thy defs gr dep names (if_library thyname module) | 
| 22271 | 493 | [] (prep_intrs intrs) nparms)) | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 494 | (gr, foldr add_term_consts [] ts) | 
| 12557 | 495 | |
| 22271 | 496 | and mk_ind_def thy defs gr dep names module modecs intrs nparms = | 
| 24129 
591394d9ee66
- Added cycle test to function mk_ind_def to avoid non-termination
 berghofe parents: 
23761diff
changeset | 497 | add_edge_acyclic (hd names, dep) gr handle | 
| 
591394d9ee66
- Added cycle test to function mk_ind_def to avoid non-termination
 berghofe parents: 
23761diff
changeset | 498 | Graph.CYCLES (xs :: _) => | 
| 
591394d9ee66
- Added cycle test to function mk_ind_def to avoid non-termination
 berghofe parents: 
23761diff
changeset | 499 |       error ("InductiveCodegen: illegal cyclic dependencies:\n" ^ commas xs)
 | 
| 
591394d9ee66
- Added cycle test to function mk_ind_def to avoid non-termination
 berghofe parents: 
23761diff
changeset | 500 | | Graph.UNDEF _ => | 
| 11537 | 501 | let | 
| 22271 | 502 | val _ $ u = Logic.strip_imp_concl (hd intrs); | 
| 503 | val args = List.take (snd (strip_comb u), nparms); | |
| 15570 | 504 | val arg_vs = List.concat (map term_vs args); | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 505 | |
| 22271 | 506 | fun get_nparms s = if s mem names then SOME nparms else | 
| 507 | Option.map #3 (get_clauses thy s); | |
| 11537 | 508 | |
| 26806 | 509 |       fun dest_prem (_ $ (Const ("op :", _) $ t $ u)) = Prem ([t], Envir.beta_eta_contract u, true)
 | 
| 510 |         | dest_prem (_ $ ((eq as Const ("op =", _)) $ t $ u)) = Prem ([t, u], eq, false)
 | |
| 22271 | 511 | | dest_prem (_ $ t) = | 
| 512 | (case strip_comb t of | |
| 26806 | 513 | (v as Var _, ts) => if v mem args then Prem (ts, v, false) else Sidecond t | 
| 22271 | 514 | | (c as Const (s, _), ts) => (case get_nparms s of | 
| 515 | NONE => Sidecond t | |
| 516 | | SOME k => | |
| 517 | let val (ts1, ts2) = chop k ts | |
| 26806 | 518 | in Prem (ts2, list_comb (c, ts1), false) end) | 
| 22271 | 519 | | _ => Sidecond t); | 
| 520 | ||
| 521 | fun add_clause intr (clauses, arities) = | |
| 11537 | 522 | let | 
| 22271 | 523 | val _ $ t = Logic.strip_imp_concl intr; | 
| 524 | val (Const (name, T), ts) = strip_comb t; | |
| 525 | val (ts1, ts2) = chop nparms ts; | |
| 526 | val prems = map dest_prem (Logic.strip_imp_prems intr); | |
| 527 | val (Ts, Us) = chop nparms (binder_types T) | |
| 11537 | 528 | in | 
| 22271 | 529 | (AList.update op = (name, these (AList.lookup op = clauses name) @ | 
| 530 | [(ts2, prems)]) clauses, | |
| 531 | AList.update op = (name, (map (fn U => (case strip_type U of | |
| 532 |                  (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
 | |
| 533 | | _ => NONE)) Ts, | |
| 534 | length Us)) arities) | |
| 11537 | 535 | end; | 
| 536 | ||
| 16645 | 537 | val gr' = mk_extra_defs thy defs | 
| 17144 | 538 | (add_edge (hd names, dep) | 
| 539 | (new_node (hd names, (NONE, "", "")) gr)) (hd names) names module intrs; | |
| 22271 | 540 | val (extra_modes, extra_arities) = lookup_modes gr' (hd names); | 
| 541 | val (clauses, arities) = fold add_clause intrs ([], []); | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 542 | val modes = constrain modecs | 
| 22271 | 543 | (infer_modes thy extra_modes arities arg_vs clauses); | 
| 544 | val _ = print_arities arities; | |
| 11537 | 545 | val _ = print_modes modes; | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 546 | val (s, gr'') = compile_preds thy defs (hd names) module (terms_vs intrs) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 547 | arg_vs (modes @ extra_modes) clauses gr'; | 
| 11537 | 548 | in | 
| 17144 | 549 | (map_node (hd names) | 
| 22271 | 550 | (K (SOME (Modes (modes, arities)), module, s)) gr'') | 
| 16645 | 551 | end; | 
| 11537 | 552 | |
| 22271 | 553 | fun find_mode gr dep s u modes is = (case find_first (fn Mode (_, js, _) => is=js) | 
| 15660 | 554 | (modes_of modes u handle Option => []) of | 
| 17144 | 555 | NONE => codegen_error gr dep | 
| 556 |        ("No such mode for " ^ s ^ ": " ^ string_of_mode ([], is))
 | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 557 | | mode => mode); | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 558 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 559 | fun mk_ind_call thy defs dep module is_query s T ts names thyname k intrs gr = | 
| 22271 | 560 | let | 
| 561 | val (ts1, ts2) = chop k ts; | |
| 562 | val u = list_comb (Const (s, T), ts1); | |
| 563 | ||
| 564 |     fun mk_mode (((ts, mode), i), Const ("dummy_pattern", _)) =
 | |
| 565 | ((ts, mode), i+1) | |
| 566 | | mk_mode (((ts, mode), i), t) = ((ts @ [t], mode @ [i]), i+1); | |
| 11537 | 567 | |
| 22271 | 568 | val module' = if_library thyname module; | 
| 569 | val gr1 = mk_extra_defs thy defs | |
| 570 | (mk_ind_def thy defs gr dep names module' | |
| 571 | [] (prep_intrs intrs) k) dep names module' [u]; | |
| 572 | val (modes, _) = lookup_modes gr1 dep; | |
| 573 | val (ts', is) = if is_query then | |
| 574 | fst (Library.foldl mk_mode ((([], []), 1), ts2)) | |
| 575 | else (ts2, 1 upto length (binder_types T) - k); | |
| 576 | val mode = find_mode gr1 dep s u modes is; | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 577 | val (in_ps, gr2) = fold_map (invoke_codegen thy defs dep module true) ts' gr1; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 578 | val (ps, gr3) = compile_expr thy defs dep module false modes (mode, u) gr2; | 
| 22271 | 579 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 580 | (Pretty.block (ps @ (if null in_ps then [] else [Pretty.brk 1]) @ | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 581 | separate (Pretty.brk 1) in_ps), gr3) | 
| 22271 | 582 | end; | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 583 | |
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 584 | fun clause_of_eqn eqn = | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 585 | let | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 586 | val (t, u) = HOLogic.dest_eq (HOLogic.dest_Trueprop (concl_of eqn)); | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 587 | val (Const (s, T), ts) = strip_comb t; | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 588 | val (Ts, U) = strip_type T | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 589 | in | 
| 22271 | 590 | rename_term (Logic.list_implies (prems_of eqn, HOLogic.mk_Trueprop | 
| 591 | (list_comb (Const (s ^ "_aux", Ts @ [U] ---> HOLogic.boolT), ts @ [u])))) | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 592 | end; | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 593 | |
| 17144 | 594 | fun mk_fun thy defs name eqns dep module module' gr = | 
| 595 | case try (get_node gr) name of | |
| 596 | NONE => | |
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 597 | let | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 598 | val clauses = map clause_of_eqn eqns; | 
| 17144 | 599 | val pname = name ^ "_aux"; | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 600 | val arity = length (snd (strip_comb (fst (HOLogic.dest_eq | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 601 | (HOLogic.dest_Trueprop (concl_of (hd eqns))))))); | 
| 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 602 | val mode = 1 upto arity; | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 603 | val ((fun_id, mode_id), gr') = gr |> | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 604 | mk_const_id module' name ||>> | 
| 17144 | 605 | modename module' pname ([], mode); | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 606 |       val vars = map (fn i => str ("x" ^ string_of_int i)) mode;
 | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 607 | val s = string_of (Pretty.block | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 608 |         [mk_app false (str ("fun " ^ snd fun_id)) vars, str " =",
 | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 609 | Pretty.brk 1, str "DSeq.hd", Pretty.brk 1, | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 610 | parens (Pretty.block (separate (Pretty.brk 1) (str mode_id :: | 
| 22271 | 611 | vars)))]) ^ ";\n\n"; | 
| 17144 | 612 | val gr'' = mk_ind_def thy defs (add_edge (name, dep) | 
| 613 | (new_node (name, (NONE, module', s)) gr')) name [pname] module' | |
| 22271 | 614 | [(pname, [([], mode)])] clauses 0; | 
| 17144 | 615 | val (modes, _) = lookup_modes gr'' dep; | 
| 22271 | 616 | val _ = find_mode gr'' dep pname (head_of (HOLogic.dest_Trueprop | 
| 617 | (Logic.strip_imp_concl (hd clauses)))) modes mode | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 618 | in (mk_qual_id module fun_id, gr'') end | 
| 17144 | 619 | | SOME _ => | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 620 | (mk_qual_id module (get_const_id gr name), add_edge (name, dep) gr); | 
| 15031 
726dc9146bb1
- Added support for conditional equations whose premises involve
 berghofe parents: 
14981diff
changeset | 621 | |
| 23761 | 622 | (* convert n-tuple to nested pairs *) | 
| 623 | ||
| 624 | fun conv_ntuple fs ts p = | |
| 625 | let | |
| 626 | val k = length fs; | |
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 627 |     val xs = map (fn i => str ("x" ^ string_of_int i)) (0 upto k);
 | 
| 23761 | 628 | val xs' = map (fn Bound i => nth xs (k - i)) ts; | 
| 629 | fun conv xs js = | |
| 630 | if js mem fs then | |
| 631 | let | |
| 632 | val (p, xs') = conv xs (1::js); | |
| 633 | val (q, xs'') = conv xs' (2::js) | |
| 634 | in (mk_tuple [p, q], xs'') end | |
| 635 | else (hd xs, tl xs) | |
| 636 | in | |
| 637 | if k > 0 then | |
| 638 | Pretty.block | |
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 639 | [str "DSeq.map (fn", Pretty.brk 1, | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 640 | mk_tuple xs', str " =>", Pretty.brk 1, fst (conv xs []), | 
| 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 641 | str ")", Pretty.brk 1, parens p] | 
| 23761 | 642 | else p | 
| 643 | end; | |
| 644 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 645 | fun inductive_codegen thy defs dep module brack t gr = (case strip_comb t of | 
| 23761 | 646 |     (Const ("Collect", _), [u]) =>
 | 
| 647 | let val (r, Ts, fs) = HOLogic.strip_split u | |
| 648 | in case strip_comb r of | |
| 649 | (Const (s, T), ts) => | |
| 650 | (case (get_clauses thy s, get_assoc_code thy (s, T)) of | |
| 651 | (SOME (names, thyname, k, intrs), NONE) => | |
| 652 | let | |
| 653 | val (ts1, ts2) = chop k ts; | |
| 654 | val ts2' = map | |
| 655 | (fn Bound i => Term.dummy_pattern (nth Ts i) | t => t) ts2; | |
| 656 | val (ots, its) = List.partition is_Bound ts2; | |
| 657 | val no_loose = forall (fn t => not (loose_bvar (t, 0))) | |
| 658 | in | |
| 659 | if null (duplicates op = ots) andalso | |
| 660 | no_loose ts1 andalso no_loose its | |
| 661 | then | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 662 | let val (call_p, gr') = mk_ind_call thy defs dep module true | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 663 | s T (ts1 @ ts2') names thyname k intrs gr | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 664 | in SOME ((if brack then parens else I) (Pretty.block | 
| 26975 
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
 berghofe parents: 
26939diff
changeset | 665 |                       [str "DSeq.list_of", Pretty.brk 1, str "(",
 | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 666 | conv_ntuple fs ots call_p, str ")"]), gr') | 
| 23761 | 667 | end | 
| 668 | else NONE | |
| 669 | end | |
| 670 | | _ => NONE) | |
| 671 | | _ => NONE | |
| 672 | end | |
| 22271 | 673 | | (Const (s, T), ts) => (case Symtab.lookup (#eqns (CodegenData.get thy)) s of | 
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 674 | NONE => (case (get_clauses thy s, get_assoc_code thy (s, T)) of | 
| 22271 | 675 | (SOME (names, thyname, k, intrs), NONE) => | 
| 676 | if length ts < k then NONE else SOME | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 677 | (let val (call_p, gr') = mk_ind_call thy defs dep module false | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 678 | s T (map Term.no_dummy_patterns ts) names thyname k intrs gr | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 679 | in (mk_funcomp brack "?!" | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 680 | (length (binder_types T) - length ts) (parens call_p), gr') | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 681 | end handle TERM _ => mk_ind_call thy defs dep module true | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 682 | s T ts names thyname k intrs gr ) | 
| 22271 | 683 | | _ => NONE) | 
| 684 | | SOME eqns => | |
| 685 | let | |
| 22642 | 686 | val (_, thyname) :: _ = eqns; | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 687 | val (id, gr') = mk_fun thy defs s (preprocess thy (map fst (rev eqns))) | 
| 22271 | 688 | dep module (if_library thyname module) gr; | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 689 | val (ps, gr'') = fold_map | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 690 | (invoke_codegen thy defs dep module true) ts gr'; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
27809diff
changeset | 691 | in SOME (mk_app brack (str id) ps, gr'') | 
| 22271 | 692 | end) | 
| 693 | | _ => NONE); | |
| 11537 | 694 | |
| 12557 | 695 | val setup = | 
| 18708 | 696 | add_codegen "inductive" inductive_codegen #> | 
| 24219 | 697 |   Code.add_attribute ("ind", Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) --
 | 
| 27809 
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27398diff
changeset | 698 | Scan.option (Args.$$$ "params" |-- Args.colon |-- OuterParse.nat) >> uncurry add); | 
| 11537 | 699 | |
| 700 | end; |