src/HOL/Tools/inductive_codegen.ML
author berghofe
Mon, 22 Sep 2003 16:06:05 +0200
changeset 14195 e7c9206dd2ef
parent 14163 5ffa75ce4919
child 14250 d09e92e7c2bf
permissions -rw-r--r--
Changed interface of add_attribute.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
     1
(*  Title:      HOL/inductive_codegen.ML
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
11539
0f17da240450 tuned headers;
wenzelm
parents: 11537
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
0f17da240450 tuned headers;
wenzelm
parents: 11537
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     5
11539
0f17da240450 tuned headers;
wenzelm
parents: 11537
diff changeset
     6
Code generator for inductive predicates.
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     7
*)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     8
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     9
signature INDUCTIVE_CODEGEN =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    10
sig
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    11
  val add : theory attribute
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    12
  val setup : (theory -> theory) list
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    13
end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    14
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    15
structure InductiveCodegen : INDUCTIVE_CODEGEN =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    16
struct
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    17
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    18
open Codegen;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    19
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    20
(**** theory data ****)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    21
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    22
structure CodegenArgs =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    23
struct
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    24
  val name = "HOL/inductive_codegen";
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    25
  type T = thm list Symtab.table * unit Graph.T;
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    26
  val empty = (Symtab.empty, Graph.empty);
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    27
  val copy = I;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    28
  val prep_ext = I;
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    29
  fun merge ((tab1, graph1), (tab2, graph2)) =
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    30
    (Symtab.merge_multi Drule.eq_thm_prop (tab1, tab2),
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    31
     Graph.merge (K true) (graph1, graph2));
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    32
  fun print _ _ = ();
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    33
end;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    34
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    35
structure CodegenData = TheoryDataFun(CodegenArgs);
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    36
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    37
fun warn thm = warning ("InductiveCodegen: Not a proper clause:\n" ^
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    38
  string_of_thm thm);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    39
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    40
fun add_node (g, x) = Graph.new_node (x, ()) g handle Graph.DUP _ => g;
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    41
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    42
fun add (p as (thy, thm)) =
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    43
  let val (tab, graph) = CodegenData.get thy;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    44
  in (case concl_of thm of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    45
      _ $ (Const ("op :", _) $ _ $ t) => (case head_of t of
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    46
        Const (s, _) =>
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    47
          let val cs = foldr add_term_consts (prems_of thm, [])
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    48
          in (CodegenData.put
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    49
            (Symtab.update ((s,
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    50
               if_none (Symtab.lookup (tab, s)) [] @ [thm]), tab),
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    51
             foldr (uncurry (Graph.add_edge o pair s))
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    52
               (cs, foldl add_node (graph, s :: cs))) thy, thm)
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    53
          end
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    54
      | _ => (warn thm; p))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    55
    | _ => (warn thm; p))
