src/HOL/Tools/inductive_codegen.ML
author bulwahn
Thu, 25 Feb 2010 09:28:01 +0100
changeset 35378 95d0e3adf38e
parent 35021 c839a4c670c6
child 35382 598ee3a4c1aa
permissions -rw-r--r--
added basic reporting of test cases to quickcheck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24584
01e83ffa6c54 fixed title
haftmann
parents: 24458
diff changeset
     1
(*  Title:      HOL/Tools/inductive_codegen.ML
11539
0f17da240450 tuned headers;
wenzelm
parents: 11537
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     3
11539
0f17da240450 tuned headers;
wenzelm
parents: 11537
diff changeset
     4
Code generator for inductive predicates.
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     5
*)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     6
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     7
signature INDUCTIVE_CODEGEN =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
     8
sig
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
     9
  val add : string option -> int option -> attribute
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
    10
  val test_fn : (int * int * int -> term list option) Unsynchronized.ref
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35021
diff changeset
    11
  val test_term:
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35021
diff changeset
    12
    Proof.context -> bool -> term -> int -> term list option * (bool list * bool)
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18388
diff changeset
    13
  val setup : theory -> theory
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
    14
  val quickcheck_setup : theory -> theory
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    15
end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    16
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    17
structure InductiveCodegen : INDUCTIVE_CODEGEN =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    18
struct
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    19
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    20
open Codegen;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    21
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    22
(**** theory data ****)
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    23
18930
29d39c10822e replaced Symtab.merge_multi by local merge_rules;
wenzelm
parents: 18928
diff changeset
    24
fun merge_rules tabs =
22642
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    25
  Symtab.join (fn _ => AList.merge (Thm.eq_thm_prop) (K true)) tabs;
18930
29d39c10822e replaced Symtab.merge_multi by local merge_rules;
wenzelm
parents: 18928
diff changeset
    26
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33338
diff changeset
    27
structure CodegenData = Theory_Data
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22791
diff changeset
    28
(
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
    29
  type T =
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    30
    {intros : (thm * (string * int)) list Symtab.table,
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
    31
     graph : unit Graph.T,
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    32
     eqns : (thm * string) list Symtab.table};
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
    33
  val empty =
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
    34
    {intros = Symtab.empty, graph = Graph.empty, eqns = Symtab.empty};
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15660
diff changeset
    35
  val extend = I;
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33338
diff changeset
    36
  fun merge ({intros=intros1, graph=graph1, eqns=eqns1},
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33338
diff changeset
    37
    {intros=intros2, graph=graph2, eqns=eqns2}) : T =
18930
29d39c10822e replaced Symtab.merge_multi by local merge_rules;
wenzelm
parents: 18928
diff changeset
    38
    {intros = merge_rules (intros1, intros2),
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
    39
     graph = Graph.merge (K true) (graph1, graph2),
18930
29d39c10822e replaced Symtab.merge_multi by local merge_rules;
wenzelm
parents: 18928
diff changeset
    40
     eqns = merge_rules (eqns1, eqns2)};
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22791
diff changeset
    41
);
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    42
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    43
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    44
fun warn thm = warning ("InductiveCodegen: Not a proper clause:\n" ^
32091
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 31998
diff changeset
    45
  Display.string_of_thm_without_context thm);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
    46
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
    47
fun add_node x g = Graph.new_node (x, ()) g handle Graph.DUP _ => g;
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    48
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    49
fun add optmod optnparms = Thm.declaration_attribute (fn thm => Context.mapping (fn thy =>
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    50
  let
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    51
    val {intros, graph, eqns} = CodegenData.get thy;
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    52
    fun thyname_of s = (case optmod of
27398
768da1da59d6 simplified retrieval of theory names of consts and types
haftmann
parents: 26975
diff changeset
    53
      NONE => Codegen.thyname_of_const thy s | SOME s => s);
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    54
  in (case Option.map strip_comb (try HOLogic.dest_Trueprop (concl_of thm)) of
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    55
      SOME (Const ("op =", _), [t, _]) => (case head_of t of
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
    56
        Const (s, _) =>
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    57
          CodegenData.put {intros = intros, graph = graph,
22642
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    58
             eqns = eqns |> Symtab.map_default (s, [])
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    59
               (AList.update Thm.eq_thm_prop (thm, thyname_of s))} thy
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    60
      | _ => (warn thm; thy))
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    61
    | SOME (Const (s, _), _) =>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    62
        let
29287
5b0bfd63b5da eliminated OldTerm.(add_)term_consts;
wenzelm
parents: 29270
diff changeset
    63
          val cs = fold Term.add_const_names (Thm.prems_of thm) [];
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    64
          val rules = Symtab.lookup_list intros s;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    65
          val nparms = (case optnparms of
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    66
            SOME k => k
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    67
          | NONE => (case rules of
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30190
diff changeset
    68
             [] => (case try (Inductive.the_inductive (ProofContext.init thy)) s of
22791
992222f3d2eb Moved function params_of to inductive_package.ML.
berghofe
parents: 22661
diff changeset
    69
                 SOME (_, {raw_induct, ...}) =>
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30190
diff changeset
    70
                   length (Inductive.params_of raw_induct)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    71
               | NONE => 0)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    72
            | xs => snd (snd (snd (split_last xs)))))
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    73
        in CodegenData.put
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    74
          {intros = intros |>
22642
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    75
           Symtab.update (s, (AList.update Thm.eq_thm_prop
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    76
             (thm, (thyname_of s, nparms)) rules)),
33338
de76079f973a eliminated some old folds;
wenzelm
parents: 33317
diff changeset
    77
           graph = fold_rev (Graph.add_edge o pair s) cs (fold add_node (s :: cs) graph),
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    78
           eqns = eqns} thy
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    79
        end
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    80
    | _ => (warn thm; thy))
20897
3f8d2834b2c4 attribute: Context.mapping;
wenzelm
parents: 20071
diff changeset
    81
  end) I);
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    82
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
    83
fun get_clauses thy s =
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
    84
  let val {intros, graph, ...} = CodegenData.get thy
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
    85
  in case Symtab.lookup intros s of
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30190
diff changeset
    86
      NONE => (case try (Inductive.the_inductive (ProofContext.init thy)) s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
    87
        NONE => NONE
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
    88
      | SOME ({names, ...}, {intrs, raw_induct, ...}) =>
27398
768da1da59d6 simplified retrieval of theory names of consts and types
haftmann
parents: 26975
diff changeset
    89
          SOME (names, Codegen.thyname_of_const thy s,
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30190
diff changeset
    90
            length (Inductive.params_of raw_induct),
22661
f3ba63a2663e Removed erroneous application of rev in get_clauses that caused
berghofe
parents: 22642
diff changeset
    91
            preprocess thy intrs))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
    92
    | SOME _ =>
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    93
        let
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    94
          val SOME names = find_first
