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