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