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