src/HOL/Tools/inductive_codegen.ML
author berghofe
Sat Mar 31 12:40:55 2007 +0200 (2007-03-31)
changeset 22556 b067fdca022d
parent 22360 26ead7ed4f4b
child 22642 bfdb29f11eb4
permissions -rw-r--r--
Fixed bug in dest_prem: premises of the form "p x_1 ... x_n"
(where p is a variable) are now treated as side conditions
if p is not in the list of (global) parameters.
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@22271
    10
  val add : string option -> int option -> attribute
wenzelm@18708
    11
  val setup : theory -> theory
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@22271
    19
(* read off parameters of inductive predicate from raw induction rule *)
berghofe@22271
    20
fun params_of induct =
berghofe@22271
    21
  let
berghofe@22271
    22
    val (_ $ t $ u :: _) =
berghofe@22271
    23
      HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of induct));
berghofe@22271
    24
    val (_, ts) = strip_comb t;
berghofe@22271
    25
    val (_, us) = strip_comb u
berghofe@22271
    26
  in
berghofe@22271
    27
    List.take (ts, length ts - length us)
berghofe@22271
    28
  end;
berghofe@22271
    29
berghofe@12557
    30
(**** theory data ****)
berghofe@12557
    31
wenzelm@18930
    32
fun merge_rules tabs =
wenzelm@18930
    33
  Symtab.join (fn _ => fn (ths, ths') =>
wenzelm@22360
    34
    gen_merge_lists (Thm.eq_thm_prop o pairself fst) ths ths') tabs;
wenzelm@18930
    35
wenzelm@16424
    36
structure CodegenData = TheoryDataFun
wenzelm@16424
    37
(struct
berghofe@12557
    38
  val name = "HOL/inductive_codegen";
berghofe@15031
    39
  type T =
berghofe@22271
    40
    {intros : (thm * (string * int)) list Symtab.table,
berghofe@15031
    41
     graph : unit Graph.T,
berghofe@16645
    42
     eqns : (thm * string) list Symtab.table};
berghofe@15031
    43
  val empty =
berghofe@15031
    44
    {intros = Symtab.empty, graph = Graph.empty, eqns = Symtab.empty};
berghofe@12557
    45
  val copy = I;
wenzelm@16424
    46
  val extend = I;
wenzelm@16424
    47
  fun merge _ ({intros=intros1, graph=graph1, eqns=eqns1},
berghofe@15031
    48
    {intros=intros2, graph=graph2, eqns=eqns2}) =
wenzelm@18930
    49
    {intros = merge_rules (intros1, intros2),
berghofe@15031
    50
     graph = Graph.merge (K true) (graph1, graph2),
wenzelm@18930
    51
     eqns = merge_rules (eqns1, eqns2)};
berghofe@12557
    52
  fun print _ _ = ();
wenzelm@16424
    53
end);
berghofe@12557
    54
berghofe@12557
    55
berghofe@12557
    56
fun warn thm = warning ("InductiveCodegen: Not a proper clause:\n" ^
berghofe@12557
    57
  string_of_thm thm);
berghofe@11537
    58
berghofe@14162
    59
fun add_node (g, x) = Graph.new_node (x, ()) g handle Graph.DUP _ => g;
berghofe@14162
    60
berghofe@22271
    61
fun add optmod optnparms = Thm.declaration_attribute (fn thm => Context.mapping (fn thy =>
berghofe@16645
    62
  let
berghofe@16645
    63
    val {intros, graph, eqns} = CodegenData.get thy;
berghofe@16645
    64
    fun thyname_of s = (case optmod of
berghofe@16645
    65
      NONE => thyname_of_const s thy | SOME s => s);
berghofe@22271
    66
  in (case Option.map strip_comb (try HOLogic.dest_Trueprop (concl_of thm)) of
berghofe@22271
    67
      SOME (Const ("op =", _), [t, _]) => (case head_of t of
berghofe@15031
    68
        Const (s, _) =>
wenzelm@18728
    69
          CodegenData.put {intros = intros, graph = graph,
wenzelm@17412
    70
             eqns = eqns |> Symtab.update
wenzelm@18930
    71
               (s, Symtab.lookup_list eqns s @ [(thm, thyname_of s)])} thy
wenzelm@18728
    72
      | _ => (warn thm; thy))
berghofe@22271
    73
    | SOME (Const (s, _), _) =>
berghofe@22271
    74
        let
berghofe@22271
    75
          val cs = foldr add_term_consts [] (prems_of thm);
berghofe@22271
    76
          val rules = Symtab.lookup_list intros s;
berghofe@22271
    77
          val nparms = (case optnparms of
berghofe@22271
    78
            SOME k => k
berghofe@22271
    79
          | NONE => (case rules of
berghofe@22271
    80
             [] => (case try (InductivePackage.the_inductive (ProofContext.init thy)) s of
berghofe@22271
    81
                 SOME (_, {raw_induct, ...}) => length (params_of raw_induct)
berghofe@22271
    82
               | NONE => 0)
berghofe@22271
    83
            | xs => snd (snd (snd (split_last xs)))))
berghofe@22271
    84
        in CodegenData.put
berghofe@22271
    85
          {intros = intros |>
berghofe@22271
    86
           Symtab.update (s, rules @ [(thm, (thyname_of s, nparms))]),
berghofe@22271
    87
           graph = foldr (uncurry (Graph.add_edge o pair s))
berghofe@22271
    88
             (Library.foldl add_node (graph, s :: cs)) cs,
berghofe@22271
    89
           eqns = eqns} thy
berghofe@22271
    90
        end
wenzelm@18728
    91
    | _ => (warn thm; thy))
wenzelm@20897
    92
  end) I);
berghofe@12557
    93
berghofe@12557
    94
fun get_clauses thy s =
berghofe@15031
    95
  let val {intros, graph, ...} = CodegenData.get thy
wenzelm@17412
    96
  in case Symtab.lookup intros s of
berghofe@22271
    97
      NONE => (case try (InductivePackage.the_inductive (ProofContext.init thy)) s of
skalberg@15531
    98
        NONE => NONE
berghofe@22271
    99
      | SOME ({names, ...}, {intrs, raw_induct, ...}) =>
berghofe@22271
   100
          SOME (names, thyname_of_const s thy, length (params_of raw_induct),
berghofe@16645
   101
            preprocess thy intrs))
skalberg@15531
   102
    | SOME _ =>
berghofe@16645
   103
        let
berghofe@16645
   104
          val SOME names = find_first
berghofe@16645
   105
            (fn xs => s mem xs) (Graph.strong_conn graph);
berghofe@16645
   106
          val intrs = List.concat (map
wenzelm@17412
   107
            (fn s => the (Symtab.lookup intros s)) names);
berghofe@22271
   108
          val (_, (_, (thyname, nparms))) = split_last intrs
berghofe@22271
   109
        in SOME (names, thyname, nparms, preprocess thy (map fst intrs)) end
berghofe@14162
   110
  end;
berghofe@12557
   111
berghofe@12557
   112
berghofe@11537
   113
(**** check if a term contains only constructor functions ****)
berghofe@11537
   114
berghofe@11537
   115
fun is_constrt thy =
berghofe@11537
   116
  let
skalberg@15570
   117
    val cnstrs = List.concat (List.concat (map
berghofe@11537
   118
      (map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd)
berghofe@11537
   119
      (Symtab.dest (DatatypePackage.get_datatypes thy))));
berghofe@11537
   120
    fun check t = (case strip_comb t of
berghofe@11537
   121
        (Var _, []) => true
haftmann@17521
   122
      | (Const (s, _), ts) => (case AList.lookup (op =) cnstrs s of
skalberg@15531
   123
            NONE => false
skalberg@15531
   124
          | SOME i => length ts = i andalso forall check ts)
berghofe@11537
   125
      | _ => false)
berghofe@11537
   126
  in check end;
berghofe@11537
   127
berghofe@11537
   128
(**** check if a type is an equality type (i.e. doesn't contain fun) ****)
berghofe@11537
   129
berghofe@11537
   130
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts
berghofe@11537
   131
  | is_eqT _ = true;
berghofe@11537
   132
berghofe@11537
   133
(**** mode inference ****)
berghofe@11537
   134
berghofe@15204
   135
fun string_of_mode (iss, is) = space_implode " -> " (map
skalberg@15531
   136
  (fn NONE => "X"
skalberg@15531
   137
    | SOME js => enclose "[" "]" (commas (map string_of_int js)))
skalberg@15531
   138
       (iss @ [SOME is]));
berghofe@15204
   139
berghofe@15204
   140
fun print_modes modes = message ("Inferred modes:\n" ^
berghofe@15204
   141
  space_implode "\n" (map (fn (s, ms) => s ^ ": " ^ commas (map
berghofe@15204
   142
    string_of_mode ms)) modes));
berghofe@15204
   143
berghofe@11537
   144
val term_vs = map (fst o fst o dest_Var) o term_vars;
wenzelm@19046
   145
val terms_vs = distinct (op =) o List.concat o (map term_vs);
berghofe@11537
   146
berghofe@11537
   147
(** collect all Vars in a term (with duplicates!) **)
wenzelm@16861
   148
fun term_vTs tm =
wenzelm@16861
   149
  fold_aterms (fn Var ((x, _), T) => cons (x, T) | _ => I) tm [];
berghofe@11537
   150
berghofe@11537
   151
fun get_args _ _ [] = ([], [])
berghofe@11537
   152
  | get_args is i (x::xs) = (if i mem is then apfst else apsnd) (cons x)
berghofe@11537
   153
      (get_args is (i+1) xs);
berghofe@11537
   154
berghofe@11537
   155
fun merge xs [] = xs
berghofe@11537
   156
  | merge [] ys = ys
berghofe@11537
   157
  | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys)
berghofe@11537
   158
      else y::merge (x::xs) ys;
berghofe@11537
   159
berghofe@11537
   160
fun subsets i j = if i <= j then
berghofe@11537
   161
       let val is = subsets (i+1) j
berghofe@11537
   162
       in merge (map (fn ks => i::ks) is) is end
berghofe@11537
   163
     else [[]];
berghofe@11537
   164
berghofe@12557
   165
fun cprod ([], ys) = []
berghofe@12557
   166
  | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys);
berghofe@12557
   167
skalberg@15574
   168
fun cprods xss = foldr (map op :: o cprod) [[]] xss;
berghofe@12557
   169
berghofe@22271
   170
datatype mode = Mode of (int list option list * int list) * int list * mode option list;
berghofe@12557
   171
berghofe@12557
   172
fun modes_of modes t =
berghofe@12557
   173
  let
berghofe@22271
   174
    val ks = 1 upto length (binder_types (fastype_of t));
berghofe@22271
   175
    val default = [Mode (([], ks), ks, [])];
berghofe@22271
   176
    fun mk_modes name args = Option.map (List.concat o
berghofe@22271
   177
      map (fn (m as (iss, is)) =>
berghofe@22271
   178
        let
berghofe@22271
   179
          val (args1, args2) =
berghofe@22271
   180
            if length args < length iss then
berghofe@22271
   181
              error ("Too few arguments for inductive predicate " ^ name)
berghofe@22271
   182
            else chop (length iss) args;
berghofe@22271
   183
          val k = length args2;
berghofe@22271
   184
          val prfx = 1 upto k
berghofe@22271
   185
        in
berghofe@22271
   186
          if not (is_prefix op = prfx is) then [] else
berghofe@22271
   187
          let val is' = map (fn i => i - k) (List.drop (is, k))
berghofe@22271
   188
          in map (fn x => Mode (m, is', x)) (cprods (map
berghofe@22271
   189
            (fn (NONE, _) => [NONE]
berghofe@22271
   190
              | (SOME js, arg) => map SOME (List.filter
berghofe@22271
   191
                  (fn Mode (_, js', _) => js=js') (modes_of modes arg)))
berghofe@22271
   192
                    (iss ~~ args1)))
berghofe@22271
   193
          end
berghofe@22271
   194
        end)) (AList.lookup op = modes name)
berghofe@12557
   195
berghofe@12557
   196
  in (case strip_comb t of
berghofe@14163
   197
      (Const ("op =", Type (_, [T, _])), _) =>
berghofe@22271
   198
        [Mode (([], [1]), [1], []), Mode (([], [2]), [2], [])] @
berghofe@22271
   199
        (if is_eqT T then [Mode (([], [1, 2]), [1, 2], [])] else [])
berghofe@22271
   200
    | (Const (name, _), args) => the_default default (mk_modes name args)
berghofe@22271
   201
    | (Var ((name, _), _), args) => the (mk_modes name args)
berghofe@22271
   202
    | (Free (name, _), args) => the (mk_modes name args)
berghofe@22271
   203
    | _ => default)
berghofe@12557
   204
  end;
berghofe@12557
   205
berghofe@12557
   206
datatype indprem = Prem of term list * term | Sidecond of term;
berghofe@12557
   207
berghofe@11537
   208
fun select_mode_prem thy modes vs ps =
wenzelm@19861
   209
  find_first (is_some o snd) (ps ~~ map
berghofe@22271
   210
    (fn Prem (us, t) => find_first (fn Mode (_, is, _) =>
berghofe@11537
   211
          let
berghofe@15031
   212
            val (in_ts, out_ts) = get_args is 1 us;
skalberg@15570
   213
            val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts;
skalberg@15570
   214
            val vTs = List.concat (map term_vTs out_ts');
wenzelm@18964
   215
            val dupTs = map snd (duplicates (op =) vTs) @
haftmann@17521
   216
              List.mapPartial (AList.lookup (op =) vTs) vs;
berghofe@11537
   217
          in
berghofe@15031
   218
            terms_vs (in_ts @ in_ts') subset vs andalso
berghofe@15031
   219
            forall (is_eqT o fastype_of) in_ts' andalso
berghofe@12557
   220
            term_vs t subset vs andalso
berghofe@11537
   221
            forall is_eqT dupTs
berghofe@11537
   222
          end)
berghofe@22271
   223
            (modes_of modes t handle Option => [Mode (([], []), [], [])])
berghofe@22271
   224
      | Sidecond t => if term_vs t subset vs then SOME (Mode (([], []), [], []))
skalberg@15531
   225
          else NONE) ps);
berghofe@11537
   226
berghofe@12557
   227
fun check_mode_clause thy arg_vs modes (iss, is) (ts, ps) =
berghofe@11537
   228
  let
skalberg@15570
   229
    val modes' = modes @ List.mapPartial
skalberg@15531
   230
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
berghofe@12557
   231
        (arg_vs ~~ iss);
skalberg@15531
   232
    fun check_mode_prems vs [] = SOME vs
berghofe@12557
   233
      | check_mode_prems vs ps = (case select_mode_prem thy modes' vs ps of
skalberg@15531
   234
          NONE => NONE
skalberg@15531
   235
        | SOME (x, _) => check_mode_prems
berghofe@12557
   236
            (case x of Prem (us, _) => vs union terms_vs us | _ => vs)
berghofe@11537
   237
            (filter_out (equal x) ps));
skalberg@15570
   238
    val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (get_args is 1 ts));
berghofe@11537
   239
    val in_vs = terms_vs in_ts;
berghofe@11537
   240
    val concl_vs = terms_vs ts
berghofe@11537
   241
  in
wenzelm@18964
   242
    forall is_eqT (map snd (duplicates (op =) (List.concat (map term_vTs in_ts)))) andalso
berghofe@15204
   243
    forall (is_eqT o fastype_of) in_ts' andalso
berghofe@11537
   244
    (case check_mode_prems (arg_vs union in_vs) ps of
skalberg@15531
   245
       NONE => false
skalberg@15531
   246
     | SOME vs => concl_vs subset vs)
berghofe@11537
   247
  end;
berghofe@11537
   248
berghofe@11537
   249
fun check_modes_pred thy arg_vs preds modes (p, ms) =
haftmann@17521
   250
  let val SOME rs = AList.lookup (op =) preds p
skalberg@15570
   251
  in (p, List.filter (fn m => case find_index
berghofe@15204
   252
    (not o check_mode_clause thy arg_vs modes m) rs of
berghofe@15204
   253
      ~1 => true
berghofe@15204
   254
    | i => (message ("Clause " ^ string_of_int (i+1) ^ " of " ^
berghofe@15204
   255
      p ^ " violates mode " ^ string_of_mode m); false)) ms)
berghofe@15204
   256
  end;
berghofe@11537
   257
berghofe@11537
   258
fun fixp f x =
berghofe@11537
   259
  let val y = f x
berghofe@11537
   260
  in if x = y then x else fixp f y end;
berghofe@11537
   261
berghofe@22271
   262
fun infer_modes thy extra_modes arities arg_vs preds = fixp (fn modes =>
berghofe@11537
   263
  map (check_modes_pred thy arg_vs preds (modes @ extra_modes)) modes)
berghofe@22271
   264
    (map (fn (s, (ks, k)) => (s, cprod (cprods (map
skalberg@15531
   265
      (fn NONE => [NONE]
berghofe@22271
   266
        | SOME k' => map SOME (subsets 1 k')) ks),
berghofe@22271
   267
      subsets 1 k))) arities);
berghofe@11537
   268
berghofe@11537
   269
(**** code generation ****)
berghofe@11537
   270
berghofe@11537
   271
fun mk_eq (x::xs) =
berghofe@11537
   272
  let fun mk_eqs _ [] = []
berghofe@11537
   273
        | mk_eqs a (b::cs) = Pretty.str (a ^ " = " ^ b) :: mk_eqs b cs
berghofe@11537
   274
  in mk_eqs x xs end;
berghofe@11537
   275
berghofe@11537
   276
fun mk_tuple xs = Pretty.block (Pretty.str "(" ::
skalberg@15570
   277
  List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @
berghofe@11537
   278
  [Pretty.str ")"]);
berghofe@11537
   279
haftmann@17521
   280
fun mk_v ((names, vs), s) = (case AList.lookup (op =) vs s of
skalberg@15531
   281
      NONE => ((names, (s, [s])::vs), s)
wenzelm@20071
   282
    | SOME xs => let val s' = Name.variant names s in
haftmann@17521
   283
        ((s'::names, AList.update (op =) (s, s'::xs) vs), s') end);
berghofe@11537
   284
berghofe@11537
   285
fun distinct_v (nvs, Var ((s, 0), T)) =
berghofe@11537
   286
      apsnd (Var o rpair T o rpair 0) (mk_v (nvs, s))
berghofe@11537
   287
  | distinct_v (nvs, t $ u) =
berghofe@11537
   288
      let
berghofe@11537
   289
        val (nvs', t') = distinct_v (nvs, t);
berghofe@11537
   290
        val (nvs'', u') = distinct_v (nvs', u);
berghofe@11537
   291
      in (nvs'', t' $ u') end
berghofe@11537
   292
  | distinct_v x = x;
berghofe@11537
   293
berghofe@15061
   294
fun is_exhaustive (Var _) = true
berghofe@15061
   295
  | is_exhaustive (Const ("Pair", _) $ t $ u) =
berghofe@15061
   296
      is_exhaustive t andalso is_exhaustive u
berghofe@15061
   297
  | is_exhaustive _ = false;
berghofe@15061
   298
berghofe@15061
   299
fun compile_match nvs eq_ps out_ps success_p can_fail =
skalberg@15570
   300
  let val eqs = List.concat (separate [Pretty.str " andalso", Pretty.brk 1]
skalberg@15570
   301
    (map single (List.concat (map (mk_eq o snd) nvs) @ eq_ps)));
berghofe@11537
   302
  in
berghofe@11537
   303
    Pretty.block
berghofe@11537
   304
     ([Pretty.str "(fn ", mk_tuple out_ps, Pretty.str " =>", Pretty.brk 1] @
berghofe@11537
   305
      (Pretty.block ((if eqs=[] then [] else Pretty.str "if " ::
berghofe@11537
   306
         [Pretty.block eqs, Pretty.brk 1, Pretty.str "then "]) @
berghofe@11537
   307
         (success_p ::
berghofe@15061
   308
          (if eqs=[] then [] else [Pretty.brk 1, Pretty.str "else Seq.empty"]))) ::
berghofe@15061
   309
       (if can_fail then
berghofe@15061
   310
          [Pretty.brk 1, Pretty.str "| _ => Seq.empty)"]
berghofe@15061
   311
        else [Pretty.str ")"])))
berghofe@11537
   312
  end;
berghofe@11537
   313
berghofe@17144
   314
fun modename module s (iss, is) gr =
berghofe@17144
   315
  let val (gr', id) = if s = "op =" then (gr, ("", "equal"))
berghofe@17144
   316
    else mk_const_id module s gr
berghofe@17144
   317
  in (gr', space_implode "__"
berghofe@17144
   318
    (mk_qual_id module id ::
berghofe@17144
   319
      map (space_implode "_" o map string_of_int) (List.mapPartial I iss @ [is])))
berghofe@17144
   320
  end;
berghofe@11537
   321
berghofe@22271
   322
fun mk_funcomp brack s k p = (if brack then parens else I)
berghofe@22271
   323
  (Pretty.block [Pretty.block ((if k = 0 then [] else [Pretty.str "("]) @
berghofe@22271
   324
    separate (Pretty.brk 1) (Pretty.str s :: replicate k (Pretty.str "|> ???")) @
berghofe@22271
   325
    (if k = 0 then [] else [Pretty.str ")"])), Pretty.brk 1, p]);
berghofe@22271
   326
berghofe@22271
   327
fun compile_expr thy defs dep module brack modes (gr, (NONE, t)) =
berghofe@17144
   328
      apsnd single (invoke_codegen thy defs dep module brack (gr, t))
berghofe@22271
   329
  | compile_expr _ _ _ _ _ _ (gr, (SOME _, Var ((name, _), _))) =
berghofe@12557
   330
      (gr, [Pretty.str name])
berghofe@22271
   331
  | compile_expr thy defs dep module brack modes (gr, (SOME (Mode (mode, _, ms)), t)) =
berghofe@22271
   332
      (case strip_comb t of
berghofe@22271
   333
         (Const (name, _), args) =>
berghofe@22271
   334
           if name = "op =" orelse AList.defined op = modes name then
berghofe@22271
   335
             let
berghofe@22271
   336
               val (args1, args2) = chop (length ms) args;
berghofe@22271
   337
               val (gr', (ps, mode_id)) = foldl_map
berghofe@22271
   338
                   (compile_expr thy defs dep module true modes) (gr, ms ~~ args1) |>>>
berghofe@22271
   339
                 modename module name mode;
berghofe@22271
   340
               val (gr'', ps') = foldl_map
berghofe@22271
   341
                 (invoke_codegen thy defs dep module true) (gr', args2)
berghofe@22271
   342
             in (gr', (if brack andalso not (null ps andalso null ps') then
berghofe@22271
   343
               single o parens o Pretty.block else I)
berghofe@22271
   344
                 (List.concat (separate [Pretty.brk 1]
berghofe@22271
   345
                   ([Pretty.str mode_id] :: ps @ map single ps'))))
berghofe@22271
   346
             end
berghofe@22271
   347
           else apsnd (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
berghofe@22271
   348
             (invoke_codegen thy defs dep module true (gr, t))
berghofe@22271
   349
       | _ => apsnd (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
berghofe@22271
   350
           (invoke_codegen thy defs dep module true (gr, t)));
berghofe@12557
   351
berghofe@22271
   352
fun compile_clause thy defs gr dep module all_vs arg_vs modes (iss, is) (ts, ps) inp =
berghofe@11537
   353
  let
skalberg@15570
   354
    val modes' = modes @ List.mapPartial
skalberg@15531
   355
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
berghofe@12557
   356
        (arg_vs ~~ iss);
berghofe@12557
   357
berghofe@11537
   358
    fun check_constrt ((names, eqs), t) =
berghofe@11537
   359
      if is_constrt thy t then ((names, eqs), t) else
wenzelm@20071
   360
        let val s = Name.variant names "x";
berghofe@11537
   361
        in ((s::names, (s, t)::eqs), Var ((s, 0), fastype_of t)) end;
berghofe@11537
   362
berghofe@15031
   363
    fun compile_eq (gr, (s, t)) =
berghofe@15031
   364
      apsnd (Pretty.block o cons (Pretty.str (s ^ " = ")) o single)
berghofe@17144
   365
        (invoke_codegen thy defs dep module false (gr, t));
berghofe@15031
   366
berghofe@12557
   367
    val (in_ts, out_ts) = get_args is 1 ts;
berghofe@11537
   368
    val ((all_vs', eqs), in_ts') =
berghofe@11537
   369
      foldl_map check_constrt ((all_vs, []), in_ts);
berghofe@11537
   370
berghofe@15031
   371
    fun is_ind t = (case head_of t of
haftmann@17521
   372
          Const (s, _) => s = "op =" orelse AList.defined (op =) modes s
berghofe@15031
   373
        | Var ((s, _), _) => s mem arg_vs);
berghofe@15031
   374
berghofe@11537
   375
    fun compile_prems out_ts' vs names gr [] =
berghofe@11537
   376
          let
berghofe@12453
   377
            val (gr2, out_ps) = foldl_map
berghofe@17144
   378
              (invoke_codegen thy defs dep module false) (gr, out_ts);
berghofe@15031
   379
            val (gr3, eq_ps) = foldl_map compile_eq (gr2, eqs);
berghofe@15031
   380
            val ((names', eqs'), out_ts'') =
berghofe@15031
   381
              foldl_map check_constrt ((names, []), out_ts');
berghofe@15031
   382
            val (nvs, out_ts''') = foldl_map distinct_v
berghofe@15031
   383
              ((names', map (fn x => (x, [x])) vs), out_ts'');
berghofe@12453
   384
            val (gr4, out_ps') = foldl_map
berghofe@17144
   385
              (invoke_codegen thy defs dep module false) (gr3, out_ts''');
berghofe@15031
   386
            val (gr5, eq_ps') = foldl_map compile_eq (gr4, eqs')
berghofe@11537
   387
          in
berghofe@15031
   388
            (gr5, compile_match (snd nvs) (eq_ps @ eq_ps') out_ps'
berghofe@11537
   389
              (Pretty.block [Pretty.str "Seq.single", Pretty.brk 1, mk_tuple out_ps])
berghofe@15061
   390
              (exists (not o is_exhaustive) out_ts'''))
berghofe@11537
   391
          end
berghofe@11537
   392
      | compile_prems out_ts vs names gr ps =
berghofe@11537
   393
          let
wenzelm@19046
   394
            val vs' = distinct (op =) (List.concat (vs :: map term_vs out_ts));
berghofe@22271
   395
            val SOME (p, mode as SOME (Mode (_, js, _))) =
berghofe@15126
   396
              select_mode_prem thy modes' vs' ps;
berghofe@11537
   397
            val ps' = filter_out (equal p) ps;
berghofe@15031
   398
            val ((names', eqs), out_ts') =
berghofe@15031
   399
              foldl_map check_constrt ((names, []), out_ts);
berghofe@15031
   400
            val (nvs, out_ts'') = foldl_map distinct_v
berghofe@15031
   401
              ((names', map (fn x => (x, [x])) vs), out_ts');
berghofe@15031
   402
            val (gr0, out_ps) = foldl_map
berghofe@17144
   403
              (invoke_codegen thy defs dep module false) (gr, out_ts'');
berghofe@15031
   404
            val (gr1, eq_ps) = foldl_map compile_eq (gr0, eqs)
berghofe@11537
   405
          in
berghofe@11537
   406
            (case p of
berghofe@12557
   407
               Prem (us, t) =>
berghofe@11537
   408
                 let
berghofe@15031
   409
                   val (in_ts, out_ts''') = get_args js 1 us;
berghofe@15031
   410
                   val (gr2, in_ps) = foldl_map
berghofe@22271
   411
                     (invoke_codegen thy defs dep module true) (gr1, in_ts);
berghofe@15031
   412
                   val (gr3, ps) = if is_ind t then
berghofe@22271
   413
                       apsnd (fn ps => ps @ Pretty.brk 1 ::
berghofe@22271
   414
                           separate (Pretty.brk 1) in_ps)
berghofe@22271
   415
                         (compile_expr thy defs dep module false modes
berghofe@16645
   416
                           (gr2, (mode, t)))
berghofe@15031
   417
                     else
berghofe@22271
   418
                       apsnd (fn p => [Pretty.str "Seq.of_list", Pretty.brk 1, p])
berghofe@17144
   419
                           (invoke_codegen thy defs dep module true (gr2, t));
berghofe@15031
   420
                   val (gr4, rest) = compile_prems out_ts''' vs' (fst nvs) gr3 ps';
berghofe@11537
   421
                 in
berghofe@15031
   422
                   (gr4, compile_match (snd nvs) eq_ps out_ps
berghofe@12557
   423
                      (Pretty.block (ps @
berghofe@15031
   424
                         [Pretty.str " :->", Pretty.brk 1, rest]))
berghofe@15061
   425
                      (exists (not o is_exhaustive) out_ts''))
berghofe@11537
   426
                 end
berghofe@11537
   427
             | Sidecond t =>
berghofe@11537
   428
                 let
berghofe@17144
   429
                   val (gr2, side_p) = invoke_codegen thy defs dep module true (gr1, t);
berghofe@11537
   430
                   val (gr3, rest) = compile_prems [] vs' (fst nvs) gr2 ps';
berghofe@11537
   431
                 in
berghofe@15031
   432
                   (gr3, compile_match (snd nvs) eq_ps out_ps
berghofe@11537
   433
                      (Pretty.block [Pretty.str "?? ", side_p,
berghofe@11537
   434
                        Pretty.str " :->", Pretty.brk 1, rest])
berghofe@15061
   435
                      (exists (not o is_exhaustive) out_ts''))
berghofe@11537
   436
                 end)
berghofe@11537
   437
          end;
berghofe@11537
   438
berghofe@15126
   439
    val (gr', prem_p) = compile_prems in_ts' arg_vs all_vs' gr ps;
berghofe@11537
   440
  in
berghofe@22271
   441
    (gr', Pretty.block [Pretty.str "Seq.single", Pretty.brk 1, inp,
berghofe@22271
   442
       Pretty.str " :->", Pretty.brk 1, prem_p])
berghofe@11537
   443
  end;
berghofe@11537
   444
berghofe@17144
   445
fun compile_pred thy defs gr dep module prfx all_vs arg_vs modes s cls mode =
berghofe@22271
   446
  let
berghofe@22271
   447
    val xs = map Pretty.str (Name.variant_list arg_vs
berghofe@22271
   448
      (map (fn i => "x" ^ string_of_int i) (snd mode)));
berghofe@22271
   449
    val (gr', (cl_ps, mode_id)) =
berghofe@22271
   450
      foldl_map (fn (gr, cl) => compile_clause thy defs
berghofe@22271
   451
        gr dep module all_vs arg_vs modes mode cl (mk_tuple xs)) (gr, cls) |>>>
berghofe@22271
   452
      modename module s mode
berghofe@11537
   453
  in
berghofe@11537
   454
    ((gr', "and "), Pretty.block
berghofe@11537
   455
      ([Pretty.block (separate (Pretty.brk 1)
berghofe@17144
   456
         (Pretty.str (prfx ^ mode_id) ::
berghofe@22271
   457
           map Pretty.str arg_vs @ xs) @
berghofe@22271
   458
         [Pretty.str " ="]),
berghofe@11537
   459
        Pretty.brk 1] @
skalberg@15570
   460
       List.concat (separate [Pretty.str " ++", Pretty.brk 1] (map single cl_ps))))
berghofe@11537
   461
  end;
berghofe@11537
   462
berghofe@17144
   463
fun compile_preds thy defs gr dep module all_vs arg_vs modes preds =
berghofe@11537
   464
  let val ((gr', _), prs) = foldl_map (fn ((gr, prfx), (s, cls)) =>
berghofe@16645
   465
    foldl_map (fn ((gr', prfx'), mode) => compile_pred thy defs gr'
berghofe@17144
   466
      dep module prfx' all_vs arg_vs modes s cls mode)
haftmann@17521
   467
        ((gr, prfx), ((the o AList.lookup (op =) modes) s))) ((gr, "fun "), preds)
berghofe@11537
   468
  in
skalberg@15570
   469
    (gr', space_implode "\n\n" (map Pretty.string_of (List.concat prs)) ^ ";\n\n")
berghofe@11537
   470
  end;
berghofe@11537
   471
berghofe@11537
   472
(**** processing of introduction rules ****)
berghofe@11537
   473
berghofe@12557
   474
exception Modes of
berghofe@12557
   475
  (string * (int list option list * int list) list) list *
berghofe@22271
   476
  (string * (int option list * int)) list;
berghofe@12557
   477
berghofe@17144
   478
fun lookup_modes gr dep = apfst List.concat (apsnd List.concat (ListPair.unzip
berghofe@17144
   479
  (map ((fn (SOME (Modes x), _, _) => x | _ => ([], [])) o get_node gr)
berghofe@17144
   480
    (Graph.all_preds (fst gr) [dep]))));
berghofe@12557
   481
berghofe@22271
   482
fun print_arities arities = message ("Arities:\n" ^
berghofe@22271
   483
  space_implode "\n" (map (fn (s, (ks, k)) => s ^ ": " ^
berghofe@12557
   484
    space_implode " -> " (map
berghofe@22271
   485
      (fn NONE => "X" | SOME k' => string_of_int k')
berghofe@22271
   486
        (ks @ [SOME k]))) arities));
berghofe@11537
   487
berghofe@15031
   488
fun prep_intrs intrs = map (rename_term o #prop o rep_thm o standard) intrs;
berghofe@15031
   489
berghofe@15031
   490
fun constrain cs [] = []
haftmann@17521
   491
  | constrain cs ((s, xs) :: ys) = (s, case AList.lookup (op =) cs s of
skalberg@15531
   492
      NONE => xs
skalberg@15531
   493
    | SOME xs' => xs inter xs') :: constrain cs ys;
berghofe@15031
   494
berghofe@17144
   495
fun mk_extra_defs thy defs gr dep names module ts =
skalberg@15570
   496
  Library.foldl (fn (gr, name) =>
berghofe@12557
   497
    if name mem names then gr
berghofe@12557
   498
    else (case get_clauses thy name of
skalberg@15531
   499
        NONE => gr
berghofe@22271
   500
      | SOME (names, thyname, nparms, intrs) =>
berghofe@17144
   501
          mk_ind_def thy defs gr dep names (if_library thyname module)
berghofe@22271
   502
            [] (prep_intrs intrs) nparms))
skalberg@15574
   503
            (gr, foldr add_term_consts [] ts)
berghofe@12557
   504
berghofe@22271
   505
and mk_ind_def thy defs gr dep names module modecs intrs nparms =
berghofe@17144
   506
  add_edge (hd names, dep) gr handle Graph.UNDEF _ =>
berghofe@11537
   507
    let
berghofe@22271
   508
      val _ $ u = Logic.strip_imp_concl (hd intrs);
berghofe@22271
   509
      val args = List.take (snd (strip_comb u), nparms);
skalberg@15570
   510
      val arg_vs = List.concat (map term_vs args);
berghofe@15031
   511
berghofe@22271
   512
      fun get_nparms s = if s mem names then SOME nparms else
berghofe@22271
   513
        Option.map #3 (get_clauses thy s);
berghofe@11537
   514
berghofe@22271
   515
      fun dest_prem (_ $ (Const ("op :", _) $ t $ u)) = Prem ([t], u)
berghofe@22271
   516
        | dest_prem (_ $ ((eq as Const ("op =", _)) $ t $ u)) = Prem ([t, u], eq)
berghofe@22271
   517
        | dest_prem (_ $ t) =
berghofe@22271
   518
            (case strip_comb t of
berghofe@22556
   519
               (v as Var _, ts) => if v mem args then Prem (ts, v) else Sidecond t
berghofe@22271
   520
             | (c as Const (s, _), ts) => (case get_nparms s of
berghofe@22271
   521
                 NONE => Sidecond t
berghofe@22271
   522
               | SOME k =>
berghofe@22271
   523
                   let val (ts1, ts2) = chop k ts
berghofe@22271
   524
                   in Prem (ts2, list_comb (c, ts1)) end)
berghofe@22271
   525
             | _ => Sidecond t);
berghofe@22271
   526
berghofe@22271
   527
      fun add_clause intr (clauses, arities) =
berghofe@11537
   528
        let
berghofe@22271
   529
          val _ $ t = Logic.strip_imp_concl intr;
berghofe@22271
   530
          val (Const (name, T), ts) = strip_comb t;
berghofe@22271
   531
          val (ts1, ts2) = chop nparms ts;
berghofe@22271
   532
          val prems = map dest_prem (Logic.strip_imp_prems intr);
berghofe@22271
   533
          val (Ts, Us) = chop nparms (binder_types T)
berghofe@11537
   534
        in
berghofe@22271
   535
          (AList.update op = (name, these (AList.lookup op = clauses name) @
berghofe@22271
   536
             [(ts2, prems)]) clauses,
berghofe@22271
   537
           AList.update op = (name, (map (fn U => (case strip_type U of
berghofe@22271
   538
                 (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
berghofe@22271
   539
               | _ => NONE)) Ts,
berghofe@22271
   540
             length Us)) arities)
berghofe@11537
   541
        end;
berghofe@11537
   542
berghofe@16645
   543
      val gr' = mk_extra_defs thy defs
berghofe@17144
   544
        (add_edge (hd names, dep)
berghofe@17144
   545
          (new_node (hd names, (NONE, "", "")) gr)) (hd names) names module intrs;
berghofe@22271
   546
      val (extra_modes, extra_arities) = lookup_modes gr' (hd names);
berghofe@22271
   547
      val (clauses, arities) = fold add_clause intrs ([], []);
berghofe@15031
   548
      val modes = constrain modecs
berghofe@22271
   549
        (infer_modes thy extra_modes arities arg_vs clauses);
berghofe@22271
   550
      val _ = print_arities arities;
berghofe@11537
   551
      val _ = print_modes modes;
berghofe@17144
   552
      val (gr'', s) = compile_preds thy defs gr' (hd names) module (terms_vs intrs)
berghofe@17144
   553
        arg_vs (modes @ extra_modes) clauses;
berghofe@11537
   554
    in
berghofe@17144
   555
      (map_node (hd names)
berghofe@22271
   556
        (K (SOME (Modes (modes, arities)), module, s)) gr'')
berghofe@16645
   557
    end;
berghofe@11537
   558
berghofe@22271
   559
fun find_mode gr dep s u modes is = (case find_first (fn Mode (_, js, _) => is=js)
wenzelm@15660
   560
  (modes_of modes u handle Option => []) of
berghofe@17144
   561
     NONE => codegen_error gr dep
berghofe@17144
   562
       ("No such mode for " ^ s ^ ": " ^ string_of_mode ([], is))
berghofe@15031
   563
   | mode => mode);
berghofe@15031
   564
berghofe@22271
   565
fun mk_ind_call thy defs gr dep module is_query s T ts names thyname k intrs =
berghofe@22271
   566
  let
berghofe@22271
   567
    val (ts1, ts2) = chop k ts;
berghofe@22271
   568
    val u = list_comb (Const (s, T), ts1);
berghofe@22271
   569
berghofe@22271
   570
    fun mk_mode (((ts, mode), i), Const ("dummy_pattern", _)) =
berghofe@22271
   571
          ((ts, mode), i+1)
berghofe@22271
   572
      | mk_mode (((ts, mode), i), t) = ((ts @ [t], mode @ [i]), i+1);
berghofe@11537
   573
berghofe@22271
   574
    val module' = if_library thyname module;
berghofe@22271
   575
    val gr1 = mk_extra_defs thy defs
berghofe@22271
   576
      (mk_ind_def thy defs gr dep names module'
berghofe@22271
   577
      [] (prep_intrs intrs) k) dep names module' [u];
berghofe@22271
   578
    val (modes, _) = lookup_modes gr1 dep;
berghofe@22271
   579
    val (ts', is) = if is_query then
berghofe@22271
   580
        fst (Library.foldl mk_mode ((([], []), 1), ts2))
berghofe@22271
   581
      else (ts2, 1 upto length (binder_types T) - k);
berghofe@22271
   582
    val mode = find_mode gr1 dep s u modes is;
berghofe@22271
   583
    val (gr2, in_ps) = foldl_map
berghofe@22271
   584
      (invoke_codegen thy defs dep module true) (gr1, ts');
berghofe@22271
   585
    val (gr3, ps) =
berghofe@22271
   586
      compile_expr thy defs dep module false modes (gr2, (mode, u))
berghofe@22271
   587
  in
berghofe@22271
   588
    (gr3, Pretty.block (ps @ (if null in_ps then [] else [Pretty.brk 1]) @
berghofe@22271
   589
       separate (Pretty.brk 1) in_ps))
berghofe@22271
   590
  end;
berghofe@15031
   591
berghofe@15031
   592
fun clause_of_eqn eqn =
berghofe@15031
   593
  let
berghofe@15031
   594
    val (t, u) = HOLogic.dest_eq (HOLogic.dest_Trueprop (concl_of eqn));
berghofe@15031
   595
    val (Const (s, T), ts) = strip_comb t;
berghofe@15031
   596
    val (Ts, U) = strip_type T
berghofe@15031
   597
  in
berghofe@22271
   598
    rename_term (Logic.list_implies (prems_of eqn, HOLogic.mk_Trueprop
berghofe@22271
   599
      (list_comb (Const (s ^ "_aux", Ts @ [U] ---> HOLogic.boolT), ts @ [u]))))
berghofe@15031
   600
  end;
berghofe@15031
   601
berghofe@17144
   602
fun mk_fun thy defs name eqns dep module module' gr =
berghofe@17144
   603
  case try (get_node gr) name of
berghofe@17144
   604
    NONE =>
berghofe@15031
   605
    let
berghofe@15031
   606
      val clauses = map clause_of_eqn eqns;
berghofe@17144
   607
      val pname = name ^ "_aux";
berghofe@15031
   608
      val arity = length (snd (strip_comb (fst (HOLogic.dest_eq
berghofe@15031
   609
        (HOLogic.dest_Trueprop (concl_of (hd eqns)))))));
berghofe@15031
   610
      val mode = 1 upto arity;
berghofe@17144
   611
      val (gr', (fun_id, mode_id)) = gr |>
berghofe@17144
   612
        mk_const_id module' name |>>>
berghofe@17144
   613
        modename module' pname ([], mode);
berghofe@15031
   614
      val vars = map (fn i => Pretty.str ("x" ^ string_of_int i)) mode;
berghofe@15031
   615
      val s = Pretty.string_of (Pretty.block
berghofe@17144
   616
        [mk_app false (Pretty.str ("fun " ^ snd fun_id)) vars, Pretty.str " =",
berghofe@15031
   617
         Pretty.brk 1, Pretty.str "Seq.hd", Pretty.brk 1,
berghofe@22271
   618
         parens (Pretty.block (separate (Pretty.brk 1) (Pretty.str mode_id ::
berghofe@22271
   619
           vars)))]) ^ ";\n\n";
berghofe@17144
   620
      val gr'' = mk_ind_def thy defs (add_edge (name, dep)
berghofe@17144
   621
        (new_node (name, (NONE, module', s)) gr')) name [pname] module'
berghofe@22271
   622
        [(pname, [([], mode)])] clauses 0;
berghofe@17144
   623
      val (modes, _) = lookup_modes gr'' dep;
berghofe@22271
   624
      val _ = find_mode gr'' dep pname (head_of (HOLogic.dest_Trueprop
berghofe@22271
   625
        (Logic.strip_imp_concl (hd clauses)))) modes mode
berghofe@17144
   626
    in (gr'', mk_qual_id module fun_id) end
berghofe@17144
   627
  | SOME _ =>
berghofe@17144
   628
      (add_edge (name, dep) gr, mk_qual_id module (get_const_id name gr));
berghofe@15031
   629
berghofe@22271
   630
fun inductive_codegen thy defs gr dep module brack t = (case strip_comb t of
berghofe@22271
   631
    (Const ("Collect", Type (_, [_, Type (_, [U])])), [u]) => (case strip_comb u of
berghofe@22271
   632
        (Const (s, T), ts) => (case (get_clauses thy s, get_assoc_code thy s T) of
berghofe@22271
   633
          (SOME (names, thyname, k, intrs), NONE) =>
berghofe@22271
   634
            let val (gr', call_p) = mk_ind_call thy defs gr dep module true
berghofe@22271
   635
              s T (ts @ [Term.dummy_pattern U]) names thyname k intrs
berghofe@22271
   636
            in SOME (gr', (if brack then parens else I) (Pretty.block
berghofe@22271
   637
              [Pretty.str "Seq.list_of", Pretty.brk 1, Pretty.str "(",
berghofe@22271
   638
               call_p, Pretty.str ")"]))
berghofe@22271
   639
            end
berghofe@22271
   640
        | _ => NONE)
berghofe@22271
   641
      | _ => NONE)
berghofe@22271
   642
  | (Const (s, T), ts) => (case Symtab.lookup (#eqns (CodegenData.get thy)) s of
berghofe@22271
   643
      NONE => (case (get_clauses thy s, get_assoc_code thy s T) of
berghofe@22271
   644
        (SOME (names, thyname, k, intrs), NONE) =>
berghofe@22271
   645
          if length ts < k then NONE else SOME
berghofe@22271
   646
            (let val (gr', call_p) = mk_ind_call thy defs gr dep module false
berghofe@22271
   647
               s T (map Term.no_dummy_patterns ts) names thyname k intrs
berghofe@22271
   648
             in (gr', mk_funcomp brack "?!"
berghofe@22271
   649
               (length (binder_types T) - length ts) (parens call_p))
berghofe@22271
   650
             end handle TERM _ => mk_ind_call thy defs gr dep module true
berghofe@22271
   651
               s T ts names thyname k intrs)
berghofe@22271
   652
      | _ => NONE)
berghofe@22271
   653
    | SOME eqns =>
berghofe@22271
   654
        let
berghofe@22271
   655
          val (_, (_, thyname)) = split_last eqns;
berghofe@22271
   656
          val (gr', id) = mk_fun thy defs s (preprocess thy (map fst eqns))
berghofe@22271
   657
            dep module (if_library thyname module) gr;
berghofe@22271
   658
          val (gr'', ps) = foldl_map
berghofe@22271
   659
            (invoke_codegen thy defs dep module true) (gr', ts);
berghofe@22271
   660
        in SOME (gr'', mk_app brack (Pretty.str id) ps)
berghofe@22271
   661
        end)
berghofe@22271
   662
  | _ => NONE);
berghofe@11537
   663
berghofe@12557
   664
val setup =
wenzelm@18708
   665
  add_codegen "inductive" inductive_codegen #>
wenzelm@18708
   666
  CodegenData.init #>
berghofe@22271
   667
  add_attribute "ind" (Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) --
berghofe@22271
   668
    Scan.option (Args.$$$ "params" |-- Args.colon |-- Args.nat) >> uncurry add);
berghofe@11537
   669
berghofe@11537
   670
end;
berghofe@12453
   671
berghofe@12453
   672
berghofe@12453
   673
(**** combinators for code generated from inductive predicates ****)
berghofe@12453
   674
berghofe@12453
   675
infix 5 :->;
berghofe@12453
   676
infix 3 ++;
berghofe@12453
   677
wenzelm@19861
   678
fun s :-> f = Seq.maps f s;
berghofe@12453
   679
wenzelm@19861
   680
fun s1 ++ s2 = Seq.append s1 s2;
berghofe@12453
   681
berghofe@12453
   682
fun ?? b = if b then Seq.single () else Seq.empty;
berghofe@12453
   683
berghofe@22271
   684
fun ??? f g = f o g;
berghofe@22271
   685
wenzelm@19861
   686
fun ?! s = is_some (Seq.pull s);
berghofe@12453
   687
berghofe@17144
   688
fun equal__1 x = Seq.single x;
berghofe@12453
   689
berghofe@17144
   690
val equal__2 = equal__1;
berghofe@12557
   691
berghofe@17144
   692
fun equal__1_2 (x, y) = ?? (x = y);