src/HOL/Tools/inductive_codegen.ML
author haftmann
Fri Jan 22 16:56:51 2010 +0100 (2010-01-22)
changeset 34962 807f6ce0273d
parent 33963 977b94b64905
child 35021 c839a4c670c6
permissions -rw-r--r--
HOLogic.strip_psplits: types are ordered after syntactic appearance, not after corresponding de-Bruin index (closer correspondence to similar strip operations)
     1 (*  Title:      HOL/Tools/inductive_codegen.ML
     2     Author:     Stefan Berghofer, TU Muenchen
     3 
     4 Code generator for inductive predicates.
     5 *)
     6 
     7 signature INDUCTIVE_CODEGEN =
     8 sig
     9   val add : string option -> int option -> attribute
    10   val test_fn : (int * int * int -> term list option) Unsynchronized.ref
    11   val test_term: Proof.context -> term -> int -> term list option
    12   val setup : theory -> theory
    13   val quickcheck_setup : theory -> theory
    14 end;
    15 
    16 structure InductiveCodegen : INDUCTIVE_CODEGEN =
    17 struct
    18 
    19 open Codegen;
    20 
    21 (**** theory data ****)
    22 
    23 fun merge_rules tabs =
    24   Symtab.join (fn _ => AList.merge (Thm.eq_thm_prop) (K true)) tabs;
    25 
    26 structure CodegenData = Theory_Data
    27 (
    28   type T =
    29     {intros : (thm * (string * int)) list Symtab.table,
    30      graph : unit Graph.T,
    31      eqns : (thm * string) list Symtab.table};
    32   val empty =
    33     {intros = Symtab.empty, graph = Graph.empty, eqns = Symtab.empty};
    34   val extend = I;
    35   fun merge ({intros=intros1, graph=graph1, eqns=eqns1},
    36     {intros=intros2, graph=graph2, eqns=eqns2}) : T =
    37     {intros = merge_rules (intros1, intros2),
    38      graph = Graph.merge (K true) (graph1, graph2),
    39      eqns = merge_rules (eqns1, eqns2)};
    40 );
    41 
    42 
    43 fun warn thm = warning ("InductiveCodegen: Not a proper clause:\n" ^
    44   Display.string_of_thm_without_context thm);
    45 
    46 fun add_node x g = Graph.new_node (x, ()) g handle Graph.DUP _ => g;
    47 
    48 fun add optmod optnparms = Thm.declaration_attribute (fn thm => Context.mapping (fn thy =>
    49   let
    50     val {intros, graph, eqns} = CodegenData.get thy;
    51     fun thyname_of s = (case optmod of
    52       NONE => Codegen.thyname_of_const thy s | SOME s => s);
    53   in (case Option.map strip_comb (try HOLogic.dest_Trueprop (concl_of thm)) of
    54       SOME (Const ("op =", _), [t, _]) => (case head_of t of
    55         Const (s, _) =>
    56           CodegenData.put {intros = intros, graph = graph,
    57              eqns = eqns |> Symtab.map_default (s, [])
    58                (AList.update Thm.eq_thm_prop (thm, thyname_of s))} thy
    59       | _ => (warn thm; thy))
    60     | SOME (Const (s, _), _) =>
    61         let
    62           val cs = fold Term.add_const_names (Thm.prems_of thm) [];
    63           val rules = Symtab.lookup_list intros s;
    64           val nparms = (case optnparms of
    65             SOME k => k
    66           | NONE => (case rules of
    67              [] => (case try (Inductive.the_inductive (ProofContext.init thy)) s of
    68                  SOME (_, {raw_induct, ...}) =>
    69                    length (Inductive.params_of raw_induct)
    70                | NONE => 0)
    71             | xs => snd (snd (snd (split_last xs)))))
    72         in CodegenData.put
    73           {intros = intros |>
    74            Symtab.update (s, (AList.update Thm.eq_thm_prop
    75              (thm, (thyname_of s, nparms)) rules)),
    76            graph = fold_rev (Graph.add_edge o pair s) cs (fold add_node (s :: cs) graph),
    77            eqns = eqns} thy
    78         end
    79     | _ => (warn thm; thy))
    80   end) I);
    81 
    82 fun get_clauses thy s =
    83   let val {intros, graph, ...} = CodegenData.get thy
    84   in case Symtab.lookup intros s of
    85       NONE => (case try (Inductive.the_inductive (ProofContext.init thy)) s of
    86         NONE => NONE
    87       | SOME ({names, ...}, {intrs, raw_induct, ...}) =>
    88           SOME (names, Codegen.thyname_of_const thy s,
    89             length (Inductive.params_of raw_induct),
    90             preprocess thy intrs))
    91     | SOME _ =>
    92         let
    93           val SOME names = find_first
    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
    98   end;
    99 
   100 
   101 (**** check if a term contains only constructor functions ****)
   102 
   103 fun is_constrt thy =
   104   let
   105     val cnstrs = flat (maps
   106       (map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd)
   107       (Symtab.dest (Datatype_Data.get_all thy)));
   108     fun check t = (case strip_comb t of
   109         (Var _, []) => true
   110       | (Const (s, _), ts) => (case AList.lookup (op =) cnstrs s of
   111             NONE => false
   112           | SOME i => length ts = i andalso forall check ts)
   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 
   123 fun string_of_mode (iss, is) = space_implode " -> " (map
   124   (fn NONE => "X"
   125     | SOME js => enclose "[" "]" (commas (map string_of_int js)))
   126        (iss @ [SOME is]));
   127 
   128 fun print_modes modes = message ("Inferred modes:\n" ^
   129   cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
   130     (fn (m, rnd) => string_of_mode m ^
   131        (if rnd then " (random)" else "")) ms)) modes));
   132 
   133 val term_vs = map (fst o fst o dest_Var) o OldTerm.term_vars;
   134 val terms_vs = distinct (op =) o maps term_vs;
   135 
   136 (** collect all Vars in a term (with duplicates!) **)
   137 fun term_vTs tm =
   138   fold_aterms (fn Var ((x, _), T) => cons (x, T) | _ => I) tm [];
   139 
   140 fun get_args _ _ [] = ([], [])
   141   | get_args is i (x::xs) = (if i mem is then apfst else apsnd) (cons x)
   142       (get_args is (i+1) xs);
   143 
   144 fun merge xs [] = xs
   145   | merge [] ys = ys
   146   | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys)
   147       else y::merge (x::xs) ys;
   148 
   149 fun subsets i j = if i <= j then
   150        let val is = subsets (i+1) j
   151        in merge (map (fn ks => i::ks) is) is end
   152      else [[]];
   153 
   154 fun cprod ([], ys) = []
   155   | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys);
   156 
   157 fun cprods xss = List.foldr (map op :: o cprod) [[]] xss;
   158 
   159 datatype mode = Mode of ((int list option list * int list) * bool) * int list * mode option list;
   160 
   161 fun needs_random (Mode ((_, b), _, ms)) =
   162   b orelse exists (fn NONE => false | SOME m => needs_random m) ms;
   163 
   164 fun modes_of modes t =
   165   let
   166     val ks = 1 upto length (binder_types (fastype_of t));
   167     val default = [Mode ((([], ks), false), ks, [])];
   168     fun mk_modes name args = Option.map
   169      (maps (fn (m as ((iss, is), _)) =>
   170         let
   171           val (args1, args2) =
   172             if length args < length iss then
   173               error ("Too few arguments for inductive predicate " ^ name)
   174             else chop (length iss) args;
   175           val k = length args2;
   176           val prfx = 1 upto k
   177         in
   178           if not (is_prefix op = prfx is) then [] else
   179           let val is' = map (fn i => i - k) (List.drop (is, k))
   180           in map (fn x => Mode (m, is', x)) (cprods (map
   181             (fn (NONE, _) => [NONE]
   182               | (SOME js, arg) => map SOME (filter
   183                   (fn Mode (_, js', _) => js=js') (modes_of modes arg)))
   184                     (iss ~~ args1)))
   185           end
   186         end)) (AList.lookup op = modes name)
   187 
   188   in (case strip_comb t of
   189       (Const ("op =", Type (_, [T, _])), _) =>
   190         [Mode ((([], [1]), false), [1], []), Mode ((([], [2]), false), [2], [])] @
   191         (if is_eqT T then [Mode ((([], [1, 2]), false), [1, 2], [])] else [])
   192     | (Const (name, _), args) => the_default default (mk_modes name args)
   193     | (Var ((name, _), _), args) => the (mk_modes name args)
   194     | (Free (name, _), args) => the (mk_modes name args)
   195     | _ => default)
   196   end;
   197 
   198 datatype indprem = Prem of term list * term * bool | Sidecond of term;
   199 
   200 fun missing_vars vs ts = subtract (fn (x, ((y, _), _)) => x = y) vs
   201   (fold Term.add_vars ts []);
   202 
   203 fun monomorphic_vars vs = null (fold (Term.add_tvarsT o snd) vs []);
   204 
   205 fun mode_ord p = int_ord (pairself (fn (Mode ((_, rnd), _, _), vs) =>
   206   length vs + (if null vs then 0 else 1) + (if rnd then 1 else 0)) p);
   207 
   208 fun select_mode_prem thy modes vs ps =
   209   sort (mode_ord o pairself (hd o snd))
   210     (filter_out (null o snd) (ps ~~ map
   211       (fn Prem (us, t, is_set) => sort mode_ord
   212           (List.mapPartial (fn m as Mode (_, is, _) =>
   213             let
   214               val (in_ts, out_ts) = get_args is 1 us;
   215               val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts;
   216               val vTs = maps term_vTs out_ts';
   217               val dupTs = map snd (duplicates (op =) vTs) @
   218                 map_filter (AList.lookup (op =) vTs) vs;
   219               val missing_vs = missing_vars vs (t :: in_ts @ in_ts')
   220             in
   221               if forall (is_eqT o fastype_of) in_ts' andalso forall is_eqT dupTs
   222                 andalso monomorphic_vars missing_vs
   223               then SOME (m, missing_vs)
   224               else NONE
   225             end)
   226               (if is_set then [Mode ((([], []), false), [], [])]
   227                else modes_of modes t handle Option =>
   228                  error ("Bad predicate: " ^ Syntax.string_of_term_global thy t)))
   229         | Sidecond t =>
   230             let val missing_vs = missing_vars vs [t]
   231             in
   232               if monomorphic_vars missing_vs
   233               then [(Mode ((([], []), false), [], []), missing_vs)]
   234               else []
   235             end)
   236               ps));
   237 
   238 fun use_random () = "random_ind" mem !Codegen.mode;
   239 
   240 fun check_mode_clause thy arg_vs modes ((iss, is), rnd) (ts, ps) =
   241   let
   242     val modes' = modes @ map_filter
   243       (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [(([], js), false)]))
   244         (arg_vs ~~ iss);
   245     fun check_mode_prems vs rnd [] = SOME (vs, rnd)
   246       | check_mode_prems vs rnd ps = (case select_mode_prem thy modes' vs ps of
   247           (x, (m, []) :: _) :: _ => check_mode_prems
   248             (case x of Prem (us, _, _) => union (op =) vs (terms_vs us) | _ => vs)
   249             (rnd orelse needs_random m)
   250             (filter_out (equal x) ps)
   251         | (_, (_, vs') :: _) :: _ =>
   252             if use_random () then
   253               check_mode_prems (union (op =) vs (map (fst o fst) vs')) true ps
   254             else NONE
   255         | _ => NONE);
   256     val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (get_args is 1 ts));
   257     val in_vs = terms_vs in_ts;
   258   in
   259     if forall is_eqT (map snd (duplicates (op =) (maps term_vTs in_ts))) andalso
   260       forall (is_eqT o fastype_of) in_ts'
   261     then (case check_mode_prems (union (op =) arg_vs in_vs) rnd ps of
   262        NONE => NONE
   263      | SOME (vs, rnd') =>
   264          let val missing_vs = missing_vars vs ts
   265          in
   266            if null missing_vs orelse
   267              use_random () andalso monomorphic_vars missing_vs
   268            then SOME (rnd' orelse not (null missing_vs))
   269            else NONE
   270          end)
   271     else NONE
   272   end;
   273 
   274 fun check_modes_pred thy arg_vs preds modes (p, ms) =
   275   let val SOME rs = AList.lookup (op =) preds p
   276   in (p, List.mapPartial (fn m as (m', _) =>
   277     let val xs = map (check_mode_clause thy arg_vs modes m) rs
   278     in case find_index is_none xs of
   279         ~1 => SOME (m', exists (fn SOME b => b) xs)
   280       | i => (message ("Clause " ^ string_of_int (i+1) ^ " of " ^
   281         p ^ " violates mode " ^ string_of_mode m'); NONE)
   282     end) ms)
   283   end;
   284 
   285 fun fixp f (x : (string * ((int list option list * int list) * bool) list) list) =
   286   let val y = f x
   287   in if x = y then x else fixp f y end;
   288 
   289 fun infer_modes thy extra_modes arities arg_vs preds = fixp (fn modes =>
   290   map (check_modes_pred thy arg_vs preds (modes @ extra_modes)) modes)
   291     (map (fn (s, (ks, k)) => (s, map (rpair false) (cprod (cprods (map
   292       (fn NONE => [NONE]
   293         | SOME k' => map SOME (subsets 1 k')) ks),
   294       subsets 1 k)))) arities);
   295 
   296 (**** code generation ****)
   297 
   298 fun mk_eq (x::xs) =
   299   let fun mk_eqs _ [] = []
   300         | mk_eqs a (b::cs) = str (a ^ " = " ^ b) :: mk_eqs b cs
   301   in mk_eqs x xs end;
   302 
   303 fun mk_tuple xs = Pretty.block (str "(" ::
   304   flat (separate [str ",", Pretty.brk 1] (map single xs)) @
   305   [str ")"]);
   306 
   307 fun mk_v s (names, vs) =
   308   (case AList.lookup (op =) vs s of
   309     NONE => (s, (names, (s, [s])::vs))
   310   | SOME xs =>
   311       let val s' = Name.variant names s
   312       in (s', (s'::names, AList.update (op =) (s, s'::xs) vs)) end);
   313 
   314 fun distinct_v (Var ((s, 0), T)) nvs =
   315       let val (s', nvs') = mk_v s nvs
   316       in (Var ((s', 0), T), nvs') end
   317   | distinct_v (t $ u) nvs =
   318       let
   319         val (t', nvs') = distinct_v t nvs;
   320         val (u', nvs'') = distinct_v u nvs';
   321       in (t' $ u', nvs'') end
   322   | distinct_v t nvs = (t, nvs);
   323 
   324 fun is_exhaustive (Var _) = true
   325   | is_exhaustive (Const ("Pair", _) $ t $ u) =
   326       is_exhaustive t andalso is_exhaustive u
   327   | is_exhaustive _ = false;
   328 
   329 fun compile_match nvs eq_ps out_ps success_p can_fail =
   330   let val eqs = flat (separate [str " andalso", Pretty.brk 1]
   331     (map single (maps (mk_eq o snd) nvs @ eq_ps)));
   332   in
   333     Pretty.block
   334      ([str "(fn ", mk_tuple out_ps, str " =>", Pretty.brk 1] @
   335       (Pretty.block ((if eqs=[] then [] else str "if " ::
   336          [Pretty.block eqs, Pretty.brk 1, str "then "]) @
   337          (success_p ::
   338           (if eqs=[] then [] else [Pretty.brk 1, str "else DSeq.empty"]))) ::
   339        (if can_fail then
   340           [Pretty.brk 1, str "| _ => DSeq.empty)"]
   341         else [str ")"])))
   342   end;
   343 
   344 fun modename module s (iss, is) gr =
   345   let val (id, gr') = if s = @{const_name "op ="} then (("", "equal"), gr)
   346     else mk_const_id module s gr
   347   in (space_implode "__"
   348     (mk_qual_id module id ::
   349       map (space_implode "_" o map string_of_int) (map_filter I iss @ [is])), gr')
   350   end;
   351 
   352 fun mk_funcomp brack s k p = (if brack then parens else I)
   353   (Pretty.block [Pretty.block ((if k = 0 then [] else [str "("]) @
   354     separate (Pretty.brk 1) (str s :: replicate k (str "|> ???")) @
   355     (if k = 0 then [] else [str ")"])), Pretty.brk 1, p]);
   356 
   357 fun compile_expr thy defs dep module brack modes (NONE, t) gr =
   358       apfst single (invoke_codegen thy defs dep module brack t gr)
   359   | compile_expr _ _ _ _ _ _ (SOME _, Var ((name, _), _)) gr =
   360       ([str name], gr)
   361   | compile_expr thy defs dep module brack modes (SOME (Mode ((mode, _), _, ms)), t) gr =
   362       (case strip_comb t of
   363          (Const (name, _), args) =>
   364            if name = @{const_name "op ="} orelse AList.defined op = modes name then
   365              let
   366                val (args1, args2) = chop (length ms) args;
   367                val ((ps, mode_id), gr') = gr |> fold_map
   368                    (compile_expr thy defs dep module true modes) (ms ~~ args1)
   369                    ||>> modename module name mode;
   370                val (ps', gr'') = (case mode of
   371                    ([], []) => ([str "()"], gr')
   372                  | _ => fold_map
   373                      (invoke_codegen thy defs dep module true) args2 gr')
   374              in ((if brack andalso not (null ps andalso null ps') then
   375                single o parens o Pretty.block else I)
   376                  (flat (separate [Pretty.brk 1]
   377                    ([str mode_id] :: ps @ map single ps'))), gr')
   378              end
   379            else apfst (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
   380              (invoke_codegen thy defs dep module true t gr)
   381        | _ => apfst (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
   382            (invoke_codegen thy defs dep module true t gr));
   383 
   384 fun compile_clause thy defs dep module all_vs arg_vs modes (iss, is) (ts, ps) inp gr =
   385   let
   386     val modes' = modes @ map_filter
   387       (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [(([], js), false)]))
   388         (arg_vs ~~ iss);
   389 
   390     fun check_constrt t (names, eqs) =
   391       if is_constrt thy t then (t, (names, eqs))
   392       else
   393         let val s = Name.variant names "x";
   394         in (Var ((s, 0), fastype_of t), (s::names, (s, t)::eqs)) end;
   395 
   396     fun compile_eq (s, t) gr =
   397       apfst (Pretty.block o cons (str (s ^ " = ")) o single)
   398         (invoke_codegen thy defs dep module false t gr);
   399 
   400     val (in_ts, out_ts) = get_args is 1 ts;
   401     val (in_ts', (all_vs', eqs)) = fold_map check_constrt in_ts (all_vs, []);
   402 
   403     fun compile_prems out_ts' vs names [] gr =
   404           let
   405             val (out_ps, gr2) =
   406               fold_map (invoke_codegen thy defs dep module false) out_ts gr;
   407             val (eq_ps, gr3) = fold_map compile_eq eqs gr2;
   408             val (out_ts'', (names', eqs')) = fold_map check_constrt out_ts' (names, []);
   409             val (out_ts''', nvs) =
   410               fold_map distinct_v out_ts'' (names', map (fn x => (x, [x])) vs);
   411             val (out_ps', gr4) =
   412               fold_map (invoke_codegen thy defs dep module false) out_ts''' gr3;
   413             val (eq_ps', gr5) = fold_map compile_eq eqs' gr4;
   414             val vs' = distinct (op =) (flat (vs :: map term_vs out_ts'));
   415             val missing_vs = missing_vars vs' out_ts;
   416             val final_p = Pretty.block
   417               [str "DSeq.single", Pretty.brk 1, mk_tuple out_ps]
   418           in
   419             if null missing_vs then
   420               (compile_match (snd nvs) (eq_ps @ eq_ps') out_ps'
   421                  final_p (exists (not o is_exhaustive) out_ts'''), gr5)
   422             else
   423               let
   424                 val (pat_p, gr6) = invoke_codegen thy defs dep module true
   425                   (HOLogic.mk_tuple (map Var missing_vs)) gr5;
   426                 val gen_p = mk_gen gr6 module true [] ""
   427                   (HOLogic.mk_tupleT (map snd missing_vs))
   428               in
   429                 (compile_match (snd nvs) eq_ps' out_ps'
   430                    (Pretty.block [str "DSeq.generator ", gen_p,
   431                       str " :->", Pretty.brk 1,
   432                       compile_match [] eq_ps [pat_p] final_p false])
   433                    (exists (not o is_exhaustive) out_ts'''),
   434                  gr6)
   435               end
   436           end
   437       | compile_prems out_ts vs names ps gr =
   438           let
   439             val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
   440             val (out_ts', (names', eqs)) = fold_map check_constrt out_ts (names, []);
   441             val (out_ts'', nvs) = fold_map distinct_v out_ts' (names', map (fn x => (x, [x])) vs);
   442             val (out_ps, gr0) = fold_map (invoke_codegen thy defs dep module false) out_ts'' gr;
   443             val (eq_ps, gr1) = fold_map compile_eq eqs gr0;
   444           in
   445             (case hd (select_mode_prem thy modes' vs' ps) of
   446                (p as Prem (us, t, is_set), (mode as Mode (_, js, _), []) :: _) =>
   447                  let
   448                    val ps' = filter_out (equal p) ps;
   449                    val (in_ts, out_ts''') = get_args js 1 us;
   450                    val (in_ps, gr2) = fold_map
   451                      (invoke_codegen thy defs dep module true) in_ts gr1;
   452                    val (ps, gr3) =
   453                      if not is_set then
   454                        apfst (fn ps => ps @
   455                            (if null in_ps then [] else [Pretty.brk 1]) @
   456                            separate (Pretty.brk 1) in_ps)
   457                          (compile_expr thy defs dep module false modes
   458                            (SOME mode, t) gr2)
   459                      else
   460                        apfst (fn p => Pretty.breaks [str "DSeq.of_list", str "(case", p,
   461                          str "of", str "Set", str "xs", str "=>", str "xs)"])
   462                          (*this is a very strong assumption about the generated code!*)
   463                            (invoke_codegen thy defs dep module true t gr2);
   464                    val (rest, gr4) = compile_prems out_ts''' vs' (fst nvs) ps' gr3;
   465                  in
   466                    (compile_match (snd nvs) eq_ps out_ps
   467                       (Pretty.block (ps @
   468                          [str " :->", Pretty.brk 1, rest]))
   469                       (exists (not o is_exhaustive) out_ts''), gr4)
   470                  end
   471              | (p as Sidecond t, [(_, [])]) =>
   472                  let
   473                    val ps' = filter_out (equal p) ps;
   474                    val (side_p, gr2) = invoke_codegen thy defs dep module true t gr1;
   475                    val (rest, gr3) = compile_prems [] vs' (fst nvs) ps' gr2;
   476                  in
   477                    (compile_match (snd nvs) eq_ps out_ps
   478                       (Pretty.block [str "?? ", side_p,
   479                         str " :->", Pretty.brk 1, rest])
   480                       (exists (not o is_exhaustive) out_ts''), gr3)
   481                  end
   482              | (_, (_, missing_vs) :: _) =>
   483                  let
   484                    val T = HOLogic.mk_tupleT (map snd missing_vs);
   485                    val (_, gr2) = invoke_tycodegen thy defs dep module false T gr1;
   486                    val gen_p = mk_gen gr2 module true [] "" T;
   487                    val (rest, gr3) = compile_prems
   488                      [HOLogic.mk_tuple (map Var missing_vs)] vs' (fst nvs) ps gr2
   489                  in
   490                    (compile_match (snd nvs) eq_ps out_ps
   491                       (Pretty.block [str "DSeq.generator", Pretty.brk 1,
   492                         gen_p, str " :->", Pretty.brk 1, rest])
   493                       (exists (not o is_exhaustive) out_ts''), gr3)
   494                  end)
   495           end;
   496 
   497     val (prem_p, gr') = compile_prems in_ts' arg_vs all_vs' ps gr ;
   498   in
   499     (Pretty.block [str "DSeq.single", Pretty.brk 1, inp,
   500        str " :->", Pretty.brk 1, prem_p], gr')
   501   end;
   502 
   503 fun compile_pred thy defs dep module prfx all_vs arg_vs modes s cls mode gr =
   504   let
   505     val xs = map str (Name.variant_list arg_vs
   506       (map (fn i => "x" ^ string_of_int i) (snd mode)));
   507     val ((cl_ps, mode_id), gr') = gr |>
   508       fold_map (fn cl => compile_clause thy defs
   509         dep module all_vs arg_vs modes mode cl (mk_tuple xs)) cls ||>>
   510       modename module s mode
   511   in
   512     (Pretty.block
   513       ([Pretty.block (separate (Pretty.brk 1)
   514          (str (prfx ^ mode_id) ::
   515            map str arg_vs @
   516            (case mode of ([], []) => [str "()"] | _ => xs)) @
   517          [str " ="]),
   518         Pretty.brk 1] @
   519        flat (separate [str " ++", Pretty.brk 1] (map single cl_ps))), (gr', "and "))
   520   end;
   521 
   522 fun compile_preds thy defs dep module all_vs arg_vs modes preds gr =
   523   let val (prs, (gr', _)) = fold_map (fn (s, cls) =>
   524     fold_map (fn (mode, _) => fn (gr', prfx') => compile_pred thy defs
   525       dep module prfx' all_vs arg_vs modes s cls mode gr')
   526         (((the o AList.lookup (op =) modes) s))) preds (gr, "fun ")
   527   in
   528     (space_implode "\n\n" (map string_of (flat prs)) ^ ";\n\n", gr')
   529   end;
   530 
   531 (**** processing of introduction rules ****)
   532 
   533 exception Modes of
   534   (string * ((int list option list * int list) * bool) list) list *
   535   (string * (int option list * int)) list;
   536 
   537 fun lookup_modes gr dep = apfst flat (apsnd flat (ListPair.unzip
   538   (map ((fn (SOME (Modes x), _, _) => x | _ => ([], [])) o get_node gr)
   539     (Graph.all_preds (fst gr) [dep]))));
   540 
   541 fun print_arities arities = message ("Arities:\n" ^
   542   cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^
   543     space_implode " -> " (map
   544       (fn NONE => "X" | SOME k' => string_of_int k')
   545         (ks @ [SOME k]))) arities));
   546 
   547 fun prep_intrs intrs = map (rename_term o #prop o rep_thm o Drule.standard) intrs;
   548 
   549 fun constrain cs [] = []
   550   | constrain cs ((s, xs) :: ys) =
   551       (s,
   552         case AList.lookup (op =) cs (s : string) of
   553           NONE => xs
   554         | SOME xs' => inter (op = o apfst fst) xs' xs) :: constrain cs ys;
   555 
   556 fun mk_extra_defs thy defs gr dep names module ts =
   557   fold (fn name => fn gr =>
   558     if name mem names then gr
   559     else
   560       (case get_clauses thy name of
   561         NONE => gr
   562       | SOME (names, thyname, nparms, intrs) =>
   563           mk_ind_def thy defs gr dep names (if_library thyname module)
   564             [] (prep_intrs intrs) nparms))
   565     (fold Term.add_const_names ts []) gr
   566 
   567 and mk_ind_def thy defs gr dep names module modecs intrs nparms =
   568   add_edge_acyclic (hd names, dep) gr handle
   569     Graph.CYCLES (xs :: _) =>
   570       error ("InductiveCodegen: illegal cyclic dependencies:\n" ^ commas xs)
   571   | Graph.UNDEF _ =>
   572     let
   573       val _ $ u = Logic.strip_imp_concl (hd intrs);
   574       val args = List.take (snd (strip_comb u), nparms);
   575       val arg_vs = maps term_vs args;
   576 
   577       fun get_nparms s = if s mem names then SOME nparms else
   578         Option.map #3 (get_clauses thy s);
   579 
   580       fun dest_prem (_ $ (Const ("op :", _) $ t $ u)) = Prem ([t], Envir.beta_eta_contract u, true)
   581         | dest_prem (_ $ ((eq as Const ("op =", _)) $ t $ u)) = Prem ([t, u], eq, false)
   582         | dest_prem (_ $ t) =
   583             (case strip_comb t of
   584                (v as Var _, ts) => if v mem args then Prem (ts, v, false) else Sidecond t
   585              | (c as Const (s, _), ts) => (case get_nparms s of
   586                  NONE => Sidecond t
   587                | SOME k =>
   588                    let val (ts1, ts2) = chop k ts
   589                    in Prem (ts2, list_comb (c, ts1), false) end)
   590              | _ => Sidecond t);
   591 
   592       fun add_clause intr (clauses, arities) =
   593         let
   594           val _ $ t = Logic.strip_imp_concl intr;
   595           val (Const (name, T), ts) = strip_comb t;
   596           val (ts1, ts2) = chop nparms ts;
   597           val prems = map dest_prem (Logic.strip_imp_prems intr);
   598           val (Ts, Us) = chop nparms (binder_types T)
   599         in
   600           (AList.update op = (name, these (AList.lookup op = clauses name) @
   601              [(ts2, prems)]) clauses,
   602            AList.update op = (name, (map (fn U => (case strip_type U of
   603                  (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
   604                | _ => NONE)) Ts,
   605              length Us)) arities)
   606         end;
   607 
   608       val gr' = mk_extra_defs thy defs
   609         (add_edge (hd names, dep)
   610           (new_node (hd names, (NONE, "", "")) gr)) (hd names) names module intrs;
   611       val (extra_modes, extra_arities) = lookup_modes gr' (hd names);
   612       val (clauses, arities) = fold add_clause intrs ([], []);
   613       val modes = constrain modecs
   614         (infer_modes thy extra_modes arities arg_vs clauses);
   615       val _ = print_arities arities;
   616       val _ = print_modes modes;
   617       val (s, gr'') = compile_preds thy defs (hd names) module (terms_vs intrs)
   618         arg_vs (modes @ extra_modes) clauses gr';
   619     in
   620       (map_node (hd names)
   621         (K (SOME (Modes (modes, arities)), module, s)) gr'')
   622     end;
   623 
   624 fun find_mode gr dep s u modes is = (case find_first (fn Mode (_, js, _) => is=js)
   625   (modes_of modes u handle Option => []) of
   626      NONE => codegen_error gr dep
   627        ("No such mode for " ^ s ^ ": " ^ string_of_mode ([], is))
   628    | mode => mode);
   629 
   630 fun mk_ind_call thy defs dep module is_query s T ts names thyname k intrs gr =
   631   let
   632     val (ts1, ts2) = chop k ts;
   633     val u = list_comb (Const (s, T), ts1);
   634 
   635     fun mk_mode (Const ("dummy_pattern", _)) ((ts, mode), i) = ((ts, mode), i + 1)
   636       | mk_mode t ((ts, mode), i) = ((ts @ [t], mode @ [i]), i + 1);
   637 
   638     val module' = if_library thyname module;
   639     val gr1 = mk_extra_defs thy defs
   640       (mk_ind_def thy defs gr dep names module'
   641       [] (prep_intrs intrs) k) dep names module' [u];
   642     val (modes, _) = lookup_modes gr1 dep;
   643     val (ts', is) =
   644       if is_query then fst (fold mk_mode ts2 (([], []), 1))
   645       else (ts2, 1 upto length (binder_types T) - k);
   646     val mode = find_mode gr1 dep s u modes is;
   647     val _ = if is_query orelse not (needs_random (the mode)) then ()
   648       else warning ("Illegal use of random data generators in " ^ s);
   649     val (in_ps, gr2) = fold_map (invoke_codegen thy defs dep module true) ts' gr1;
   650     val (ps, gr3) = compile_expr thy defs dep module false modes (mode, u) gr2;
   651   in
   652     (Pretty.block (ps @ (if null in_ps then [] else [Pretty.brk 1]) @
   653        separate (Pretty.brk 1) in_ps), gr3)
   654   end;
   655 
   656 fun clause_of_eqn eqn =
   657   let
   658     val (t, u) = HOLogic.dest_eq (HOLogic.dest_Trueprop (concl_of eqn));
   659     val (Const (s, T), ts) = strip_comb t;
   660     val (Ts, U) = strip_type T
   661   in
   662     rename_term (Logic.list_implies (prems_of eqn, HOLogic.mk_Trueprop
   663       (list_comb (Const (s ^ "_aux", Ts @ [U] ---> HOLogic.boolT), ts @ [u]))))
   664   end;
   665 
   666 fun mk_fun thy defs name eqns dep module module' gr =
   667   case try (get_node gr) name of
   668     NONE =>
   669     let
   670       val clauses = map clause_of_eqn eqns;
   671       val pname = name ^ "_aux";
   672       val arity = length (snd (strip_comb (fst (HOLogic.dest_eq
   673         (HOLogic.dest_Trueprop (concl_of (hd eqns)))))));
   674       val mode = 1 upto arity;
   675       val ((fun_id, mode_id), gr') = gr |>
   676         mk_const_id module' name ||>>
   677         modename module' pname ([], mode);
   678       val vars = map (fn i => str ("x" ^ string_of_int i)) mode;
   679       val s = string_of (Pretty.block
   680         [mk_app false (str ("fun " ^ snd fun_id)) vars, str " =",
   681          Pretty.brk 1, str "DSeq.hd", Pretty.brk 1,
   682          parens (Pretty.block (separate (Pretty.brk 1) (str mode_id ::
   683            vars)))]) ^ ";\n\n";
   684       val gr'' = mk_ind_def thy defs (add_edge (name, dep)
   685         (new_node (name, (NONE, module', s)) gr')) name [pname] module'
   686         [(pname, [([], mode)])] clauses 0;
   687       val (modes, _) = lookup_modes gr'' dep;
   688       val _ = find_mode gr'' dep pname (head_of (HOLogic.dest_Trueprop
   689         (Logic.strip_imp_concl (hd clauses)))) modes mode
   690     in (mk_qual_id module fun_id, gr'') end
   691   | SOME _ =>
   692       (mk_qual_id module (get_const_id gr name), add_edge (name, dep) gr);
   693 
   694 (* convert n-tuple to nested pairs *)
   695 
   696 fun conv_ntuple fs ts p =
   697   let
   698     val k = length fs;
   699     val xs = map_range (fn i => str ("x" ^ string_of_int i)) (k + 1);
   700     val xs' = map (fn Bound i => nth xs (k - i)) ts;
   701     fun conv xs js =
   702       if js mem fs then
   703         let
   704           val (p, xs') = conv xs (1::js);
   705           val (q, xs'') = conv xs' (2::js)
   706         in (mk_tuple [p, q], xs'') end
   707       else (hd xs, tl xs)
   708   in
   709     if k > 0 then
   710       Pretty.block
   711         [str "DSeq.map (fn", Pretty.brk 1,
   712          mk_tuple xs', str " =>", Pretty.brk 1, fst (conv xs []),
   713          str ")", Pretty.brk 1, parens p]
   714     else p
   715   end;
   716 
   717 fun inductive_codegen thy defs dep module brack t gr  = (case strip_comb t of
   718     (Const ("Collect", _), [u]) =>
   719       let val (r, Ts, fs) = HOLogic.strip_psplits u
   720       in case strip_comb r of
   721           (Const (s, T), ts) =>
   722             (case (get_clauses thy s, get_assoc_code thy (s, T)) of
   723               (SOME (names, thyname, k, intrs), NONE) =>
   724                 let
   725                   val (ts1, ts2) = chop k ts;
   726                   val ts2' = map
   727                     (fn Bound i => Term.dummy_pattern (nth Ts (length Ts - i - 1)) | t => t) ts2;
   728                   val (ots, its) = List.partition is_Bound ts2;
   729                   val no_loose = forall (fn t => not (loose_bvar (t, 0)))
   730                 in
   731                   if null (duplicates op = ots) andalso
   732                     no_loose ts1 andalso no_loose its
   733                   then
   734                     let val (call_p, gr') = mk_ind_call thy defs dep module true
   735                       s T (ts1 @ ts2') names thyname k intrs gr 
   736                     in SOME ((if brack then parens else I) (Pretty.block
   737                       [str "Set", Pretty.brk 1, str "(DSeq.list_of", Pretty.brk 1, str "(",
   738                        conv_ntuple fs ots call_p, str "))"]),
   739                        (*this is a very strong assumption about the generated code!*)
   740                        gr')
   741                     end
   742                   else NONE
   743                 end
   744             | _ => NONE)
   745         | _ => NONE
   746       end
   747   | (Const (s, T), ts) => (case Symtab.lookup (#eqns (CodegenData.get thy)) s of
   748       NONE => (case (get_clauses thy s, get_assoc_code thy (s, T)) of
   749         (SOME (names, thyname, k, intrs), NONE) =>
   750           if length ts < k then NONE else SOME
   751             (let val (call_p, gr') = mk_ind_call thy defs dep module false
   752                s T (map Term.no_dummy_patterns ts) names thyname k intrs gr
   753              in (mk_funcomp brack "?!"
   754                (length (binder_types T) - length ts) (parens call_p), gr')
   755              end handle TERM _ => mk_ind_call thy defs dep module true
   756                s T ts names thyname k intrs gr )
   757       | _ => NONE)
   758     | SOME eqns =>
   759         let
   760           val (_, thyname) :: _ = eqns;
   761           val (id, gr') = mk_fun thy defs s (preprocess thy (map fst (rev eqns)))
   762             dep module (if_library thyname module) gr;
   763           val (ps, gr'') = fold_map
   764             (invoke_codegen thy defs dep module true) ts gr';
   765         in SOME (mk_app brack (str id) ps, gr'')
   766         end)
   767   | _ => NONE);
   768 
   769 val setup =
   770   add_codegen "inductive" inductive_codegen #>
   771   Attrib.setup @{binding code_ind}
   772     (Scan.lift (Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) --
   773       Scan.option (Args.$$$ "params" |-- Args.colon |-- OuterParse.nat) >> uncurry add))
   774     "introduction rules for executable predicates";
   775 
   776 (**** Quickcheck involving inductive predicates ****)
   777 
   778 val test_fn : (int * int * int -> term list option) Unsynchronized.ref =
   779   Unsynchronized.ref (fn _ => NONE);
   780 
   781 fun strip_imp p =
   782   let val (q, r) = HOLogic.dest_imp p
   783   in strip_imp r |>> cons q end
   784   handle TERM _ => ([], p);
   785 
   786 fun deepen bound f i =
   787   if i > bound then NONE
   788   else (case f i of
   789       NONE => deepen bound f (i + 1)
   790     | SOME x => SOME x);
   791 
   792 val depth_bound_value =
   793   Config.declare false "ind_quickcheck_depth" (Config.Int 10);
   794 val depth_bound = Config.int depth_bound_value;
   795 
   796 val depth_start_value =
   797   Config.declare false "ind_quickcheck_depth_start" (Config.Int 1);
   798 val depth_start = Config.int depth_start_value;
   799 
   800 val random_values_value =
   801   Config.declare false "ind_quickcheck_random" (Config.Int 5);
   802 val random_values = Config.int random_values_value;
   803 
   804 val size_offset_value =
   805   Config.declare false "ind_quickcheck_size_offset" (Config.Int 0);
   806 val size_offset = Config.int size_offset_value;
   807 
   808 fun test_term ctxt t =
   809   let
   810     val thy = ProofContext.theory_of ctxt;
   811     val (xs, p) = strip_abs t;
   812     val args' = map_index (fn (i, (_, T)) => ("arg" ^ string_of_int i, T)) xs;
   813     val args = map Free args';
   814     val (ps, q) = strip_imp p;
   815     val Ts = map snd xs;
   816     val T = Ts ---> HOLogic.boolT;
   817     val rl = Logic.list_implies
   818       (map (HOLogic.mk_Trueprop o curry subst_bounds (rev args)) ps @
   819        [HOLogic.mk_Trueprop (HOLogic.mk_not (subst_bounds (rev args, q)))],
   820        HOLogic.mk_Trueprop (list_comb (Free ("quickcheckp", T), args)));
   821     val (_, thy') = Inductive.add_inductive_global
   822       {quiet_mode=true, verbose=false, alt_name=Binding.empty, coind=false,
   823        no_elim=true, no_ind=false, skip_mono=false, fork_mono=false}
   824       [((Binding.name "quickcheckp", T), NoSyn)] []
   825       [(Attrib.empty_binding, rl)] [] (Theory.copy thy);
   826     val pred = HOLogic.mk_Trueprop (list_comb
   827       (Const (Sign.intern_const thy' "quickcheckp", T),
   828        map Term.dummy_pattern Ts));
   829     val (code, gr) = setmp_CRITICAL mode ["term_of", "test", "random_ind"]
   830       (generate_code_i thy' [] "Generated") [("testf", pred)];
   831     val s = "structure TestTerm =\nstruct\n\n" ^
   832       cat_lines (map snd code) ^
   833       "\nopen Generated;\n\n" ^ string_of
   834         (Pretty.block [str "val () = InductiveCodegen.test_fn :=",
   835           Pretty.brk 1, str "(fn p =>", Pretty.brk 1,
   836           str "case Seq.pull (testf p) of", Pretty.brk 1,
   837           str "SOME ", mk_tuple [mk_tuple (map (str o fst) args'), str "_"],
   838           str " =>", Pretty.brk 1, str "SOME ",
   839           Pretty.block (str "[" ::
   840             Pretty.commas (map (fn (s, T) => Pretty.block
   841               [mk_term_of gr "Generated" false T, Pretty.brk 1, str s]) args') @
   842             [str "]"]), Pretty.brk 1,
   843           str "| NONE => NONE);"]) ^
   844       "\n\nend;\n";
   845     val _ = ML_Context.eval_in (SOME ctxt) false Position.none s;
   846     val values = Config.get ctxt random_values;
   847     val bound = Config.get ctxt depth_bound;
   848     val start = Config.get ctxt depth_start;
   849     val offset = Config.get ctxt size_offset;
   850     val test_fn' = !test_fn;
   851     fun test k = deepen bound (fn i =>
   852       (priority ("Search depth: " ^ string_of_int i);
   853        test_fn' (i, values, k+offset))) start;
   854   in test end;
   855 
   856 val quickcheck_setup =
   857   Attrib.register_config depth_bound_value #>
   858   Attrib.register_config depth_start_value #>
   859   Attrib.register_config random_values_value #>
   860   Attrib.register_config size_offset_value #>
   861   Quickcheck.add_generator ("SML_inductive", test_term);
   862 
   863 end;