22642
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    95
            (fn xs => member (op =) xs s) (Graph.strong_conn graph);
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    96
          val intrs as (_, (thyname, nparms)) :: _ =
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    97
            maps (the o Symtab.lookup intros) names;
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
    98
        in SOME (names, thyname, nparms, preprocess thy (map fst (rev intrs))) end
14162
f05f299512e9 Fixed problem with "code ind" attribute that caused code generator to
berghofe
parents: 13105
diff changeset
    99
  end;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   100
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   101
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   102
(**** check if a term contains only constructor functions ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   103
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   104
fun is_constrt thy =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   105
  let
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
   106
    val cnstrs = flat (maps
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   107
      (map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd)
33963
977b94b64905 renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33771
diff changeset
   108
      (Symtab.dest (Datatype_Data.get_all thy)));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   109
    fun check t = (case strip_comb t of
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   110
        (Var _, []) => true
17521
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 17412
diff changeset
   111
      | (Const (s, _), ts) => (case AList.lookup (op =) cnstrs s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
   112
            NONE => false
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
   113
          | SOME i => length ts = i andalso forall check ts)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   114
      | _ => false)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   115
  in check end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   116
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   117
(**** check if a type is an equality type (i.e. doesn't contain fun) ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   118
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   119
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   120
  | is_eqT _ = true;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   121
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   122
(**** mode inference ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   123
15204
d15f85347fcb - Inserted additional check for equality types in check_mode_clause that
berghofe
parents: 15126
diff changeset
   124
fun string_of_mode (iss, is) = space_implode " -> " (map
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
   125
  (fn NONE => "X"
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
   126
    | SOME js => enclose "[" "]" (commas (map string_of_int js)))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
   127
       (iss @ [SOME is]));
15204
d15f85347fcb - Inserted additional check for equality types in check_mode_clause that
berghofe
parents: 15126
diff changeset
   128
d15f85347fcb - Inserted additional check for equality types in check_mode_clause that
berghofe
parents: 15126
diff changeset
   129
fun print_modes modes = message ("Inferred modes:\n" ^
26931
aa226d8405a8 cat_lines;
wenzelm
parents: 26928
diff changeset
   130
  cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   131
    (fn (m, rnd) => string_of_mode m ^
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   132
       (if rnd then " (random)" else "")) ms)) modes));
15204
d15f85347fcb - Inserted additional check for equality types in check_mode_clause that
berghofe
parents: 15126
diff changeset
   133
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28537
diff changeset
   134
val term_vs = map (fst o fst o dest_Var) o OldTerm.term_vars;
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   135
val terms_vs = distinct (op =) o maps term_vs;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   136
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   137
(** collect all Vars in a term (with duplicates!) **)
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16645
diff changeset
   138
fun term_vTs tm =
7446b4be013b tuned fold on terms;
wenzelm
parents: 16645
diff changeset
   139
  fold_aterms (fn Var ((x, _), T) => cons (x, T) | _ => I) tm [];
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   140
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   141
fun get_args _ _ [] = ([], [])
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   142
  | 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
   143
      (get_args is (i+1) xs);
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   144
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   145
fun merge xs [] = xs
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   146
  | merge [] ys = ys
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   147
  | 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
   148
      else y::merge (x::xs) ys;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   149
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   150
fun subsets i j = if i <= j then
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   151
       let val is = subsets (i+1) j
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   152
       in merge (map (fn ks => i::ks) is) is end
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   153
     else [[]];
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   154
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   155
fun cprod ([], ys) = []
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   156
  | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys);
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   157
30190
479806475f3c use long names for old-style fold combinators;
wenzelm
parents: 29287
diff changeset
   158
fun cprods xss = List.foldr (map op :: o cprod) [[]] xss;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   159
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   160
datatype mode = Mode of ((int list option list * int list) * bool) * int list * mode option list;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   161
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   162
fun needs_random (Mode ((_, b), _, ms)) =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   163
  b orelse exists (fn NONE => false | SOME m => needs_random m) ms;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   164
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   165
fun modes_of modes t =
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   166
  let
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   167
    val ks = 1 upto length (binder_types (fastype_of t));
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   168
    val default = [Mode ((([], ks), false), ks, [])];
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   169
    fun mk_modes name args = Option.map
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   170
     (maps (fn (m as ((iss, is), _)) =>
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   171
        let
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   172
          val (args1, args2) =
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   173
            if length args < length iss then
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   174
              error ("Too few arguments for inductive predicate " ^ name)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   175
            else chop (length iss) args;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   176
          val k = length args2;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   177
          val prfx = 1 upto k
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   178
        in
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   179
          if not (is_prefix op = prfx is) then [] else
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   180
          let val is' = map (fn i => i - k) (List.drop (is, k))
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   181
          in map (fn x => Mode (m, is', x)) (cprods (map
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   182
            (fn (NONE, _) => [NONE]
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33244
diff changeset
   183
              | (SOME js, arg) => map SOME (filter
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   184
                  (fn Mode (_, js', _) => js=js') (modes_of modes arg)))
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   185
                    (iss ~~ args1)))
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   186
          end
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   187
        end)) (AList.lookup op = 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, _])), _) =>
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   191
        [Mode ((([], [1]), false), [1], []), Mode ((([], [2]), false), [2], [])] @
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   192
        (if is_eqT T then [Mode ((([], [1, 2]), false), [1, 2], [])] else [])
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   193
    | (Const (name, _), args) => the_default default (mk_modes name args)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   194
    | (Var ((name, _), _), args) => the (mk_modes name args)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   195
    | (Free (name, _), args) => the (mk_modes name args)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   196
    | _ => default)
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   197
  end;
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   198
26806
40b411ec05aa Adapted to encoding of sets as predicates
berghofe
parents: 24625
diff changeset
   199