12562
323ce5a89695 Fixed bug in function add.
berghofe
parents: 12557
diff changeset
    56
  end;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    57
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    58
fun get_clauses thy s =
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    59
  let val (tab, graph) = CodegenData.get thy
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    60
  in case Symtab.lookup (tab, s) of
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    61
      None => (case InductivePackage.get_inductive thy s of
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    62
        None => None
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    63
      | Some ({names, ...}, {intrs, ...}) => Some (names, intrs))
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    64
    | Some _ =>
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    65
        let val Some names = find_first
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    66
          (fn xs => s mem xs) (Graph.strong_conn graph)
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    67
        in Some (names,
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    68
          flat (map (fn s => the (Symtab.lookup (tab, s))) names))
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    69
        end
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    70
  end;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    71
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    72
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    73
(**** improper tuples ****)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    74
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    75
fun prod_factors p (Const ("Pair", _) $ t $ u) =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    76
      p :: prod_factors (1::p) t @ prod_factors (2::p) u
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    77
  | prod_factors p _ = [];
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    78
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    79
fun split_prod p ps t = if p mem ps then (case t of
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    80
       Const ("Pair", _) $ t $ u =>
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    81
         split_prod (1::p) ps t @ split_prod (2::p) ps u
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    82
     | _ => error "Inconsistent use of products") else [t];
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    83
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    84
datatype factors = FVar of int list list | FFix of int list list;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    85
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    86
exception Factors;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    87
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    88
fun mg_factor (FVar f) (FVar f') = FVar (f inter f')
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    89
  | mg_factor (FVar f) (FFix f') =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    90
      if f' subset f then FFix f' else raise Factors
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    91
  | mg_factor (FFix f) (FVar f') =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    92
      if f subset f' then FFix f else raise Factors
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    93
  | mg_factor (FFix f) (FFix f') =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    94
      if f subset f' andalso f' subset f then FFix f else raise Factors;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    95
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    96
fun dest_factors (FVar f) = f
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    97
  | dest_factors (FFix f) = f;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    98
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    99
fun infer_factors sg extra_fs (fs, (optf, t)) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   100
  let fun err s = error (s ^ "\n" ^ Sign.string_of_term sg t)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   101
  in (case (optf, strip_comb t) of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   102
      (Some f, (Const (name, _), args)) =>
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   103
        (case assoc (extra_fs, name) of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   104
           None => overwrite (fs, (name, if_none
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   105
             (apsome (mg_factor f) (assoc (fs, name))) f))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   106
         | Some (fs', f') => (mg_factor f (FFix f');
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   107
             foldl (infer_factors sg extra_fs)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   108
               (fs, map (apsome FFix) fs' ~~ args)))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   109
    | (Some f, (Var ((name, _), _), [])) =>
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   110
        overwrite (fs, (name, if_none
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   111
          (apsome (mg_factor f) (assoc (fs, name))) f))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   112
    | (None, _) => fs
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   113
    | _ => err "Illegal term")
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   114
      handle Factors => err "Product factor mismatch in"
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   115
  end;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   116
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   117
fun string_of_factors p ps = if p mem ps then
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   118
    "(" ^ string_of_factors (1::p) ps ^ ", " ^ string_of_factors (2::p) ps ^ ")"
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   119
  else "_";
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   120
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   121
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   122
(**** check if a term contains only constructor functions ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   123
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   124
fun is_constrt thy =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   125
  let
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   126
    val cnstrs = flat (flat (map
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   127
      (map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   128
      (Symtab.dest (DatatypePackage.get_datatypes thy))));
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   129
    fun check t = (case strip_comb t of
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   130
        (Var _, []) => true
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   131
      | (Const (s, _), ts) => (case assoc (cnstrs, s) of
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   132
            None => false
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   133
          | Some i => length ts = i andalso forall check ts)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   134
      | _ => false)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   135
  in check end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   136
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   137
(**** check if a type is an equality type (i.e. doesn't contain fun) ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   138
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   139
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   140
  | is_eqT _ = true;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   141
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   142
(**** mode inference ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   143
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   144
val term_vs = map (fst o fst o dest_Var) o term_vars;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   145
val terms_vs = distinct o flat o (map term_vs);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   146
13038
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   147
fun assoc' s tab key = (case assoc (tab, key) of
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   148
      None => error ("Unable to determine " ^ s ^ " of " ^ quote key)
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   149
    | Some x => x);
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   150
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   151
(** collect all Vars in a term (with duplicates!) **)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   152
fun term_vTs t = map (apfst fst o dest_Var)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   153
  (filter is_Var (foldl_aterms (op :: o Library.swap) ([], t)));
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   154
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   155
fun known_args _ _ [] = []
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   156
  | known_args vs i (t::ts) = if term_vs t subset vs then i::known_args vs (i+1) ts
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   157
      else known_args vs (i+1) ts;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   158
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   159
fun get_args _ _ [] = ([], [])
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   160
  | get_args is i (x::xs) = (if i mem is then apfst else apsnd) (cons x)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   161
      (get_args is (i+1) xs);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   162
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   163
fun merge xs [] = xs
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   164
  | merge [] ys = ys
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   165
  | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   166
      else y::merge (x::xs) ys;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   167
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   168
fun subsets i j = if i <= j then
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   169
       let val is = subsets (i+1) j
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   170
       in merge (map (fn ks => i::ks) is) is end
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   171
     else [[]];
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   172
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   173
fun cprod ([], ys) = []
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   174
  | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys);
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   175
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   176
fun cprods xss = foldr (map op :: o cprod) (xss, [[]]);
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   177
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   178
datatype mode = Mode of (int list option list * int list) * mode option list;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   179
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   180
fun modes_of modes t =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   181
  let
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   182
    fun mk_modes name args = flat
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   183
      (map (fn (m as (iss, is)) => map (Mode o pair m) (cprods (map
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   184
        (fn (None, _) => [None]
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   185
          | (Some js, arg) => map Some
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   186
              (filter (fn Mode ((_, js'), _) => js=js') (modes_of modes arg)))
13038
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   187
                (iss ~~ args)))) (assoc' "modes" modes name))
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   188
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   189
  in (case strip_comb t of
14163
5ffa75ce4919 Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents: 14162
diff changeset
   190
      (Const ("op =", Type (_, [T, _])), _) =>
5ffa75ce4919 Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents: 14162
diff changeset
   191
        [Mode (([], [1]), []), Mode (([], [2]), [])] @
5ffa75ce4919 Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents: 14162
diff changeset
   192
        (if is_eqT T then [Mode (([], [1, 2]), [])] else [])
5ffa75ce4919 Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents: 14162
diff changeset
   193
    | (Const (name, _), args) => mk_modes name args
13038
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   194
    | (Var ((name, _), _), args) => mk_modes name args
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   195
    | (Free (name, _), args) => mk_modes name args)
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   196
  end;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   197
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   198
datatype indprem = Prem of term list * term | Sidecond of term;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   199
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   200
fun select_mode_prem thy modes vs ps =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   201
  find_first (is_some o snd) (ps ~~ map
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   202
    (fn Prem (us, t) => find_first (fn Mode ((_, is), _) =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   203
          let
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   204
            val (_, out_ts) = get_args is 1 us;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   205
            val vTs = flat (map term_vTs out_ts);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   206
            val dupTs = map snd (duplicates vTs) @
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   207
              mapfilter (curry assoc vTs) vs;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   208
          in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   209
            is subset known_args vs 1 us andalso
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   210
            forall (is_constrt thy) (snd (get_args is 1 us)) andalso
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   211
            term_vs t subset vs andalso
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   212
            forall is_eqT dupTs
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   213
          end)
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   214
            (modes_of modes t)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   215
      | Sidecond t => if term_vs t subset vs then Some (Mode (([], []), []))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   216
          else None) ps);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   217
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   218
fun check_mode_clause thy arg_vs modes (iss, is) (ts, ps) =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   219
  let
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   220
    val modes' = modes @ mapfilter
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   221
      (fn (_, None) => None | (v, Some js) => Some (v, [([], js)]))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   222
        (arg_vs ~~ iss);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   223
    fun check_mode_prems vs [] = Some vs
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   224
      | check_mode_prems vs ps = (case select_mode_prem thy modes' vs ps of
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   225
          None => None
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   226
        | Some (x, _) => check_mode_prems
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   227
            (case x of Prem (us, _) => vs union terms_vs us | _ => vs)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   228
            (filter_out (equal x) ps));
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   229
    val (in_ts', _) = get_args is 1 ts;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   230
    val in_ts = filter (is_constrt thy) in_ts';
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   231
    val in_vs = terms_vs in_ts;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   232
    val concl_vs = terms_vs ts
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   233
  in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   234
    forall is_eqT (map snd (duplicates (flat (map term_vTs in_ts')))) andalso
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   235
    (case check_mode_prems (arg_vs union in_vs) ps of
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   236
       None => false
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   237
     | Some vs => concl_vs subset vs)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   238
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   239
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   240
fun check_modes_pred thy arg_vs preds modes (p, ms) =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   241
  let val Some rs = assoc (preds, p)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   242
  in (p, filter (fn m => forall (check_mode_clause thy arg_vs modes m) rs) ms) end
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   243
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   244
fun fixp f x =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   245
  let val y = f x
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   246
  in if x = y then x else fixp f y end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   247
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   248
fun infer_modes thy extra_modes factors arg_vs preds = fixp (fn modes =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   249
  map (check_modes_pred thy arg_vs preds (modes @ extra_modes)) modes)
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   250
    (map (fn (s, (fs, f)) => (s, cprod (cprods (map
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   251
      (fn None => [None]
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   252
        | Some f' => map Some (subsets 1 (length f' + 1))) fs),
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   253
      subsets 1 (length f + 1)))) factors);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   254
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   255
(**** code generation ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   256
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   257
fun mk_eq (x::xs) =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   258
  let fun mk_eqs _ [] = []
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   259
        | mk_eqs a (b::cs) = Pretty.str (a ^ " = " ^ b) :: mk_eqs b cs
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   260
  in mk_eqs x xs end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   261
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   262
fun mk_tuple xs = Pretty.block (Pretty.str "(" ::
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   263
  flat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   264
  [Pretty.str ")"]);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   265
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   266
fun mk_v ((names, vs), s) = (case assoc (vs, s) of
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   267
      None => ((names, (s, [s])::vs), s)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   268
    | Some xs => let val s' = variant names s in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   269
        ((s'::names, overwrite (vs, (s, s'::xs))), s') end);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   270
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   271
fun distinct_v (nvs, Var ((s, 0), T)) =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   272
      apsnd (Var o rpair T o rpair 0) (mk_v (nvs, s))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   273
  | distinct_v (nvs, t $ u) =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   274
      let
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   275
        val (nvs', t') = distinct_v (nvs, t);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   276
        val (nvs'', u') = distinct_v (nvs', u);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   277
      in (nvs'', t' $ u') end
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   278
  | distinct_v x = x;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   279
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   280
fun compile_match nvs eq_ps out_ps success_p fail_p =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   281
  let val eqs = flat (separate [Pretty.str " andalso", Pretty.brk 1]
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   282
    (map single (flat (map (mk_eq o snd) nvs) @ eq_ps)));
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   283
  in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   284
    Pretty.block
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   285
     ([Pretty.str "(fn ", mk_tuple out_ps, Pretty.str " =>", Pretty.brk 1] @
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   286
      (Pretty.block ((if eqs=[] then [] else Pretty.str "if " ::
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   287
         [Pretty.block eqs, Pretty.brk 1, Pretty.str "then "]) @
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   288
         (success_p ::
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   289
          (if eqs=[] then [] else [Pretty.brk 1, Pretty.str "else ", fail_p]))) ::
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   290
       [Pretty.brk 1, Pretty.str "| _ => ", fail_p, Pretty.str ")"]))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   291
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   292
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   293
fun modename thy s (iss, is) = space_implode "__"
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   294
  (mk_const_id (sign_of thy) s ::
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   295
    map (space_implode "_" o map string_of_int) (mapfilter I iss @ [is]));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   296
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   297
fun compile_expr thy dep brack (gr, (None, t)) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   298
      apsnd single (invoke_codegen thy dep brack (gr, t))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   299
  | compile_expr _ _ _ (gr, (Some _, Var ((name, _), _))) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   300
      (gr, [Pretty.str name])
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   301
  | compile_expr thy dep brack (gr, (Some (Mode (mode, ms)), t)) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   302
      let
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   303
        val (Const (name, _), args) = strip_comb t;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   304
        val (gr', ps) = foldl_map
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   305
          (compile_expr thy dep true) (gr, ms ~~ args);
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   306
      in (gr', (if brack andalso not (null ps) then
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   307
        single o parens o Pretty.block else I)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   308
          (flat (separate [Pretty.brk 1]
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   309
            ([Pretty.str (modename thy name mode)] :: ps))))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   310
      end;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   311
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   312
fun compile_clause thy gr dep all_vs arg_vs modes (iss, is) (ts, ps) =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   313
  let
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   314
    val modes' = modes @ mapfilter
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   315
      (fn (_, None) => None | (v, Some js) => Some (v, [([], js)]))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   316
        (arg_vs ~~ iss);
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   317
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   318
    fun check_constrt ((names, eqs), t) =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   319
      if is_constrt thy t then ((names, eqs), t) else
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   320
        let val s = variant names "x";
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   321
        in ((s::names, (s, t)::eqs), Var ((s, 0), fastype_of t)) end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   322
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   323
    val (in_ts, out_ts) = get_args is 1 ts;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   324
    val ((all_vs', eqs), in_ts') =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   325
      foldl_map check_constrt ((all_vs, []), in_ts);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   326
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   327
    fun compile_prems out_ts' vs names gr [] =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   328
          let
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   329
            val (gr2, out_ps) = foldl_map
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   330
              (invoke_codegen thy dep false) (gr, out_ts);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   331
            val (gr3, eq_ps) = foldl_map (fn (gr, (s, t)) =>
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   332
              apsnd (Pretty.block o cons (Pretty.str (s ^ " = ")) o single)
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   333
                (invoke_codegen thy dep false (gr, t))) (gr2, eqs);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   334
            val (nvs, out_ts'') = foldl_map distinct_v
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   335
              ((names, map (fn x => (x, [x])) vs), out_ts');
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   336
            val (gr4, out_ps') = foldl_map
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   337
              (invoke_codegen thy dep false) (gr3, out_ts'');
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   338
          in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   339
            (gr4, compile_match (snd nvs) eq_ps out_ps'
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   340
              (Pretty.block [Pretty.str "Seq.single", Pretty.brk 1, mk_tuple out_ps])
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   341
              (Pretty.str "Seq.empty"))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   342
          end
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   343
      | compile_prems out_ts vs names gr ps =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   344
          let
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   345
            val vs' = distinct (flat (vs :: map term_vs out_ts));
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   346
            val Some (p, mode as Some (Mode ((_, js), _))) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   347
              select_mode_prem thy modes' (arg_vs union vs') ps;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   348
            val ps' = filter_out (equal p) ps;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   349
          in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   350
            (case p of
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   351
               Prem (us, t) =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   352
                 let
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   353
                   val (in_ts, out_ts') = get_args js 1 us;
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   354
                   val (gr1, in_ps) = foldl_map
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   355
                     (invoke_codegen thy dep false) (gr, in_ts);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   356
                   val (nvs, out_ts'') = foldl_map distinct_v
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   357
                     ((names, map (fn x => (x, [x])) vs), out_ts);
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   358
                   val (gr2, out_ps) = foldl_map
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   359
                     (invoke_codegen thy dep false) (gr1, out_ts'');
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   360
                   val (gr3, ps) = compile_expr thy dep false (gr2, (mode, t));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   361
                   val (gr4, rest) = compile_prems out_ts' vs' (fst nvs) gr3 ps';
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   362
                 in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   363
                   (gr4, compile_match (snd nvs) [] out_ps
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   364
                      (Pretty.block (ps @
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   365
                         [Pretty.brk 1, mk_tuple in_ps,
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   366
                          Pretty.str " :->", Pretty.brk 1, rest]))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   367
                      (Pretty.str "Seq.empty"))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   368
                 end
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   369
             | Sidecond t =>
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   370
                 let
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   371
                   val (gr1, side_p) = invoke_codegen thy dep true (gr, t);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   372
                   val (nvs, out_ts') = foldl_map distinct_v
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   373
                     ((names, map (fn x => (x, [x])) vs), out_ts);
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   374
                   val (gr2, out_ps) = foldl_map
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   375
                     (invoke_codegen thy dep false) (gr1, out_ts')
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   376
                   val (gr3, rest) = compile_prems [] vs' (fst nvs) gr2 ps';
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   377
                 in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   378
                   (gr3, compile_match (snd nvs) [] out_ps
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   379
                      (Pretty.block [Pretty.str "?? ", side_p,
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   380
                        Pretty.str " :->", Pretty.brk 1, rest])
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   381
                      (Pretty.str "Seq.empty"))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   382
                 end)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   383
          end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   384
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   385
    val (gr', prem_p) = compile_prems in_ts' [] all_vs' gr ps;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   386
  in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   387
    (gr', Pretty.block [Pretty.str "Seq.single inp :->", Pretty.brk 1, prem_p])
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   388
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   389
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   390
fun compile_pred thy gr dep prfx all_vs arg_vs modes s cls mode =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   391
  let val (gr', cl_ps) = foldl_map (fn (gr, cl) =>
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   392
    compile_clause thy gr dep all_vs arg_vs modes mode cl) (gr, cls)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   393
  in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   394
    ((gr', "and "), Pretty.block
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   395
      ([Pretty.block (separate (Pretty.brk 1)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   396
         (Pretty.str (prfx ^ modename thy s mode) :: map Pretty.str arg_vs) @
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   397
         [Pretty.str " inp ="]),
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   398
        Pretty.brk 1] @
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   399
       flat (separate [Pretty.str " ++", Pretty.brk 1] (map single cl_ps))))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   400
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   401
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   402
fun compile_preds thy gr dep all_vs arg_vs modes preds =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   403
  let val ((gr', _), prs) = foldl_map (fn ((gr, prfx), (s, cls)) =>
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   404
    foldl_map (fn ((gr', prfx'), mode) =>
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   405
      compile_pred thy gr' dep prfx' all_vs arg_vs modes s cls mode)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   406
        ((gr, prfx), the (assoc (modes, s)))) ((gr, "fun "), preds)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   407
  in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   408
    (gr', space_implode "\n\n" (map Pretty.string_of (flat prs)) ^ ";\n\n")
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   409
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   410
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   411
(**** processing of introduction rules ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   412
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   413
exception Modes of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   414
  (string * (int list option list * int list) list) list *
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   415
  (string * (int list list option list * int list list)) list;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   416
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   417
fun lookup_modes gr dep = apfst flat (apsnd flat (ListPair.unzip
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   418
  (map ((fn (Some (Modes x), _) => x | _ => ([], [])) o Graph.get_node gr)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   419
    (Graph.all_preds gr [dep]))));
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   420
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   421
fun string_of_mode (iss, is) = space_implode " -> " (map
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   422
  (fn None => "X"
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   423
    | Some js => enclose "[" "]" (commas (map string_of_int js)))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   424
       (iss @ [Some is]));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   425
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   426
fun print_modes modes = message ("Inferred modes:\n" ^
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   427
  space_implode "\n" (map (fn (s, ms) => s ^ ": " ^ commas (map
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   428
    string_of_mode ms)) modes));
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   429
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   430
fun print_factors factors = message ("Factors:\n" ^
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   431
  space_implode "\n" (map (fn (s, (fs, f)) => s ^ ": " ^
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   432
    space_implode " -> " (map
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   433
      (fn None => "X" | Some f' => string_of_factors [] f')
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   434
        (fs @ [Some f]))) factors));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   435
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   436
fun mk_extra_defs thy gr dep names ts =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   437
  foldl (fn (gr, name) =>
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   438
    if name mem names then gr
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   439
    else (case get_clauses thy name of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   440
        None => gr
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   441
      | Some (names, intrs) =>
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   442
          mk_ind_def thy gr dep names intrs))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   443
            (gr, foldr add_term_consts (ts, []))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   444
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   445
and mk_ind_def thy gr dep names intrs =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   446
  let val ids = map (mk_const_id (sign_of thy)) names
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   447
  in Graph.add_edge (hd ids, dep) gr handle Graph.UNDEF _ =>
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   448
    let
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   449
      fun dest_prem factors (_ $ (Const ("op :", _) $ t $ u)) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   450
            (case head_of u of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   451
               Const (name, _) => Prem (split_prod []
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   452
                 (the (assoc (factors, name))) t, u)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   453
             | Var ((name, _), _) => Prem (split_prod []
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   454
                 (the (assoc (factors, name))) t, u))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   455
        | dest_prem factors (_ $ ((eq as Const ("op =", _)) $ t $ u)) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   456
            Prem ([t, u], eq)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   457
        | dest_prem factors (_ $ t) = Sidecond t;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   458
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   459
      fun add_clause factors (clauses, intr) =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   460
        let
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   461
          val _ $ (_ $ t $ u) = Logic.strip_imp_concl intr;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   462
          val Const (name, _) = head_of u;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   463
          val prems = map (dest_prem factors) (Logic.strip_imp_prems intr);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   464
        in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   465
          (overwrite (clauses, (name, if_none (assoc (clauses, name)) [] @
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   466
             [(split_prod [] (the (assoc (factors, name))) t, prems)])))
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   467
        end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   468
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   469
      fun add_prod_factors extra_fs (fs, _ $ (Const ("op :", _) $ t $ u)) =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   470
            infer_factors (sign_of thy) extra_fs
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   471
              (fs, (Some (FVar (prod_factors [] t)), u))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   472
        | add_prod_factors _ (fs, _) = fs;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   473
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   474
      val intrs' = map (rename_term o #prop o rep_thm o standard) intrs;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   475
      val _ $ (_ $ _ $ u) = Logic.strip_imp_concl (hd intrs');
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   476
      val (_, args) = strip_comb u;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   477
      val arg_vs = flat (map term_vs args);
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   478
      val gr' = mk_extra_defs thy
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   479
        (Graph.add_edge (hd ids, dep)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   480
          (Graph.new_node (hd ids, (None, "")) gr)) (hd ids) names intrs';
14163
5ffa75ce4919 Improved handling of modes for equality predicate, to avoid ill-typed
berghofe
parents: 14162
diff changeset
   481
      val (extra_modes, extra_factors) = lookup_modes gr' (hd ids);
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   482
      val fs = map (apsnd dest_factors)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   483
        (foldl (add_prod_factors extra_factors) ([], flat (map (fn t =>
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   484
          Logic.strip_imp_concl t :: Logic.strip_imp_prems t) intrs')));
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   485
      val _ = (case map fst fs \\ names \\ arg_vs of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   486
          [] => ()
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   487
        | xs => error ("Non-inductive sets: " ^ commas_quote xs));
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   488
      val factors = mapfilter (fn (name, f) =>
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   489
        if name mem arg_vs then None
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   490
        else Some (name, (map (curry assoc fs) arg_vs, f))) fs;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   491
      val clauses =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   492
        foldl (add_clause (fs @ map (apsnd snd) extra_factors)) ([], intrs');
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   493
      val modes = infer_modes thy extra_modes factors arg_vs clauses;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   494
      val _ = print_factors factors;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   495
      val _ = print_modes modes;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   496
      val (gr'', s) = compile_preds thy gr' (hd ids) (terms_vs intrs') arg_vs
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   497
        (modes @ extra_modes) clauses;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   498
    in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   499
      (Graph.map_node (hd ids) (K (Some (Modes (modes, factors)), s)) gr'')
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   500
    end      
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   501
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   502
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   503
fun mk_ind_call thy gr dep t u is_query = (case head_of u of
13038
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   504
  Const (s, T) => (case (get_clauses thy s, get_assoc_code thy s T) of
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   505
       (None, _) => None
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   506
     | (Some (names, intrs), None) =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   507
         let
12565
9df4b3934487 Eliminated "query" syntax.
berghofe
parents: 12562
diff changeset
   508
          fun mk_mode (((ts, mode), i), Const ("dummy_pattern", _)) =
9df4b3934487 Eliminated "query" syntax.
berghofe
parents: 12562
diff changeset
   509
                ((ts, mode), i+1)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   510
            | mk_mode (((ts, mode), i), t) = ((ts @ [t], mode @ [i]), i+1);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   511
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   512
           val gr1 = mk_extra_defs thy
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   513
             (mk_ind_def thy gr dep names intrs) dep names [u];
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   514
           val (modes, factors) = lookup_modes gr1 dep;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   515
           val ts = split_prod [] (snd (the (assoc (factors, s)))) t;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   516
           val (ts', is) = if is_query then
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   517
               fst (foldl mk_mode ((([], []), 1), ts))
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   518
             else (ts, 1 upto length ts);
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   519
           val mode = (case find_first (fn Mode ((_, js), _) => is=js)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   520
                  (modes_of modes u) of
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   521
                None => error ("No such mode for " ^ s ^ ": " ^
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   522
                  string_of_mode ([], is))
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   523
              | mode => mode);
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   524
           val (gr2, in_ps) = foldl_map
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   525
             (invoke_codegen thy dep false) (gr1, ts');
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   526
           val (gr3, ps) = compile_expr thy dep false (gr2, (mode, u))
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   527
         in
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   528
           Some (gr3, Pretty.block
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   529
             (ps @ [Pretty.brk 1, mk_tuple in_ps]))
13038
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   530
         end
e968745986f1 - made modes_of more robust
berghofe
parents: 12565
diff changeset
   531
     | _ => None)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   532
  | _ => None);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   533
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   534
fun inductive_codegen thy gr dep brack (Const ("op :", _) $ t $ u) =
12565
9df4b3934487 Eliminated "query" syntax.
berghofe
parents: 12562
diff changeset
   535
      ((case mk_ind_call thy gr dep (Term.no_dummy_patterns t) u false of
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   536
         None => None
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   537
       | Some (gr', call_p) => Some (gr', (if brack then parens else I)
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   538
           (Pretty.block [Pretty.str "?! (", call_p, Pretty.str ")"])))
12565
9df4b3934487 Eliminated "query" syntax.
berghofe
parents: 12562
diff changeset
   539
        handle TERM _ => mk_ind_call thy gr dep t u true)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   540
  | inductive_codegen thy gr dep brack _ = None;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   541
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   542
val setup =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   543
  [add_codegen "inductive" inductive_codegen,
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   544
   CodegenData.init,
14195
e7c9206dd2ef Changed interface of add_attribute.
berghofe
parents: 14163
diff changeset
   545
   add_attribute "ind" (Scan.succeed add)];
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   546
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   547
end;
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   548
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   549
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   550
(**** combinators for code generated from inductive predicates ****)
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   551
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   552
infix 5 :->;
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   553
infix 3 ++;
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   554
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   555
fun s :-> f = Seq.flat (Seq.map f s);
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   556
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   557
fun s1 ++ s2 = Seq.append (s1, s2);
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   558
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   559
fun ?? b = if b then Seq.single () else Seq.empty;
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   560
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   561
fun ?! s = is_some (Seq.pull s);    
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   562
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   563
fun op__61__1 x = Seq.single x;
12453
806502073957 - Changed type of invoke_codegen
berghofe
parents: 11539
diff changeset
   564
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   565
val op__61__2 = op__61__1;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   566
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   567
fun op__61__1_2 (x, y) = ?? (x = y);