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