datatype indprem = Prem of term list * term * bool | Sidecond of term;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   200
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   201
fun missing_vars vs ts = subtract (fn (x, ((y, _), _)) => x = y) vs
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   202
  (fold Term.add_vars ts []);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   203
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   204
fun monomorphic_vars vs = null (fold (Term.add_tvarsT o snd) vs []);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   205
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   206
fun mode_ord p = int_ord (pairself (fn (Mode ((_, rnd), _, _), vs) =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   207
  length vs + (if null vs then 0 else 1) + (if rnd then 1 else 0)) p);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   208
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   209
fun select_mode_prem thy modes vs ps =
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   210
  sort (mode_ord o pairself (hd o snd))
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   211
    (filter_out (null o snd) (ps ~~ map
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   212
      (fn Prem (us, t, is_set) => sort mode_ord
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   213
          (List.mapPartial (fn m as Mode (_, is, _) =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   214
            let
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   215
              val (in_ts, out_ts) = get_args is 1 us;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   216
              val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   217
              val vTs = maps term_vTs out_ts';
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   218
              val dupTs = map snd (duplicates (op =) vTs) @
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   219
                map_filter (AList.lookup (op =) vTs) vs;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   220
              val missing_vs = missing_vars vs (t :: in_ts @ in_ts')
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   221
            in
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   222
              if forall (is_eqT o fastype_of) in_ts' andalso forall is_eqT dupTs
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   223
                andalso monomorphic_vars missing_vs
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   224
              then SOME (m, missing_vs)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   225
              else NONE
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   226
            end)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   227
              (if is_set then [Mode ((([], []), false), [], [])]
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   228
               else modes_of modes t handle Option =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   229
                 error ("Bad predicate: " ^ Syntax.string_of_term_global thy t)))
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   230
        | Sidecond t =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   231
            let val missing_vs = missing_vars vs [t]
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   232
            in
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   233
              if monomorphic_vars missing_vs
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   234
              then [(Mode ((([], []), false), [], []), missing_vs)]
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   235
              else []
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   236
            end)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   237
              ps));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   238
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   239
fun use_random () = "random_ind" mem !Codegen.mode;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   240
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   241
fun check_mode_clause thy arg_vs modes ((iss, is), rnd) (ts, ps) =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   242
  let
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   243
    val modes' = modes @ map_filter
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   244
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [(([], js), false)]))
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   245
        (arg_vs ~~ iss);
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   246
    fun check_mode_prems vs rnd [] = SOME (vs, rnd)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   247
      | check_mode_prems vs rnd ps = (case select_mode_prem thy modes' vs ps of
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   248
          (x, (m, []) :: _) :: _ => check_mode_prems
33042
ddf1f03a9ad9 curried union as canonical list operation
haftmann
parents: 33038
diff changeset
   249
            (case x of Prem (us, _, _) => union (op =) vs (terms_vs us) | _ => vs)
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   250
            (rnd orelse needs_random m)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   251
            (filter_out (equal x) ps)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   252
        | (_, (_, vs') :: _) :: _ =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   253
            if use_random () then
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   254
              check_mode_prems (union (op =) vs (map (fst o fst) vs')) true ps
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   255
            else NONE
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   256
        | _ => NONE);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   257
    val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (get_args is 1 ts));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   258
    val in_vs = terms_vs in_ts;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   259
  in
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   260
    if forall is_eqT (map snd (duplicates (op =) (maps term_vTs in_ts))) andalso
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   261
      forall (is_eqT o fastype_of) in_ts'
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   262
    then (case check_mode_prems (union (op =) arg_vs in_vs) rnd ps of
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   263
       NONE => NONE
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   264
     | SOME (vs, rnd') =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   265
         let val missing_vs = missing_vars vs ts
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   266
         in
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   267
           if null missing_vs orelse
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   268
             use_random () andalso monomorphic_vars missing_vs
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   269
           then SOME (rnd' orelse not (null missing_vs))
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   270
           else NONE
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   271
         end)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   272
    else NONE
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   273
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   274
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   275
fun check_modes_pred thy arg_vs preds modes (p, ms) =
17521
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 17412
diff changeset
   276
  let val SOME rs = AList.lookup (op =) preds p
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   277
  in (p, List.mapPartial (fn m as (m', _) =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   278
    let val xs = map (check_mode_clause thy arg_vs modes m) rs
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   279
    in case find_index is_none xs of
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   280
        ~1 => SOME (m', exists (fn SOME b => b) xs)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   281
      | i => (message ("Clause " ^ string_of_int (i+1) ^ " of " ^
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   282
        p ^ " violates mode " ^ string_of_mode m'); NONE)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   283
    end) ms)
15204
d15f85347fcb - Inserted additional check for equality types in check_mode_clause that
berghofe
parents: 15126
diff changeset
   284
  end;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   285
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   286
fun fixp f (x : (string * ((int list option list * int list) * bool) list) list) =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   287
  let val y = f x
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   288
  in if x = y then x else fixp f y end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   289
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   290
fun infer_modes thy extra_modes arities arg_vs preds = fixp (fn modes =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   291
  map (check_modes_pred thy arg_vs preds (modes @ extra_modes)) modes)
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   292
    (map (fn (s, (ks, k)) => (s, map (rpair false) (cprod (cprods (map
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
   293
      (fn NONE => [NONE]
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   294
        | SOME k' => map SOME (subsets 1 k')) ks),
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   295
      subsets 1 k)))) arities);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   296
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   297
(**** code generation ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   298
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   299
fun mk_eq (x::xs) =
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   300
  let fun mk_eqs _ [] = []
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   301
        | mk_eqs a (b::cs) = str (a ^ " = " ^ b) :: mk_eqs b cs
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   302
  in mk_eqs x xs end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   303
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   304
fun mk_tuple xs = Pretty.block (str "(" ::
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   305
  flat (separate [str ",", Pretty.brk 1] (map single xs)) @
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   306
  [str ")"]);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   307
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   308
fun mk_v s (names, vs) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   309
  (case AList.lookup (op =) vs s of
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   310
    NONE => (s, (names, (s, [s])::vs))
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   311
  | SOME xs =>
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   312
      let val s' = Name.variant names s
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   313
      in (s', (s'::names, AList.update (op =) (s, s'::xs) vs)) end);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   314
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   315
fun distinct_v (Var ((s, 0), T)) nvs =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   316
      let val (s', nvs') = mk_v s nvs
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   317
      in (Var ((s', 0), T), nvs') end
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   318
  | distinct_v (t $ u) nvs =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   319
      let
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   320
        val (t', nvs') = distinct_v t nvs;
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   321
        val (u', nvs'') = distinct_v u nvs';
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   322
      in (t' $ u', nvs'') end
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   323
  | distinct_v t nvs = (t, nvs);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   324
15061
61a52739cd82 Added simple check that allows code generator to produce code containing
berghofe
parents: 15031
diff changeset
   325
fun is_exhaustive (Var _) = true
61a52739cd82 Added simple check that allows code generator to produce code containing
berghofe
parents: 15031
diff changeset
   326
  | is_exhaustive (Const ("Pair", _) $ t $ u) =
61a52739cd82 Added simple check that allows code generator to produce code containing
berghofe
parents: 15031
diff changeset
   327
      is_exhaustive t andalso is_exhaustive u
61a52739cd82 Added simple check that allows code generator to produce code containing
berghofe
parents: 15031
diff changeset
   328
  | is_exhaustive _ = false;
61a52739cd82 Added simple check that allows code generator to produce code containing
berghofe
parents: 15031
diff changeset
   329
61a52739cd82 Added simple check that allows code generator to produce code containing
berghofe
parents: 15031
diff changeset
   330
fun compile_match nvs eq_ps out_ps success_p can_fail =
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   331
  let val eqs = flat (separate [str " andalso", Pretty.brk 1]
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   332
    (map single (maps (mk_eq o snd) nvs @ eq_ps)));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   333
  in
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   334
    Pretty.block
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   335
     ([str "(fn ", mk_tuple out_ps, str " =>", Pretty.brk 1] @
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   336
      (Pretty.block ((if eqs=[] then [] else str "if " ::
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   337
         [Pretty.block eqs, Pretty.brk 1, str "then "]) @
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   338
         (success_p ::
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   339
          (if eqs=[] then [] else [Pretty.brk 1, str "else DSeq.empty"]))) ::
15061
61a52739cd82 Added simple check that allows code generator to produce code containing
berghofe
parents: 15031
diff changeset
   340
       (if can_fail then
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   341
          [Pretty.brk 1, str "| _ => DSeq.empty)"]
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   342
        else [str ")"])))
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   343
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   344
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   345
fun modename module s (iss, is) gr =
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   346
  let val (id, gr') = if s = @{const_name "op ="} then (("", "equal"), gr)
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   347
    else mk_const_id module s gr
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   348
  in (space_implode "__"
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   349
    (mk_qual_id module id ::
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   350
      map (space_implode "_" o map string_of_int) (map_filter I iss @ [is])), gr')
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   351
  end;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   352
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   353
fun mk_funcomp brack s k p = (if brack then parens else I)
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   354
  (Pretty.block [Pretty.block ((if k = 0 then [] else [str "("]) @
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   355
    separate (Pretty.brk 1) (str s :: replicate k (str "|> ???")) @
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   356
    (if k = 0 then [] else [str ")"])), Pretty.brk 1, p]);
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   357
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   358
fun compile_expr thy defs dep module brack modes (NONE, t) gr =
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   359
      apfst single (invoke_codegen thy defs dep module brack t gr)
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   360
  | compile_expr _ _ _ _ _ _ (SOME _, Var ((name, _), _)) gr =
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   361
      ([str name], gr)
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   362
  | compile_expr thy defs dep module brack modes (SOME (Mode ((mode, _), _, ms)), t) gr =
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   363
      (case strip_comb t of
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   364
         (Const (name, _), args) =>
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   365
           if name = @{const_name "op ="} orelse AList.defined op = modes name then
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   366
             let
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   367
               val (args1, args2) = chop (length ms) args;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   368
               val ((ps, mode_id), gr') = gr |> fold_map
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   369
                   (compile_expr thy defs dep module true modes) (ms ~~ args1)
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   370
                   ||>> modename module name mode;
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   371
               val (ps', gr'') = (case mode of
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   372
                   ([], []) => ([str "()"], gr')
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   373
                 | _ => fold_map
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   374
                     (invoke_codegen thy defs dep module true) args2 gr')
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   375
             in ((if brack andalso not (null ps andalso null ps') then
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   376
               single o parens o Pretty.block else I)
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   377
                 (flat (separate [Pretty.brk 1]
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   378
                   ([str mode_id] :: ps @ map single ps'))), gr')
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   379
             end
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   380
           else apfst (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   381
             (invoke_codegen thy defs dep module true t gr)
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   382
       | _ => apfst (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   383
           (invoke_codegen thy defs dep module true t gr));
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   384
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   385
fun compile_clause thy defs dep module all_vs arg_vs modes (iss, is) (ts, ps) inp gr =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   386
  let
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   387
    val modes' = modes @ map_filter
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   388
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [(([], js), false)]))
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   389
        (arg_vs ~~ iss);
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   390
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   391
    fun check_constrt t (names, eqs) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   392
      if is_constrt thy t then (t, (names, eqs))
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   393
      else
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19861
diff changeset
   394
        let val s = Name.variant names "x";
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   395
        in (Var ((s, 0), fastype_of t), (s::names, (s, t)::eqs)) end;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   396
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   397
    fun compile_eq (s, t) gr =
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   398
      apfst (Pretty.block o cons (str (s ^ " = ")) o single)
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   399
        (invoke_codegen thy defs dep module false t gr);
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   400
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   401
    val (in_ts, out_ts) = get_args is 1 ts;
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   402
    val (in_ts', (all_vs', eqs)) = fold_map check_constrt in_ts (all_vs, []);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   403
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   404
    fun compile_prems out_ts' vs names [] gr =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   405
          let
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   406
            val (out_ps, gr2) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   407
              fold_map (invoke_codegen thy defs dep module false) out_ts gr;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   408
            val (eq_ps, gr3) = fold_map compile_eq eqs gr2;
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   409
            val (out_ts'', (names', eqs')) = fold_map check_constrt out_ts' (names, []);
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   410
            val (out_ts''', nvs) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   411
              fold_map distinct_v out_ts'' (names', map (fn x => (x, [x])) vs);
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   412
            val (out_ps', gr4) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   413
              fold_map (invoke_codegen thy defs dep module false) out_ts''' gr3;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   414
            val (eq_ps', gr5) = fold_map compile_eq eqs' gr4;
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   415
            val vs' = distinct (op =) (flat (vs :: map term_vs out_ts'));
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   416
            val missing_vs = missing_vars vs' out_ts;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   417
            val final_p = Pretty.block
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   418
              [str "DSeq.single", Pretty.brk 1, mk_tuple out_ps]
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   419
          in
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   420
            if null missing_vs then
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   421
              (compile_match (snd nvs) (eq_ps @ eq_ps') out_ps'
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   422
                 final_p (exists (not o is_exhaustive) out_ts'''), gr5)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   423
            else
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   424
              let
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   425
                val (pat_p, gr6) = invoke_codegen thy defs dep module true
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   426
                  (HOLogic.mk_tuple (map Var missing_vs)) gr5;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   427
                val gen_p = mk_gen gr6 module true [] ""
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   428
                  (HOLogic.mk_tupleT (map snd missing_vs))
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   429
              in
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   430
                (compile_match (snd nvs) eq_ps' out_ps'
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   431
                   (Pretty.block [str "DSeq.generator ", gen_p,
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   432
                      str " :->", Pretty.brk 1,
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   433
                      compile_match [] eq_ps [pat_p] final_p false])
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   434
                   (exists (not o is_exhaustive) out_ts'''),
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   435
                 gr6)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   436
              end
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   437
          end
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   438
      | compile_prems out_ts vs names ps gr =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   439
          let
31852
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   440
            val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   441
            val (out_ts', (names', eqs)) = fold_map check_constrt out_ts (names, []);
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   442
            val (out_ts'', nvs) = fold_map distinct_v out_ts' (names', map (fn x => (x, [x])) vs);
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   443
            val (out_ps, gr0) = fold_map (invoke_codegen thy defs dep module false) out_ts'' gr;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   444
            val (eq_ps, gr1) = fold_map compile_eq eqs gr0;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   445
          in
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   446
            (case hd (select_mode_prem thy modes' vs' ps) of
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   447
               (p as Prem (us, t, is_set), (mode as Mode (_, js, _), []) :: _) =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   448
                 let
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   449
                   val ps' = filter_out (equal p) ps;
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   450
                   val (in_ts, out_ts''') = get_args js 1 us;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   451
                   val (in_ps, gr2) = fold_map
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   452
                     (invoke_codegen thy defs dep module true) in_ts gr1;
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   453
                   val (ps, gr3) =
26806
40b411ec05aa Adapted to encoding of sets as predicates
berghofe
parents: 24625
diff changeset
   454
                     if not is_set then
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   455
                       apfst (fn ps => ps @
24129
591394d9ee66 - Added cycle test to function mk_ind_def to avoid non-termination
berghofe
parents: 23761
diff changeset
   456
                           (if null in_ps then [] else [Pretty.brk 1]) @
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   457
                           separate (Pretty.brk 1) in_ps)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   458
                         (compile_expr thy defs dep module false modes
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   459
                           (SOME mode, t) gr2)
26806
40b411ec05aa Adapted to encoding of sets as predicates
berghofe
parents: 24625
diff changeset
   460
                     else
31852
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   461
                       apfst (fn p => Pretty.breaks [str "DSeq.of_list", str "(case", p,
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   462
                         str "of", str "Set", str "xs", str "=>", str "xs)"])
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   463
                         (*this is a very strong assumption about the generated code!*)
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   464
                           (invoke_codegen thy defs dep module true t gr2);
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   465
                   val (rest, gr4) = compile_prems out_ts''' vs' (fst nvs) ps' gr3;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   466
                 in
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   467
                   (compile_match (snd nvs) eq_ps out_ps
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   468
                      (Pretty.block (ps @
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   469
                         [str " :->", Pretty.brk 1, rest]))
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   470
                      (exists (not o is_exhaustive) out_ts''), gr4)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   471
                 end
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   472
             | (p as Sidecond t, [(_, [])]) =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   473
                 let
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   474
                   val ps' = filter_out (equal p) ps;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   475
                   val (side_p, gr2) = invoke_codegen thy defs dep module true t gr1;
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   476
                   val (rest, gr3) = compile_prems [] vs' (fst nvs) ps' gr2;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   477
                 in
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   478
                   (compile_match (snd nvs) eq_ps out_ps
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   479
                      (Pretty.block [str "?? ", side_p,
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   480
                        str " :->", Pretty.brk 1, rest])
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   481
                      (exists (not o is_exhaustive) out_ts''), gr3)
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   482
                 end
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   483
             | (_, (_, missing_vs) :: _) =>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   484
                 let
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   485
                   val T = HOLogic.mk_tupleT (map snd missing_vs);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   486
                   val (_, gr2) = invoke_tycodegen thy defs dep module false T gr1;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   487
                   val gen_p = mk_gen gr2 module true [] "" T;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   488
                   val (rest, gr3) = compile_prems
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   489
                     [HOLogic.mk_tuple (map Var missing_vs)] vs' (fst nvs) ps gr2
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   490
                 in
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   491
                   (compile_match (snd nvs) eq_ps out_ps
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   492
                      (Pretty.block [str "DSeq.generator", Pretty.brk 1,
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   493
                        gen_p, str " :->", Pretty.brk 1, rest])
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   494
                      (exists (not o is_exhaustive) out_ts''), gr3)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   495
                 end)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   496
          end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   497
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   498
    val (prem_p, gr') = compile_prems in_ts' arg_vs all_vs' ps gr ;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   499
  in
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   500
    (Pretty.block [str "DSeq.single", Pretty.brk 1, inp,
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   501
       str " :->", Pretty.brk 1, prem_p], gr')
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   502
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   503
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   504
fun compile_pred thy defs dep module prfx all_vs arg_vs modes s cls mode gr =
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   505
  let
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   506
    val xs = map str (Name.variant_list arg_vs
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   507
      (map (fn i => "x" ^ string_of_int i) (snd mode)));
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   508
    val ((cl_ps, mode_id), gr') = gr |>
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   509
      fold_map (fn cl => compile_clause thy defs
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   510
        dep module all_vs arg_vs modes mode cl (mk_tuple xs)) cls ||>>
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   511
      modename module s mode
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   512
  in
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   513
    (Pretty.block
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   514
      ([Pretty.block (separate (Pretty.brk 1)
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   515
         (str (prfx ^ mode_id) ::
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   516
           map str arg_vs @
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   517
           (case mode of ([], []) => [str "()"] | _ => xs)) @
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   518
         [str " ="]),
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   519
        Pretty.brk 1] @
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   520
       flat (separate [str " ++", Pretty.brk 1] (map single cl_ps))), (gr', "and "))
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   521
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   522
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   523
fun compile_preds thy defs dep module all_vs arg_vs modes preds gr =
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   524
  let val (prs, (gr', _)) = fold_map (fn (s, cls) =>
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   525
    fold_map (fn (mode, _) => fn (gr', prfx') => compile_pred thy defs
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   526
      dep module prfx' all_vs arg_vs modes s cls mode gr')
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   527
        (((the o AList.lookup (op =) modes) s))) preds (gr, "fun ")
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   528
  in
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   529
    (space_implode "\n\n" (map string_of (flat prs)) ^ ";\n\n", gr')
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   530
  end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   531
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   532
(**** processing of introduction rules ****)
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   533
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   534
exception Modes of
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   535
  (string * ((int list option list * int list) * bool) list) list *
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   536
  (string * (int option list * int)) list;
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   537
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   538
fun lookup_modes gr dep = apfst flat (apsnd flat (ListPair.unzip
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   539
  (map ((fn (SOME (Modes x), _, _) => x | _ => ([], [])) o get_node gr)
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   540
    (Graph.all_preds (fst gr) [dep]))));
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   541
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   542
fun print_arities arities = message ("Arities:\n" ^
26931
aa226d8405a8 cat_lines;
wenzelm
parents: 26928
diff changeset
   543
  cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   544
    space_implode " -> " (map
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   545
      (fn NONE => "X" | SOME k' => string_of_int k')
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   546
        (ks @ [SOME k]))) arities));
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   547
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 34962
diff changeset
   548
fun prep_intrs intrs = map (rename_term o #prop o rep_thm o Drule.export_without_context) intrs;
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   549
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   550
fun constrain cs [] = []
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   551
  | constrain cs ((s, xs) :: ys) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   552
      (s,
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   553
        case AList.lookup (op =) cs (s : string) of
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   554
          NONE => xs
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   555
        | SOME xs' => inter (op = o apfst fst) xs' xs) :: constrain cs ys;
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   556
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   557
fun mk_extra_defs thy defs gr dep names module ts =
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   558
  fold (fn name => fn gr =>
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   559
    if name mem names then gr
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   560
    else
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   561
      (case get_clauses thy name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15257
diff changeset
   562
        NONE => gr
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   563
      | SOME (names, thyname, nparms, intrs) =>
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   564
          mk_ind_def thy defs gr dep names (if_library thyname module)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   565
            [] (prep_intrs intrs) nparms))
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   566
    (fold Term.add_const_names ts []) gr
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   567
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   568
and mk_ind_def thy defs gr dep names module modecs intrs nparms =
24129
591394d9ee66 - Added cycle test to function mk_ind_def to avoid non-termination
berghofe
parents: 23761
diff changeset
   569
  add_edge_acyclic (hd names, dep) gr handle
591394d9ee66 - Added cycle test to function mk_ind_def to avoid non-termination
berghofe
parents: 23761
diff changeset
   570
    Graph.CYCLES (xs :: _) =>
591394d9ee66 - Added cycle test to function mk_ind_def to avoid non-termination
berghofe
parents: 23761
diff changeset
   571
      error ("InductiveCodegen: illegal cyclic dependencies:\n" ^ commas xs)
591394d9ee66 - Added cycle test to function mk_ind_def to avoid non-termination
berghofe
parents: 23761
diff changeset
   572
  | Graph.UNDEF _ =>
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   573
    let
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   574
      val _ $ u = Logic.strip_imp_concl (hd intrs);
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   575
      val args = List.take (snd (strip_comb u), nparms);
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32342
diff changeset
   576
      val arg_vs = maps term_vs args;
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   577
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   578
      fun get_nparms s = if s mem names then SOME nparms else
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   579
        Option.map #3 (get_clauses thy s);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   580
26806
40b411ec05aa Adapted to encoding of sets as predicates
berghofe
parents: 24625
diff changeset
   581
      fun dest_prem (_ $ (Const ("op :", _) $ t $ u)) = Prem ([t], Envir.beta_eta_contract u, true)
40b411ec05aa Adapted to encoding of sets as predicates
berghofe
parents: 24625
diff changeset
   582
        | dest_prem (_ $ ((eq as Const ("op =", _)) $ t $ u)) = Prem ([t, u], eq, false)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   583
        | dest_prem (_ $ t) =
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   584
            (case strip_comb t of
26806
40b411ec05aa Adapted to encoding of sets as predicates
berghofe
parents: 24625
diff changeset
   585
               (v as Var _, ts) => if v mem args then Prem (ts, v, false) else Sidecond t
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   586
             | (c as Const (s, _), ts) => (case get_nparms s of
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   587
                 NONE => Sidecond t
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   588
               | SOME k =>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   589
                   let val (ts1, ts2) = chop k ts
26806
40b411ec05aa Adapted to encoding of sets as predicates
berghofe
parents: 24625
diff changeset
   590
                   in Prem (ts2, list_comb (c, ts1), false) end)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   591
             | _ => Sidecond t);
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   592
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   593
      fun add_clause intr (clauses, arities) =
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   594
        let
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   595
          val _ $ t = Logic.strip_imp_concl intr;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   596
          val (Const (name, T), ts) = strip_comb t;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   597
          val (ts1, ts2) = chop nparms ts;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   598
          val prems = map dest_prem (Logic.strip_imp_prems intr);
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   599
          val (Ts, Us) = chop nparms (binder_types T)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   600
        in
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   601
          (AList.update op = (name, these (AList.lookup op = clauses name) @
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   602
             [(ts2, prems)]) clauses,
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   603
           AList.update op = (name, (map (fn U => (case strip_type U of
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   604
                 (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   605
               | _ => NONE)) Ts,
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   606
             length Us)) arities)
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   607
        end;
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   608
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   609
      val gr' = mk_extra_defs thy defs
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   610
        (add_edge (hd names, dep)
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   611
          (new_node (hd names, (NONE, "", "")) gr)) (hd names) names module intrs;
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   612
      val (extra_modes, extra_arities) = lookup_modes gr' (hd names);
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   613
      val (clauses, arities) = fold add_clause intrs ([], []);
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   614
      val modes = constrain modecs
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   615
        (infer_modes thy extra_modes arities arg_vs clauses);
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   616
      val _ = print_arities arities;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   617
      val _ = print_modes modes;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   618
      val (s, gr'') = compile_preds thy defs (hd names) module (terms_vs intrs)
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   619
        arg_vs (modes @ extra_modes) clauses gr';
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   620
    in
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   621
      (map_node (hd names)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   622
        (K (SOME (Modes (modes, arities)), module, s)) gr'')
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   623
    end;
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   624
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   625
fun find_mode gr dep s u modes is = (case find_first (fn Mode (_, js, _) => is=js)
15660
255055554c67 handle Option instead of OPTION;
wenzelm
parents: 15574
diff changeset
   626
  (modes_of modes u handle Option => []) of
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   627
     NONE => codegen_error gr dep
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   628
       ("No such mode for " ^ s ^ ": " ^ string_of_mode ([], is))
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   629
   | mode => mode);
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   630
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   631
fun mk_ind_call thy defs dep module is_query s T ts names thyname k intrs gr =
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   632
  let
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   633
    val (ts1, ts2) = chop k ts;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   634
    val u = list_comb (Const (s, T), ts1);
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   635
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   636
    fun mk_mode (Const ("dummy_pattern", _)) ((ts, mode), i) = ((ts, mode), i + 1)
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   637
      | mk_mode t ((ts, mode), i) = ((ts @ [t], mode @ [i]), i + 1);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   638
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   639
    val module' = if_library thyname module;
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   640
    val gr1 = mk_extra_defs thy defs
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   641
      (mk_ind_def thy defs gr dep names module'
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   642
      [] (prep_intrs intrs) k) dep names module' [u];
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   643
    val (modes, _) = lookup_modes gr1 dep;
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   644
    val (ts', is) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   645
      if is_query then fst (fold mk_mode ts2 (([], []), 1))
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   646
      else (ts2, 1 upto length (binder_types T) - k);
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   647
    val mode = find_mode gr1 dep s u modes is;
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   648
    val _ = if is_query orelse not (needs_random (the mode)) then ()
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   649
      else warning ("Illegal use of random data generators in " ^ s);
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   650
    val (in_ps, gr2) = fold_map (invoke_codegen thy defs dep module true) ts' gr1;
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   651
    val (ps, gr3) = compile_expr thy defs dep module false modes (mode, u) gr2;
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   652
  in
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   653
    (Pretty.block (ps @ (if null in_ps then [] else [Pretty.brk 1]) @
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   654
       separate (Pretty.brk 1) in_ps), gr3)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   655
  end;
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   656
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   657
fun clause_of_eqn eqn =
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   658
  let
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   659
    val (t, u) = HOLogic.dest_eq (HOLogic.dest_Trueprop (concl_of eqn));
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   660
    val (Const (s, T), ts) = strip_comb t;
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   661
    val (Ts, U) = strip_type T
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   662
  in
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   663
    rename_term (Logic.list_implies (prems_of eqn, HOLogic.mk_Trueprop
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   664
      (list_comb (Const (s ^ "_aux", Ts @ [U] ---> HOLogic.boolT), ts @ [u]))))
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   665
  end;
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   666
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   667
fun mk_fun thy defs name eqns dep module module' gr =
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   668
  case try (get_node gr) name of
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   669
    NONE =>
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   670
    let
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   671
      val clauses = map clause_of_eqn eqns;
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   672
      val pname = name ^ "_aux";
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   673
      val arity = length (snd (strip_comb (fst (HOLogic.dest_eq
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   674
        (HOLogic.dest_Trueprop (concl_of (hd eqns)))))));
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   675
      val mode = 1 upto arity;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   676
      val ((fun_id, mode_id), gr') = gr |>
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   677
        mk_const_id module' name ||>>
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   678
        modename module' pname ([], mode);
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   679
      val vars = map (fn i => str ("x" ^ string_of_int i)) mode;
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   680
      val s = string_of (Pretty.block
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   681
        [mk_app false (str ("fun " ^ snd fun_id)) vars, str " =",
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   682
         Pretty.brk 1, str "DSeq.hd", Pretty.brk 1,
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   683
         parens (Pretty.block (separate (Pretty.brk 1) (str mode_id ::
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   684
           vars)))]) ^ ";\n\n";
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   685
      val gr'' = mk_ind_def thy defs (add_edge (name, dep)
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   686
        (new_node (name, (NONE, module', s)) gr')) name [pname] module'
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   687
        [(pname, [([], mode)])] clauses 0;
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   688
      val (modes, _) = lookup_modes gr'' dep;
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   689
      val _ = find_mode gr'' dep pname (head_of (HOLogic.dest_Trueprop
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   690
        (Logic.strip_imp_concl (hd clauses)))) modes mode
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   691
    in (mk_qual_id module fun_id, gr'') end
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16861
diff changeset
   692
  | SOME _ =>
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   693
      (mk_qual_id module (get_const_id gr name), add_edge (name, dep) gr);
15031
726dc9146bb1 - Added support for conditional equations whose premises involve
berghofe
parents: 14981
diff changeset
   694
23761
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   695
(* convert n-tuple to nested pairs *)
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   696
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   697
fun conv_ntuple fs ts p =
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   698
  let
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   699
    val k = length fs;
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33050
diff changeset
   700
    val xs = map_range (fn i => str ("x" ^ string_of_int i)) (k + 1);
23761
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   701
    val xs' = map (fn Bound i => nth xs (k - i)) ts;
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   702
    fun conv xs js =
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   703
      if js mem fs then
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   704
        let
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   705
          val (p, xs') = conv xs (1::js);
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   706
          val (q, xs'') = conv xs' (2::js)
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   707
        in (mk_tuple [p, q], xs'') end
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   708
      else (hd xs, tl xs)
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   709
  in
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   710
    if k > 0 then
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   711
      Pretty.block
26975
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   712
        [str "DSeq.map (fn", Pretty.brk 1,
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   713
         mk_tuple xs', str " =>", Pretty.brk 1, fst (conv xs []),
103dca19ef2e Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents: 26939
diff changeset
   714
         str ")", Pretty.brk 1, parens p]
23761
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   715
    else p
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   716
  end;
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   717
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   718
fun inductive_codegen thy defs dep module brack t gr  = (case strip_comb t of
23761
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   719
    (Const ("Collect", _), [u]) =>
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
   720
      let val (r, Ts, fs) = HOLogic.strip_psplits u
23761
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   721
      in case strip_comb r of
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   722
          (Const (s, T), ts) =>
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   723
            (case (get_clauses thy s, get_assoc_code thy (s, T)) of
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   724
              (SOME (names, thyname, k, intrs), NONE) =>
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   725
                let
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   726
                  val (ts1, ts2) = chop k ts;
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   727
                  val ts2' = map
34962
807f6ce0273d HOLogic.strip_psplits: types are ordered after syntactic appearance, not after corresponding de-Bruin index (closer correspondence to similar strip operations)
haftmann
parents: 33963
diff changeset
   728
                    (fn Bound i => Term.dummy_pattern (nth Ts (length Ts - i - 1)) | t => t) ts2;
23761
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   729
                  val (ots, its) = List.partition is_Bound ts2;
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   730
                  val no_loose = forall (fn t => not (loose_bvar (t, 0)))
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   731
                in
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   732
                  if null (duplicates op = ots) andalso
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   733
                    no_loose ts1 andalso no_loose its
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   734
                  then
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   735
                    let val (call_p, gr') = mk_ind_call thy defs dep module true
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   736
                      s T (ts1 @ ts2') names thyname k intrs gr 
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   737
                    in SOME ((if brack then parens else I) (Pretty.block
31852
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   738
                      [str "Set", Pretty.brk 1, str "(DSeq.list_of", Pretty.brk 1, str "(",
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   739
                       conv_ntuple fs ots call_p, str "))"]),
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   740
                       (*this is a very strong assumption about the generated code!*)
a16bb835e97d explicit Set constructor for code generated for sets
haftmann
parents: 31784
diff changeset
   741
                       gr')
23761
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   742
                    end
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   743
                  else NONE
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   744
                end
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   745
            | _ => NONE)
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   746
        | _ => NONE
9cebbaccf8a2 Improved code generator for Collect.
berghofe
parents: 22921
diff changeset
   747
      end
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   748
  | (Const (s, T), ts) => (case Symtab.lookup (#eqns (CodegenData.get thy)) s of
22921
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22846
diff changeset
   749
      NONE => (case (get_clauses thy s, get_assoc_code thy (s, T)) of
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   750
        (SOME (names, thyname, k, intrs), NONE) =>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   751
          if length ts < k then NONE else SOME
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   752
            (let val (call_p, gr') = mk_ind_call thy defs dep module false
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   753
               s T (map Term.no_dummy_patterns ts) names thyname k intrs gr
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   754
             in (mk_funcomp brack "?!"
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   755
               (length (binder_types T) - length ts) (parens call_p), gr')
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   756
             end handle TERM _ => mk_ind_call thy defs dep module true
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   757
               s T ts names thyname k intrs gr )
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   758
      | _ => NONE)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   759
    | SOME eqns =>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   760
        let
22642
bfdb29f11eb4 canonical merge operations
haftmann
parents: 22556
diff changeset
   761
          val (_, thyname) :: _ = eqns;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   762
          val (id, gr') = mk_fun thy defs s (preprocess thy (map fst (rev eqns)))
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   763
            dep module (if_library thyname module) gr;
28537
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   764
          val (ps, gr'') = fold_map
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   765
            (invoke_codegen thy defs dep module true) ts gr';
1e84256d1a8a established canonical argument order in SML code generators
haftmann
parents: 27809
diff changeset
   766
        in SOME (mk_app brack (str id) ps, gr'')
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   767
        end)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21022
diff changeset
   768
  | _ => NONE);
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   769
12557
bb2e4689347e Implemented higher order modes.
berghofe
parents: 12453
diff changeset
   770
val setup =
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18388
diff changeset
   771
  add_codegen "inductive" inductive_codegen #>
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   772
  Attrib.setup @{binding code_ind}
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   773
    (Scan.lift (Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) --
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33063
diff changeset
   774
      Scan.option (Args.$$$ "params" |-- Args.colon |-- OuterParse.nat) >> uncurry add))
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31852
diff changeset
   775
    "introduction rules for executable predicates";
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   776
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   777
(**** Quickcheck involving inductive predicates ****)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   778
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   779
val test_fn : (int * int * int -> term list option) Unsynchronized.ref =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   780
  Unsynchronized.ref (fn _ => NONE);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   781
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   782
fun strip_imp p =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   783
  let val (q, r) = HOLogic.dest_imp p
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   784
  in strip_imp r |>> cons q end
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   785
  handle TERM _ => ([], p);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   786
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   787
fun deepen bound f i =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   788
  if i > bound then NONE
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   789
  else (case f i of
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   790
      NONE => deepen bound f (i + 1)
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   791
    | SOME x => SOME x);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   792
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   793
val depth_bound_value =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   794
  Config.declare false "ind_quickcheck_depth" (Config.Int 10);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   795
val depth_bound = Config.int depth_bound_value;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   796
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   797
val depth_start_value =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   798
  Config.declare false "ind_quickcheck_depth_start" (Config.Int 1);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   799
val depth_start = Config.int depth_start_value;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   800
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   801
val random_values_value =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   802
  Config.declare false "ind_quickcheck_random" (Config.Int 5);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   803
val random_values = Config.int random_values_value;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   804
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   805
val size_offset_value =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   806
  Config.declare false "ind_quickcheck_size_offset" (Config.Int 0);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   807
val size_offset = Config.int size_offset_value;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   808
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35021
diff changeset
   809
fun test_term ctxt report t =
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   810
  let
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   811
    val thy = ProofContext.theory_of ctxt;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   812
    val (xs, p) = strip_abs t;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   813
    val args' = map_index (fn (i, (_, T)) => ("arg" ^ string_of_int i, T)) xs;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   814
    val args = map Free args';
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   815
    val (ps, q) = strip_imp p;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   816
    val Ts = map snd xs;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   817
    val T = Ts ---> HOLogic.boolT;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   818
    val rl = Logic.list_implies
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   819
      (map (HOLogic.mk_Trueprop o curry subst_bounds (rev args)) ps @
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   820
       [HOLogic.mk_Trueprop (HOLogic.mk_not (subst_bounds (rev args, q)))],
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   821
       HOLogic.mk_Trueprop (list_comb (Free ("quickcheckp", T), args)));
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   822
    val (_, thy') = Inductive.add_inductive_global
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   823
      {quiet_mode=true, verbose=false, alt_name=Binding.empty, coind=false,
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   824
       no_elim=true, no_ind=false, skip_mono=false, fork_mono=false}
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   825
      [((Binding.name "quickcheckp", T), NoSyn)] []
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   826
      [(Attrib.empty_binding, rl)] [] (Theory.copy thy);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   827
    val pred = HOLogic.mk_Trueprop (list_comb
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   828
      (Const (Sign.intern_const thy' "quickcheckp", T),
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   829
       map Term.dummy_pattern Ts));
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   830
    val (code, gr) = setmp_CRITICAL mode ["term_of", "test", "random_ind"]
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   831
      (generate_code_i thy' [] "Generated") [("testf", pred)];
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   832
    val s = "structure TestTerm =\nstruct\n\n" ^
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   833
      cat_lines (map snd code) ^
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   834
      "\nopen Generated;\n\n" ^ string_of
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   835
        (Pretty.block [str "val () = InductiveCodegen.test_fn :=",
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   836
          Pretty.brk 1, str "(fn p =>", Pretty.brk 1,
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   837
          str "case Seq.pull (testf p) of", Pretty.brk 1,
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   838
          str "SOME ", mk_tuple [mk_tuple (map (str o fst) args'), str "_"],
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   839
          str " =>", Pretty.brk 1, str "SOME ",
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   840
          Pretty.block (str "[" ::
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   841
            Pretty.commas (map (fn (s, T) => Pretty.block
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   842
              [mk_term_of gr "Generated" false T, Pretty.brk 1, str s]) args') @
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   843
            [str "]"]), Pretty.brk 1,
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   844
          str "| NONE => NONE);"]) ^
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   845
      "\n\nend;\n";
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   846
    val _ = ML_Context.eval_in (SOME ctxt) false Position.none s;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   847
    val values = Config.get ctxt random_values;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   848
    val bound = Config.get ctxt depth_bound;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   849
    val start = Config.get ctxt depth_start;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   850
    val offset = Config.get ctxt size_offset;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   851
    val test_fn' = !test_fn;
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35021
diff changeset
   852
    val dummy_report = ([], false)
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35021
diff changeset
   853
    fun test k = (deepen bound (fn i =>
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   854
      (priority ("Search depth: " ^ string_of_int i);
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35021
diff changeset
   855
       test_fn' (i, values, k+offset))) start, dummy_report);
33771
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   856
  in test end;
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   857
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   858
val quickcheck_setup =
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   859
  Attrib.register_config depth_bound_value #>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   860
  Attrib.register_config depth_start_value #>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   861
  Attrib.register_config random_values_value #>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   862
  Attrib.register_config size_offset_value #>
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   863
  Quickcheck.add_generator ("SML_inductive", test_term);
17926df64f0f Added new counterexample generator SML_inductive for goals involving
berghofe
parents: 33522
diff changeset
   864
11537
e007d35359c3 New code generators for HOL.
berghofe
parents:
diff changeset
   865
end;