TFL/tfl.ML
author haftmann
Mon, 18 Dec 2006 08:21:40 +0100
changeset 21883 341cefa2e4da
parent 20951 868120282837
child 22578 b0eb5652f210
permissions -rw-r--r--
dropped CodegenPackage.const_of_idf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      TFL/tfl.ML
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     4
    Copyright   1997  University of Cambridge
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     5
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     6
First part of main module.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     7
*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     8
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     9
signature PRIM =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    10
sig
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    11
  val trace: bool ref
14240
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
    12
  val trace_thms: string -> thm list -> unit
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
    13
  val trace_cterms: string -> cterm list -> unit
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    14
  type pattern
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    15
  val mk_functional: theory -> term list -> {functional: term, pats: pattern list}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    16
  val wfrec_definition0: theory -> string -> term -> term -> theory * thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    17
  val post_definition: thm list -> theory * (thm * pattern list) ->
20062
60de4603e645 tuned interface;
wenzelm
parents: 19927
diff changeset
    18
   {rules: thm,
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    19
    rows: int list,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    20
    TCs: term list list,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    21
    full_pats_TCs: (term * term list) list}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    22
  val wfrec_eqns: theory -> xstring -> thm list -> term list ->
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    23
   {WFR: term,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    24
    SV: term list,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    25
    proto_def: term,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    26
    extracta: (thm * term list) list,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    27
    pats: pattern list}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    28
  val lazyR_def: theory -> xstring -> thm list -> term list ->
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    29
   {theory: theory,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    30
    rules: thm,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    31
    R: term,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    32
    SV: term list,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    33
    full_pats_TCs: (term * term list) list,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    34
    patterns : pattern list}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    35
  val mk_induction: theory ->
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    36
    {fconst: term, R: term, SV: term list, pat_TCs_list: (term * term list) list} -> thm
11632
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
    37
  val postprocess: bool -> {wf_tac: tactic, terminator: tactic, simplifier: cterm -> thm}
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
    38
    -> theory -> {rules: thm, induction: thm, TCs: term list list}
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
    39
    -> {rules: thm, induction: thm, nested_tcs: thm list}
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    40
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    41
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    42
structure Prim: PRIM =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    43
struct
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    44
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    45
val trace = ref false;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    46
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    47
structure R = Rules;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    48
structure S = USyntax;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    49
structure U = Utils;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    50
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    51
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    52
fun TFL_ERR func mesg = U.ERR {module = "Tfl", func = func, mesg = mesg};
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    53
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    54
val concl = #2 o R.dest_thm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    55
val hyp = #1 o R.dest_thm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    56
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    57
val list_mk_type = U.end_itlist (curry (op -->));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    58
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    59
fun enumerate xs = ListPair.zip(xs, 0 upto (length xs - 1));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    60
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    61
fun front_last [] = raise TFL_ERR "front_last" "empty list"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    62
  | front_last [x] = ([],x)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    63
  | front_last (h::t) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    64
     let val (pref,x) = front_last t
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    65
     in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    66
        (h::pref,x)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    67
     end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    68
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    69
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    70
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    71
 * The next function is common to pattern-match translation and
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    72
 * proof of completeness of cases for the induction theorem.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    73
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    74
 * The curried function "gvvariant" returns a function to generate distinct
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    75
 * variables that are guaranteed not to be in names.  The names of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    76
 * the variables go u, v, ..., z, aa, ..., az, ...  The returned
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    77
 * function contains embedded refs!
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    78
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    79
fun gvvariant names =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    80
  let val slist = ref names
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    81
      val vname = ref "u"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    82
      fun new() =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    83
         if !vname mem_string (!slist)
12902
a23dc0b7566f Symbol.bump_string;
wenzelm
parents: 11632
diff changeset
    84
         then (vname := Symbol.bump_string (!vname);  new())
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    85
         else (slist := !vname :: !slist;  !vname)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    86
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    87
  fn ty => Free(new(), ty)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    88
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    89
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    90
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    91
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    92
 * Used in induction theorem production. This is the simple case of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    93
 * partitioning up pattern rows by the leading constructor.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    94
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    95
fun ipartition gv (constructors,rows) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    96
  let fun pfail s = raise TFL_ERR "partition.part" s
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    97
      fun part {constrs = [],   rows = [],   A} = rev A
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    98
        | part {constrs = [],   rows = _::_, A} = pfail"extra cases in defn"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    99
        | part {constrs = _::_, rows = [],   A} = pfail"cases missing in defn"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   100
        | part {constrs = c::crst, rows,     A} =
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   101
          let val (c, T) = dest_Const c
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   102
              val L = binder_types T
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   103
              val (in_group, not_in_group) =
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   104
               fold_rev (fn (row as (p::rst, rhs)) =>
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   105
                         fn (in_group,not_in_group) =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   106
                  let val (pc,args) = S.strip_comb p
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   107
                  in if (#1(dest_Const pc) = c)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   108
                     then ((args@rst, rhs)::in_group, not_in_group)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   109
                     else (in_group, row::not_in_group)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   110
                  end)      rows ([],[])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   111
              val col_types = U.take type_of (length L, #1(hd in_group))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   112
          in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   113
          part{constrs = crst, rows = not_in_group,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   114
               A = {constructor = c,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   115
                    new_formals = map gv col_types,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   116
                    group = in_group}::A}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   117
          end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   118
  in part{constrs = constructors, rows = rows, A = []}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   119
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   120
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   121
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   122
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   123
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   124
 * Each pattern carries with it a tag (i,b) where
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   125
 * i is the clause it came from and
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   126
 * b=true indicates that clause was given by the user
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   127
 * (or is an instantiation of a user supplied pattern)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   128
 * b=false --> i = ~1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   129
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   130
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   131
type pattern = term * (int * bool)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   132
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   133
fun pattern_map f (tm,x) = (f tm, x);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   134
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   135
fun pattern_subst theta = pattern_map (subst_free theta);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   136
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   137
val pat_of = fst;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   138
fun row_of_pat x = fst (snd x);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   139
fun given x = snd (snd x);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   140
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   141
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   142
 * Produce an instance of a constructor, plus genvars for its arguments.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   143
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   144
fun fresh_constr ty_match colty gv c =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   145
  let val (_,Ty) = dest_Const c
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   146
      val L = binder_types Ty
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   147
      and ty = body_type Ty
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   148
      val ty_theta = ty_match ty colty
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   149
      val c' = S.inst ty_theta c
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   150
      val gvars = map (S.inst ty_theta o gv) L
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   151
  in (c', gvars)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   152
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   153
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   154
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   155
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   156
 * Goes through a list of rows and picks out the ones beginning with a
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   157
 * pattern with constructor = name.
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   158
 *---------------------------------------------------------------------------*)
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   159
fun mk_group name rows =
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   160
  fold_rev (fn (row as ((prfx, p::rst), rhs)) =>
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   161
            fn (in_group,not_in_group) =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   162
               let val (pc,args) = S.strip_comb p
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   163
               in if ((#1 (Term.dest_Const pc) = name) handle TERM _ => false)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   164
                  then (((prfx,args@rst), rhs)::in_group, not_in_group)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   165
                  else (in_group, row::not_in_group) end)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   166
      rows ([],[]);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   167
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   168
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   169
 * Partition the rows. Not efficient: we should use hashing.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   170
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   171
fun partition _ _ (_,_,_,[]) = raise TFL_ERR "partition" "no rows"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   172
  | partition gv ty_match
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   173
              (constructors, colty, res_ty, rows as (((prfx,_),_)::_)) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   174
let val fresh = fresh_constr ty_match colty gv
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   175
     fun part {constrs = [],      rows, A} = rev A
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   176
       | part {constrs = c::crst, rows, A} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   177
         let val (c',gvars) = fresh c
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   178
             val (in_group, not_in_group) = mk_group (#1 (dest_Const c')) rows
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   179
             val in_group' =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   180
                 if (null in_group)  (* Constructor not given *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   181
                 then [((prfx, #2(fresh c)), (S.ARB res_ty, (~1,false)))]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   182
                 else in_group
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   183
         in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   184
         part{constrs = crst,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   185
              rows = not_in_group,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   186
              A = {constructor = c',
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   187
                   new_formals = gvars,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   188
                   group = in_group'}::A}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   189
         end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   190
in part{constrs=constructors, rows=rows, A=[]}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   191
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   192
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   193
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   194
 * Misc. routines used in mk_case
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   195
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   196
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   197
fun mk_pat (c,l) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   198
  let val L = length (binder_types (type_of c))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   199
      fun build (prfx,tag,plist) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   200
          let val args   = Library.take (L,plist)
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   201
              and plist' = Library.drop(L,plist)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   202
          in (prfx,tag,list_comb(c,args)::plist') end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   203
  in map build l end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   204
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   205
fun v_to_prfx (prfx, v::pats) = (v::prfx,pats)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   206
  | v_to_prfx _ = raise TFL_ERR "mk_case" "v_to_prfx";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   207
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   208
fun v_to_pats (v::prfx,tag, pats) = (prfx, tag, v::pats)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   209
  | v_to_pats _ = raise TFL_ERR "mk_case" "v_to_pats";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   210
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   211
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   212
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   213
 * Translation of pattern terms into nested case expressions.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   214
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   215
 * This performs the translation and also builds the full set of patterns.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   216
 * Thus it supports the construction of induction theorems even when an
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   217
 * incomplete set of patterns is given.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   218
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   219
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   220
fun mk_case ty_info ty_match usednames range_ty =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   221
 let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   222
 fun mk_case_fail s = raise TFL_ERR "mk_case" s
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   223
 val fresh_var = gvvariant usednames
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   224
 val divide = partition fresh_var ty_match
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   225
 fun expand constructors ty ((_,[]), _) = mk_case_fail"expand_var_row"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   226
   | expand constructors ty (row as ((prfx, p::rst), rhs)) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   227
       if (is_Free p)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   228
       then let val fresh = fresh_constr ty_match ty fresh_var
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   229
                fun expnd (c,gvs) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   230
                  let val capp = list_comb(c,gvs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   231
                  in ((prfx, capp::rst), pattern_subst[(p,capp)] rhs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   232
                  end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   233
            in map expnd (map fresh constructors)  end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   234
       else [row]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   235
 fun mk{rows=[],...} = mk_case_fail"no rows"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   236
   | mk{path=[], rows = ((prfx, []), (tm,tag))::_} =  (* Done *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   237
        ([(prfx,tag,[])], tm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   238
   | mk{path=[], rows = _::_} = mk_case_fail"blunder"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   239
   | mk{path as u::rstp, rows as ((prfx, []), rhs)::rst} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   240
        mk{path = path,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   241
           rows = ((prfx, [fresh_var(type_of u)]), rhs)::rst}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   242
   | mk{path = u::rstp, rows as ((_, p::_), _)::_} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   243
     let val (pat_rectangle,rights) = ListPair.unzip rows
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   244
         val col0 = map(hd o #2) pat_rectangle
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   245
     in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   246
     if (forall is_Free col0)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   247
     then let val rights' = map (fn(v,e) => pattern_subst[(v,u)] e)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   248
                                (ListPair.zip (col0, rights))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   249
              val pat_rectangle' = map v_to_prfx pat_rectangle
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   250
              val (pref_patl,tm) = mk{path = rstp,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   251
                                      rows = ListPair.zip (pat_rectangle',
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   252
                                                           rights')}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   253
          in (map v_to_pats pref_patl, tm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   254
          end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   255
     else
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   256
     let val pty as Type (ty_name,_) = type_of p
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   257
     in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   258
     case (ty_info ty_name)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14820
diff changeset
   259
     of NONE => mk_case_fail("Not a known datatype: "^ty_name)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14820
diff changeset
   260
      | SOME{case_const,constructors} =>
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   261
        let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   262
            val case_const_name = #1(dest_Const case_const)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   263
            val nrows = List.concat (map (expand constructors pty) rows)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   264
            val subproblems = divide(constructors, pty, range_ty, nrows)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   265
            val groups      = map #group subproblems
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   266
            and new_formals = map #new_formals subproblems
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   267
            and constructors' = map #constructor subproblems
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   268
            val news = map (fn (nf,rows) => {path = nf@rstp, rows=rows})
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   269
                           (ListPair.zip (new_formals, groups))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   270
            val rec_calls = map mk news
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   271
            val (pat_rect,dtrees) = ListPair.unzip rec_calls
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   272
            val case_functions = map S.list_mk_abs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   273
                                  (ListPair.zip (new_formals, dtrees))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   274
            val types = map type_of (case_functions@[u]) @ [range_ty]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   275
            val case_const' = Const(case_const_name, list_mk_type types)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   276
            val tree = list_comb(case_const', case_functions@[u])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   277
            val pat_rect1 = List.concat
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   278
                              (ListPair.map mk_pat (constructors', pat_rect))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   279
        in (pat_rect1,tree)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   280
        end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   281
     end end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   282
 in mk
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   283
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   284
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   285
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   286
(* Repeated variable occurrences in a pattern are not allowed. *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   287
fun FV_multiset tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   288
   case (S.dest_term tm)
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   289
     of S.VAR{Name = c, Ty = T} => [Free(c, T)]
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   290
      | S.CONST _ => []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   291
      | S.COMB{Rator, Rand} => FV_multiset Rator @ FV_multiset Rand
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   292
      | S.LAMB _ => raise TFL_ERR "FV_multiset" "lambda";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   293
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   294
fun no_repeat_vars thy pat =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   295
 let fun check [] = true
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   296
       | check (v::rst) =
20088
bffda4cd0f79 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20062
diff changeset
   297
         if member (op aconv) rst v then
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   298
            raise TFL_ERR "no_repeat_vars"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   299
                          (quote (#1 (dest_Free v)) ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   300
                          " occurs repeatedly in the pattern " ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   301
                          quote (string_of_cterm (Thry.typecheck thy pat)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   302
         else check rst
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   303
 in check (FV_multiset pat)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   304
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   305
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   306
fun dest_atom (Free p) = p
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   307
  | dest_atom (Const p) = p
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   308
  | dest_atom  _ = raise TFL_ERR "dest_atom" "function name not an identifier";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   309
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   310
fun same_name (p,q) = #1(dest_atom p) = #1(dest_atom q);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   311
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   312
local fun mk_functional_err s = raise TFL_ERR "mk_functional" s
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   313
      fun single [_$_] =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   314
              mk_functional_err "recdef does not allow currying"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   315
        | single [f] = f
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   316
        | single fs  =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   317
              (*multiple function names?*)
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18972
diff changeset
   318
              if length (distinct same_name fs) < length fs
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   319
              then mk_functional_err
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   320
                   "The function being declared appears with multiple types"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   321
              else mk_functional_err
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   322
                   (Int.toString (length fs) ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   323
                    " distinct function names being declared")
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   324
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   325
fun mk_functional thy clauses =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   326
 let val (L,R) = ListPair.unzip (map HOLogic.dest_eq clauses
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   327
                   handle TERM _ => raise TFL_ERR "mk_functional"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   328
                        "recursion equations must use the = relation")
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   329
     val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L)
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18972
diff changeset
   330
     val atom = single (distinct (op aconv) funcs)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   331
     val (fname,ftype) = dest_atom atom
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   332
     val dummy = map (no_repeat_vars thy) pats
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   333
     val rows = ListPair.zip (map (fn x => ([]:term list,[x])) pats,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   334
                              map (fn (t,i) => (t,(i,true))) (enumerate R))
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   335
     val names = foldr add_term_names [] R
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   336
     val atype = type_of(hd pats)
20088
bffda4cd0f79 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20062
diff changeset
   337
     and aname = Name.variant names "a"
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   338
     val a = Free(aname,atype)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   339
     val ty_info = Thry.match_info thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   340
     val ty_match = Thry.match_type thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   341
     val range_ty = type_of (hd R)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   342
     val (patts, case_tm) = mk_case ty_info ty_match (aname::names) range_ty
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   343
                                    {path=[a], rows=rows}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   344
     val patts1 = map (fn (_,tag,[pat]) => (pat,tag)) patts
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   345
          handle Match => mk_functional_err "error in pattern-match translation"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   346
     val patts2 = Library.sort (Library.int_ord o Library.pairself row_of_pat) patts1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   347
     val finals = map row_of_pat patts2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   348
     val originals = map (row_of_pat o #2) rows
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   349
     val dummy = case (originals\\finals)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   350
             of [] => ()
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   351
          | L => mk_functional_err
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   352
 ("The following clauses are redundant (covered by preceding clauses): " ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   353
                   commas (map (fn i => Int.toString (i + 1)) L))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   354
 in {functional = Abs(Sign.base_name fname, ftype,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   355
                      abstract_over (atom,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   356
                                     absfree(aname,atype, case_tm))),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   357
     pats = patts2}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   358
end end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   359
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   360
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   361
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   362
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   363
 *                    PRINCIPLES OF DEFINITION
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   364
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   365
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   366
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   367
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   368
(*For Isabelle, the lhs of a definition must be a constant.*)
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   369
fun mk_const_def sign (c, Ty, rhs) =
20155
da0505518e69 Sign.infer_types: Name.context;
wenzelm
parents: 20088
diff changeset
   370
    Sign.infer_types (Sign.pp sign) sign (Sign.consts_of sign) (K NONE) (K NONE) Name.context false
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   371
               ([Const("==",dummyT) $ Const(c,Ty) $ rhs], propT)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   372
    |> #1;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   373
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   374
(*Make all TVars available for instantiation by adding a ? to the front*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   375
fun poly_tvars (Type(a,Ts)) = Type(a, map (poly_tvars) Ts)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   376
  | poly_tvars (TFree (a,sort)) = TVar (("?" ^ a, 0), sort)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   377
  | poly_tvars (TVar ((a,i),sort)) = TVar (("?" ^ a, i+1), sort);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   378
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   379
local val f_eq_wfrec_R_M =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   380
           #ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY))))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   381
      val {lhs=f, rhs} = S.dest_eq f_eq_wfrec_R_M
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   382
      val (fname,_) = dest_Free f
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   383
      val (wfrec,_) = S.strip_comb rhs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   384
in
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   385
fun wfrec_definition0 thy fid R (functional as Abs(x, Ty, _)) =
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   386
 let val def_name = if x<>fid then
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   387
                        raise TFL_ERR "wfrec_definition0"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   388
                                      ("Expected a definition of " ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   389
                                             quote fid ^ " but found one of " ^
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   390
                                      quote x)
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   391
                    else x ^ "_def"
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20155
diff changeset
   392
     val wfrec_R_M =  map_types poly_tvars
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20155
diff changeset
   393
                          (wfrec $ map_types poly_tvars R)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   394
                      $ functional
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   395
     val def_term = mk_const_def (Theory.sign_of thy) (x, Ty, wfrec_R_M)
18358
0a733e11021a re-oriented some result tuples in PureThy
haftmann
parents: 18176
diff changeset
   396
     val ([def], thy') = PureThy.add_defs_i false [Thm.no_attributes (def_name, def_term)] thy
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   397
 in (thy', def) end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   398
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   399
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   400
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   401
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   402
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   403
 * This structure keeps track of congruence rules that aren't derived
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   404
 * from a datatype definition.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   405
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   406
fun extraction_thms thy =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   407
 let val {case_rewrites,case_congs} = Thry.extract_info thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   408
 in (case_rewrites, case_congs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   409
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   410
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   411
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   412
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   413
 * Pair patterns with termination conditions. The full list of patterns for
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   414
 * a definition is merged with the TCs arising from the user-given clauses.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   415
 * There can be fewer clauses than the full list, if the user omitted some
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   416
 * cases. This routine is used to prepare input for mk_induction.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   417
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   418
fun merge full_pats TCs =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   419
let fun insert (p,TCs) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   420
      let fun insrt ((x as (h,[]))::rst) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   421
                 if (p aconv h) then (p,TCs)::rst else x::insrt rst
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   422
            | insrt (x::rst) = x::insrt rst
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   423
            | insrt[] = raise TFL_ERR "merge.insert" "pattern not found"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   424
      in insrt end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   425
    fun pass ([],ptcl_final) = ptcl_final
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   426
      | pass (ptcs::tcl, ptcl) = pass(tcl, insert ptcs ptcl)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   427
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   428
  pass (TCs, map (fn p => (p,[])) full_pats)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   429
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   430
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   431
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   432
fun givens pats = map pat_of (List.filter given pats);
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   433
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   434
fun post_definition meta_tflCongs (theory, (def, pats)) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   435
 let val tych = Thry.typecheck theory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   436
     val f = #lhs(S.dest_eq(concl def))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   437
     val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   438
     val pats' = List.filter given pats
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   439
     val given_pats = map pat_of pats'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   440
     val rows = map row_of_pat pats'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   441
     val WFR = #ant(S.dest_imp(concl corollary))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   442
     val R = #Rand(S.dest_comb WFR)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   443
     val corollary' = R.UNDISCH corollary  (* put WF R on assums *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   444
     val corollaries = map (fn pat => R.SPEC (tych pat) corollary')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   445
                           given_pats
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   446
     val (case_rewrites,context_congs) = extraction_thms theory
14219
9fdea25f9ebb added a comment
paulson
parents: 14217
diff changeset
   447
     (*case_ss causes minimal simplification: bodies of case expressions are
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   448
       not simplified. Otherwise large examples (Red-Black trees) are too
14219
9fdea25f9ebb added a comment
paulson
parents: 14217
diff changeset
   449
       slow.*)
20062
60de4603e645 tuned interface;
wenzelm
parents: 19927
diff changeset
   450
     val case_ss = Simplifier.theory_context theory
60de4603e645 tuned interface;
wenzelm
parents: 19927
diff changeset
   451
       (HOL_basic_ss addcongs
60de4603e645 tuned interface;
wenzelm
parents: 19927
diff changeset
   452
         (map (#weak_case_cong o snd) o Symtab.dest o DatatypePackage.get_datatypes) theory addsimps case_rewrites)
14217
9f5679e97eac Fixed inefficiency in post_definition by adding weak case congruence
berghofe
parents: 12902
diff changeset
   453
     val corollaries' = map (Simplifier.simplify case_ss) corollaries
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   454
     val extract = R.CONTEXT_REWRITE_RULE
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   455
                     (f, [R], cut_apply, meta_tflCongs@context_congs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   456
     val (rules, TCs) = ListPair.unzip (map extract corollaries')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   457
     val rules0 = map (rewrite_rule [Thms.CUT_DEF]) rules
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   458
     val mk_cond_rule = R.FILTER_DISCH_ALL(not o curry (op aconv) WFR)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   459
     val rules1 = R.LIST_CONJ(map mk_cond_rule rules0)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   460
 in
20062
60de4603e645 tuned interface;
wenzelm
parents: 19927
diff changeset
   461
 {rules = rules1,
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   462
  rows = rows,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   463
  full_pats_TCs = merge (map pat_of pats) (ListPair.zip (given_pats, TCs)),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   464
  TCs = TCs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   465
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   466
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   467
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   468
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   469
 * Perform the extraction without making the definition. Definition and
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   470
 * extraction commute for the non-nested case.  (Deferred recdefs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   471
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   472
 * The purpose of wfrec_eqns is merely to instantiate the recursion theorem
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   473
 * and extract termination conditions: no definition is made.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   474
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   475
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   476
fun wfrec_eqns thy fid tflCongs eqns =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   477
 let val {lhs,rhs} = S.dest_eq (hd eqns)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   478
     val (f,args) = S.strip_comb lhs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   479
     val (fname,fty) = dest_atom f
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   480
     val (SV,a) = front_last args    (* SV = schematic variables *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   481
     val g = list_comb(f,SV)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   482
     val h = Free(fname,type_of g)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   483
     val eqns1 = map (subst_free[(g,h)]) eqns
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   484
     val {functional as Abs(x, Ty, _),  pats} = mk_functional thy eqns1
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   485
     val given_pats = givens pats
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   486
     (* val f = Free(x,Ty) *)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   487
     val Type("fun", [f_dty, f_rty]) = Ty
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   488
     val dummy = if x<>fid then
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   489
                        raise TFL_ERR "wfrec_eqns"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   490
                                      ("Expected a definition of " ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   491
                                      quote fid ^ " but found one of " ^
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   492
                                      quote x)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   493
                 else ()
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   494
     val (case_rewrites,context_congs) = extraction_thms thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   495
     val tych = Thry.typecheck thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   496
     val WFREC_THM0 = R.ISPEC (tych functional) Thms.WFREC_COROLLARY
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   497
     val Const("All",_) $ Abs(Rname,Rtype,_) = concl WFREC_THM0
20088
bffda4cd0f79 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20062
diff changeset
   498
     val R = Free (Name.variant (foldr add_term_names [] eqns) Rname,
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   499
                   Rtype)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   500
     val WFREC_THM = R.ISPECL [tych R, tych g] WFREC_THM0
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   501
     val ([proto_def, WFR],_) = S.strip_imp(concl WFREC_THM)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   502
     val dummy =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   503
           if !trace then
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   504
               writeln ("ORIGINAL PROTO_DEF: " ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   505
                          Sign.string_of_term (Theory.sign_of thy) proto_def)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   506
           else ()
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   507
     val R1 = S.rand WFR
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   508
     val corollary' = R.UNDISCH(R.UNDISCH WFREC_THM)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   509
     val corollaries = map (fn pat => R.SPEC (tych pat) corollary') given_pats
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   510
     val corollaries' = map (rewrite_rule case_rewrites) corollaries
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   511
     fun extract X = R.CONTEXT_REWRITE_RULE
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   512
                       (f, R1::SV, cut_apply, tflCongs@context_congs) X
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   513
 in {proto_def = proto_def,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   514
     SV=SV,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   515
     WFR=WFR,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   516
     pats=pats,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   517
     extracta = map extract corollaries'}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   518
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   519
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   520
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   521
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   522
 * Define the constant after extracting the termination conditions. The
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   523
 * wellfounded relation used in the definition is computed by using the
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   524
 * choice operator on the extracted conditions (plus the condition that
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   525
 * such a relation must be wellfounded).
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   526
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   527
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   528
fun lazyR_def thy fid tflCongs eqns =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   529
 let val {proto_def,WFR,pats,extracta,SV} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   530
           wfrec_eqns thy fid tflCongs eqns
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   531
     val R1 = S.rand WFR
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   532
     val f = #lhs(S.dest_eq proto_def)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   533
     val (extractants,TCl) = ListPair.unzip extracta
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   534
     val dummy = if !trace
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   535
                 then (writeln "Extractants = ";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   536
                       prths extractants;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   537
                       ())
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   538
                 else ()
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   539
     val TCs = foldr (gen_union (op aconv)) [] TCl
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   540
     val full_rqt = WFR::TCs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   541
     val R' = S.mk_select{Bvar=R1, Body=S.list_mk_conj full_rqt}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   542
     val R'abs = S.rand R'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   543
     val proto_def' = subst_free[(R1,R')] proto_def
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   544
     val dummy = if !trace then writeln ("proto_def' = " ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   545
                                         Sign.string_of_term
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   546
                                         (Theory.sign_of thy) proto_def')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   547
                           else ()
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   548
     val {lhs,rhs} = S.dest_eq proto_def'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   549
     val (c,args) = S.strip_comb lhs
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   550
     val (name,Ty) = dest_atom c
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   551
     val defn = mk_const_def (Theory.sign_of thy)
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   552
                 (name, Ty, S.list_mk_abs (args,rhs))
18358
0a733e11021a re-oriented some result tuples in PureThy
haftmann
parents: 18176
diff changeset
   553
     val ([def0], theory) =
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   554
       thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   555
       |> PureThy.add_defs_i false
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   556
            [Thm.no_attributes (fid ^ "_def", defn)]
19927
9286e99b2808 refrain from reforming TFL -- back to previous revision;
wenzelm
parents: 19925
diff changeset
   557
     val def = Thm.freezeT def0;
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   558
     val dummy = if !trace then writeln ("DEF = " ^ string_of_thm def)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   559
                           else ()
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   560
     (* val fconst = #lhs(S.dest_eq(concl def))  *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   561
     val tych = Thry.typecheck theory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   562
     val full_rqt_prop = map (Dcterm.mk_prop o tych) full_rqt
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   563
         (*lcp: a lot of object-logic inference to remove*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   564
     val baz = R.DISCH_ALL
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   565
                 (fold_rev R.DISCH full_rqt_prop
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   566
                  (R.LIST_CONJ extractants))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   567
     val dum = if !trace then writeln ("baz = " ^ string_of_thm baz)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   568
                           else ()
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   569
     val f_free = Free (fid, fastype_of f)  (*'cos f is a Const*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   570
     val SV' = map tych SV;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   571
     val SVrefls = map reflexive SV'
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   572
     val def0 = (fold (fn x => fn th => R.rbeta(combination th x))
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   573
                   SVrefls def)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   574
                RS meta_eq_to_obj_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   575
     val def' = R.MP (R.SPEC (tych R') (R.GEN (tych R1) baz)) def0
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   576
     val body_th = R.LIST_CONJ (map R.ASSUME full_rqt_prop)
11455
e07927b980ec defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents: 10769
diff changeset
   577
     val SELECT_AX = (*in this way we hope to avoid a STATIC dependence upon
e07927b980ec defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents: 10769
diff changeset
   578
                       theory Hilbert_Choice*)
16505
c4b2e3cd84ab avoid identifier 'Name';
wenzelm
parents: 15574
diff changeset
   579
         thm "Hilbert_Choice.tfl_some"
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18358
diff changeset
   580
         handle ERROR msg => cat_error msg
11455
e07927b980ec defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents: 10769
diff changeset
   581
    "defer_recdef requires theory Main or at least Hilbert_Choice as parent"
e07927b980ec defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents: 10769
diff changeset
   582
     val bar = R.MP (R.ISPECL[tych R'abs, tych R1] SELECT_AX) body_th
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   583
 in {theory = theory, R=R1, SV=SV,
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   584
     rules = fold (U.C R.MP) (R.CONJUNCTS bar) def',
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   585
     full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   586
     patterns = pats}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   587
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   588
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   589
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   590
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   591
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   592
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   593
 *                           INDUCTION THEOREM
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   594
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   595
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   596
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   597
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   598
(*------------------------  Miscellaneous function  --------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   599
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   600
 *           [x_1,...,x_n]     ?v_1...v_n. M[v_1,...,v_n]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   601
 *     -----------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   602
 *     ( M[x_1,...,x_n], [(x_i,?v_1...v_n. M[v_1,...,v_n]),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   603
 *                        ...
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   604
 *                        (x_j,?v_n. M[x_1,...,x_(n-1),v_n])] )
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   605
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   606
 * This function is totally ad hoc. Used in the production of the induction
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   607
 * theorem. The nchotomy theorem can have clauses that look like
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   608
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   609
 *     ?v1..vn. z = C vn..v1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   610
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   611
 * in which the order of quantification is not the order of occurrence of the
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   612
 * quantified variables as arguments to C. Since we have no control over this
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   613
 * aspect of the nchotomy theorem, we make the correspondence explicit by
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   614
 * pairing the incoming new variable with the term it gets beta-reduced into.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   615
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   616
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   617
fun alpha_ex_unroll (xlist, tm) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   618
  let val (qvars,body) = S.strip_exists tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   619
      val vlist = #2(S.strip_comb (S.rhs body))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   620
      val plist = ListPair.zip (vlist, xlist)
17314
04e21a27c0ad introduces some modern-style AList operations
haftmann
parents: 16853
diff changeset
   621
      val args = map (the o AList.lookup (op aconv) plist) qvars
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14820
diff changeset
   622
                   handle Option => sys_error
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   623
                       "TFL fault [alpha_ex_unroll]: no correspondence"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   624
      fun build ex      []   = []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   625
        | build (_$rex) (v::rst) =
18176
ae9bd644d106 Term.betapply;
wenzelm
parents: 17465
diff changeset
   626
           let val ex1 = Term.betapply(rex, v)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   627
           in  ex1 :: build ex1 rst
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   628
           end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   629
     val (nex::exl) = rev (tm::build tm args)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   630
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   631
  (nex, ListPair.zip (args, rev exl))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   632
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   633
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   634
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   635
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   636
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   637
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   638
 *             PROVING COMPLETENESS OF PATTERNS
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   639
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   640
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   641
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   642
fun mk_case ty_info usednames thy =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   643
 let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   644
 val divide = ipartition (gvvariant usednames)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   645
 val tych = Thry.typecheck thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   646
 fun tych_binding(x,y) = (tych x, tych y)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   647
 fun fail s = raise TFL_ERR "mk_case" s
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   648
 fun mk{rows=[],...} = fail"no rows"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   649
   | mk{path=[], rows = [([], (thm, bindings))]} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   650
                         R.IT_EXISTS (map tych_binding bindings) thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   651
   | mk{path = u::rstp, rows as (p::_, _)::_} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   652
     let val (pat_rectangle,rights) = ListPair.unzip rows
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   653
         val col0 = map hd pat_rectangle
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   654
         val pat_rectangle' = map tl pat_rectangle
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   655
     in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   656
     if (forall is_Free col0) (* column 0 is all variables *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   657
     then let val rights' = map (fn ((thm,theta),v) => (thm,theta@[(u,v)]))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   658
                                (ListPair.zip (rights, col0))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   659
          in mk{path = rstp, rows = ListPair.zip (pat_rectangle', rights')}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   660
          end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   661
     else                     (* column 0 is all constructors *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   662
     let val Type (ty_name,_) = type_of p
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   663
     in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   664
     case (ty_info ty_name)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14820
diff changeset
   665
     of NONE => fail("Not a known datatype: "^ty_name)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14820
diff changeset
   666
      | SOME{constructors,nchotomy} =>
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   667
        let val thm' = R.ISPEC (tych u) nchotomy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   668
            val disjuncts = S.strip_disj (concl thm')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   669
            val subproblems = divide(constructors, rows)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   670
            val groups      = map #group subproblems
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   671
            and new_formals = map #new_formals subproblems
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   672
            val existentials = ListPair.map alpha_ex_unroll
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   673
                                   (new_formals, disjuncts)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   674
            val constraints = map #1 existentials
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   675
            val vexl = map #2 existentials
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   676
            fun expnd tm (pats,(th,b)) = (pats,(R.SUBS[R.ASSUME(tych tm)]th,b))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   677
            val news = map (fn (nf,rows,c) => {path = nf@rstp,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   678
                                               rows = map (expnd c) rows})
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   679
                           (U.zip3 new_formals groups constraints)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   680
            val recursive_thms = map mk news
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   681
            val build_exists = Library.foldr
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   682
                                (fn((x,t), th) =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   683
                                 R.CHOOSE (tych x, R.ASSUME (tych t)) th)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   684
            val thms' = ListPair.map build_exists (vexl, recursive_thms)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   685
            val same_concls = R.EVEN_ORS thms'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   686
        in R.DISJ_CASESL thm' same_concls
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   687
        end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   688
     end end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   689
 in mk
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   690
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   691
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   692
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   693
fun complete_cases thy =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   694
 let val tych = Thry.typecheck thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   695
     val ty_info = Thry.induct_info thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   696
 in fn pats =>
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   697
 let val names = foldr add_term_names [] pats
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   698
     val T = type_of (hd pats)
20088
bffda4cd0f79 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20062
diff changeset
   699
     val aname = Name.variant names "a"
bffda4cd0f79 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20062
diff changeset
   700
     val vname = Name.variant (aname::names) "v"
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   701
     val a = Free (aname, T)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   702
     val v = Free (vname, T)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   703
     val a_eq_v = HOLogic.mk_eq(a,v)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   704
     val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   705
                           (R.REFL (tych a))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   706
     val th0 = R.ASSUME (tych a_eq_v)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   707
     val rows = map (fn x => ([x], (th0,[]))) pats
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   708
 in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   709
 R.GEN (tych a)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   710
       (R.RIGHT_ASSOC
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   711
          (R.CHOOSE(tych v, ex_th0)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   712
                (mk_case ty_info (vname::aname::names)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   713
                 thy {path=[v], rows=rows})))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   714
 end end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   715
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   716
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   717
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   718
 * Constructing induction hypotheses: one for each recursive call.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   719
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   720
 * Note. R will never occur as a variable in the ind_clause, because
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   721
 * to do so, it would have to be from a nested definition, and we don't
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   722
 * allow nested defns to have R variable.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   723
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   724
 * Note. When the context is empty, there can be no local variables.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   725
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   726
(*
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   727
local infix 5 ==>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   728
      fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   729
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   730
fun build_ih f P (pat,TCs) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   731
 let val globals = S.free_vars_lr pat
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   732
     fun nested tm = isSome (S.find_term (curry (op aconv) f) tm)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   733
     fun dest_TC tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   734
         let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   735
             val (R,y,_) = S.dest_relation R_y_pat
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   736
             val P_y = if (nested tm) then R_y_pat ==> P$y else P$y
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   737
         in case cntxt
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   738
              of [] => (P_y, (tm,[]))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   739
               | _  => let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   740
                    val imp = S.list_mk_conj cntxt ==> P_y
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   741
                    val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   742
                    val locals = #2(U.pluck (curry (op aconv) P) lvs) handle U.ERR _ => lvs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   743
                    in (S.list_mk_forall(locals,imp), (tm,locals)) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   744
         end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   745
 in case TCs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   746
    of [] => (S.list_mk_forall(globals, P$pat), [])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   747
     |  _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   748
                 val ind_clause = S.list_mk_conj ihs ==> P$pat
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   749
             in (S.list_mk_forall(globals,ind_clause), TCs_locals)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   750
             end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   751
 end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   752
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   753
*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   754
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   755
local infix 5 ==>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   756
      fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   757
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   758
fun build_ih f (P,SV) (pat,TCs) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   759
 let val pat_vars = S.free_vars_lr pat
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   760
     val globals = pat_vars@SV
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   761
     fun nested tm = isSome (S.find_term (curry (op aconv) f) tm)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   762
     fun dest_TC tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   763
         let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   764
             val (R,y,_) = S.dest_relation R_y_pat
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   765
             val P_y = if (nested tm) then R_y_pat ==> P$y else P$y
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   766
         in case cntxt
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   767
              of [] => (P_y, (tm,[]))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   768
               | _  => let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   769
                    val imp = S.list_mk_conj cntxt ==> P_y
20951
868120282837 gen_rem(s) abandoned in favour of remove / subtract
haftmann
parents: 20548
diff changeset
   770
                    val lvs = subtract (op aconv) globals (S.free_vars_lr imp)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   771
                    val locals = #2(U.pluck (curry (op aconv) P) lvs) handle U.ERR _ => lvs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   772
                    in (S.list_mk_forall(locals,imp), (tm,locals)) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   773
         end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   774
 in case TCs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   775
    of [] => (S.list_mk_forall(pat_vars, P$pat), [])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   776
     |  _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   777
                 val ind_clause = S.list_mk_conj ihs ==> P$pat
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   778
             in (S.list_mk_forall(pat_vars,ind_clause), TCs_locals)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   779
             end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   780
 end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   781
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   782
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   783
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   784
 * This function makes good on the promise made in "build_ih".
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   785
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   786
 * Input  is tm = "(!y. R y pat ==> P y) ==> P pat",
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   787
 *           TCs = TC_1[pat] ... TC_n[pat]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   788
 *           thm = ih1 /\ ... /\ ih_n |- ih[pat]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   789
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   790
fun prove_case f thy (tm,TCs_locals,thm) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   791
 let val tych = Thry.typecheck thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   792
     val antc = tych(#ant(S.dest_imp tm))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   793
     val thm' = R.SPEC_ALL thm
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   794
     fun nested tm = isSome (S.find_term (curry (op aconv) f) tm)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   795
     fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC)))))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   796
     fun mk_ih ((TC,locals),th2,nested) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   797
         R.GENL (map tych locals)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   798
            (if nested then R.DISCH (get_cntxt TC) th2 handle U.ERR _ => th2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   799
             else if S.is_imp (concl TC) then R.IMP_TRANS TC th2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   800
             else R.MP th2 TC)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   801
 in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   802
 R.DISCH antc
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   803
 (if S.is_imp(concl thm') (* recursive calls in this clause *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   804
  then let val th1 = R.ASSUME antc
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   805
           val TCs = map #1 TCs_locals
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   806
           val ylist = map (#2 o S.dest_relation o #2 o S.strip_imp o
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   807
                            #2 o S.strip_forall) TCs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   808
           val TClist = map (fn(TC,lvs) => (R.SPEC_ALL(R.ASSUME(tych TC)),lvs))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   809
                            TCs_locals
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   810
           val th2list = map (U.C R.SPEC th1 o tych) ylist
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   811
           val nlist = map nested TCs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   812
           val triples = U.zip3 TClist th2list nlist
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   813
           val Pylist = map mk_ih triples
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   814
       in R.MP thm' (R.LIST_CONJ Pylist) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   815
  else thm')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   816
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   817
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   818
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   819
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   820
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   821
 *         x = (v1,...,vn)  |- M[x]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   822
 *    ---------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   823
 *      ?v1 ... vn. x = (v1,...,vn) |- M[x]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   824
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   825
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   826
fun LEFT_ABS_VSTRUCT tych thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   827
  let fun CHOOSER v (tm,thm) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   828
        let val ex_tm = S.mk_exists{Bvar=v,Body=tm}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   829
        in (ex_tm, R.CHOOSE(tych v, R.ASSUME (tych ex_tm)) thm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   830
        end
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   831
      val [veq] = List.filter (can S.dest_eq) (#1 (R.dest_thm thm))
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   832
      val {lhs,rhs} = S.dest_eq veq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   833
      val L = S.free_vars_lr rhs
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   834
  in  #2 (fold_rev CHOOSER L (veq,thm))  end;
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   835
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   836
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   837
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   838
 * Input : f, R,  and  [(pat1,TCs1),..., (patn,TCsn)]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   839
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   840
 * Instantiates WF_INDUCTION_THM, getting Sinduct and then tries to prove
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   841
 * recursion induction (Rinduct) by proving the antecedent of Sinduct from
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   842
 * the antecedent of Rinduct.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   843
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   844
fun mk_induction thy {fconst, R, SV, pat_TCs_list} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   845
let val tych = Thry.typecheck thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   846
    val Sinduction = R.UNDISCH (R.ISPEC (tych R) Thms.WF_INDUCTION_THM)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   847
    val (pats,TCsl) = ListPair.unzip pat_TCs_list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   848
    val case_thm = complete_cases thy pats
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   849
    val domain = (type_of o hd) pats
20088
bffda4cd0f79 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20062
diff changeset
   850
    val Pname = Name.variant (foldr (Library.foldr add_term_names)
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   851
                              [] (pats::TCsl)) "P"
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   852
    val P = Free(Pname, domain --> HOLogic.boolT)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   853
    val Sinduct = R.SPEC (tych P) Sinduction
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   854
    val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   855
    val Rassums_TCl' = map (build_ih fconst (P,SV)) pat_TCs_list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   856
    val (Rassums,TCl') = ListPair.unzip Rassums_TCl'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   857
    val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums))
18176
ae9bd644d106 Term.betapply;
wenzelm
parents: 17465
diff changeset
   858
    val cases = map (fn pat => Term.betapply (Sinduct_assumf, pat)) pats
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   859
    val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   860
    val proved_cases = map (prove_case fconst thy) tasks
20088
bffda4cd0f79 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20062
diff changeset
   861
    val v = Free (Name.variant (foldr add_term_names [] (map concl proved_cases))
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   862
                    "v",
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   863
                  domain)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   864
    val vtyped = tych v
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   865
    val substs = map (R.SYM o R.ASSUME o tych o (curry HOLogic.mk_eq v)) pats
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   866
    val proved_cases1 = ListPair.map (fn (th,th') => R.SUBS[th]th')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   867
                          (substs, proved_cases)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   868
    val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   869
    val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   870
    val dc = R.MP Sinduct dant
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   871
    val Parg_ty = type_of(#Bvar(S.dest_forall(concl dc)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   872
    val vars = map (gvvariant[Pname]) (S.strip_prod_type Parg_ty)
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   873
    val dc' = fold_rev (R.GEN o tych) vars
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   874
                       (R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   875
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   876
   R.GEN (tych P) (R.DISCH (tych(concl Rinduct_assum)) dc')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   877
end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   878
handle U.ERR _ => raise TFL_ERR "mk_induction" "failed derivation";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   879
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   880
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   881
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   882
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   883
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   884
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   885
 *                        POST PROCESSING
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   886
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   887
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   888
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   889
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   890
fun simplify_induction thy hth ind =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   891
  let val tych = Thry.typecheck thy
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   892
      val (asl,_) = R.dest_thm ind
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   893
      val (_,tc_eq_tc') = R.dest_thm hth
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   894
      val tc = S.lhs tc_eq_tc'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   895
      fun loop [] = ind
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   896
        | loop (asm::rst) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   897
          if (can (Thry.match_term thy asm) tc)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   898
          then R.UNDISCH
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   899
                 (R.MATCH_MP
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   900
                     (R.MATCH_MP Thms.simp_thm (R.DISCH (tych asm) ind))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   901
                     hth)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   902
         else loop rst
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   903
  in loop asl
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   904
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   905
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   906
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   907
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   908
 * The termination condition is an antecedent to the rule, and an
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   909
 * assumption to the theorem.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   910
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   911
fun elim_tc tcthm (rule,induction) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   912
   (R.MP rule tcthm, R.PROVE_HYP tcthm induction)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   913
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   914
14240
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   915
fun trace_thms s L =
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   916
  if !trace then writeln (cat_lines (s :: map string_of_thm L))
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   917
  else ();
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   918
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   919
fun trace_cterms s L =
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   920
  if !trace then writeln (cat_lines (s :: map string_of_cterm L))
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   921
  else ();;
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   922
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   923
11632
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
   924
fun postprocess strict {wf_tac, terminator, simplifier} theory {rules,induction,TCs} =
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   925
let val tych = Thry.typecheck theory
11632
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
   926
    val prove = R.prove strict;
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   927
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   928
   (*---------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   929
    * Attempt to eliminate WF condition. It's the only assumption of rules
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   930
    *---------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   931
   val (rules1,induction1)  =
11632
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
   932
       let val thm = prove(tych(HOLogic.mk_Trueprop
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   933
                                  (hd(#1(R.dest_thm rules)))),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   934
                             wf_tac)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   935
       in (R.PROVE_HYP thm rules,  R.PROVE_HYP thm induction)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   936
       end handle U.ERR _ => (rules,induction);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   937
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   938
   (*----------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   939
    * The termination condition (tc) is simplified to |- tc = tc' (there
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   940
    * might not be a change!) and then 3 attempts are made:
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   941
    *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   942
    *   1. if |- tc = T, then eliminate it with eqT; otherwise,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   943
    *   2. apply the terminator to tc'. If |- tc' = T then eliminate; else
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   944
    *   3. replace tc by tc' in both the rules and the induction theorem.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   945
    *---------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   946
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   947
   fun simplify_tc tc (r,ind) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   948
       let val tc1 = tych tc
14240
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   949
           val _ = trace_cterms "TC before simplification: " [tc1]
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   950
           val tc_eq = simplifier tc1
14240
d3843feb9de7 improved tracing
paulson
parents: 14219
diff changeset
   951
           val _ = trace_thms "result: " [tc_eq]
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   952
       in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   953
       elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   954
       handle U.ERR _ =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   955
        (elim_tc (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
11632
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
   956
                  (prove(tych(HOLogic.mk_Trueprop(S.rhs(concl tc_eq))),
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   957
                           terminator)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   958
                 (r,ind)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   959
         handle U.ERR _ =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   960
          (R.UNDISCH(R.MATCH_MP (R.MATCH_MP Thms.simp_thm r) tc_eq),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   961
           simplify_induction theory tc_eq ind))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   962
       end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   963
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   964
   (*----------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   965
    * Nested termination conditions are harder to get at, since they are
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   966
    * left embedded in the body of the function (and in induction
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   967
    * theorem hypotheses). Our "solution" is to simplify them, and try to
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   968
    * prove termination, but leave the application of the resulting theorem
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   969
    * to a higher level. So things go much as in "simplify_tc": the
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   970
    * termination condition (tc) is simplified to |- tc = tc' (there might
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   971
    * not be a change) and then 2 attempts are made:
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   972
    *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   973
    *   1. if |- tc = T, then return |- tc; otherwise,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   974
    *   2. apply the terminator to tc'. If |- tc' = T then return |- tc; else
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   975
    *   3. return |- tc = tc'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   976
    *---------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   977
   fun simplify_nested_tc tc =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   978
      let val tc_eq = simplifier (tych (#2 (S.strip_forall tc)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   979
      in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   980
      R.GEN_ALL
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   981
       (R.MATCH_MP Thms.eqT tc_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   982
        handle U.ERR _ =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   983
          (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
11632
6fc8de600f58 prove: ``strict'' argument;
wenzelm
parents: 11455
diff changeset
   984
                      (prove(tych(HOLogic.mk_Trueprop (S.rhs(concl tc_eq))),
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   985
                               terminator))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   986
            handle U.ERR _ => tc_eq))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   987
      end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   988
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   989
   (*-------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   990
    * Attempt to simplify the termination conditions in each rule and
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   991
    * in the induction theorem.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   992
    *-------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   993
   fun strip_imp tm = if S.is_neg tm then ([],tm) else S.strip_imp tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   994
   fun loop ([],extras,R,ind) = (rev R, ind, extras)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   995
     | loop ((r,ftcs)::rst, nthms, R, ind) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   996
        let val tcs = #1(strip_imp (concl r))
20951
868120282837 gen_rem(s) abandoned in favour of remove / subtract
haftmann
parents: 20548
diff changeset
   997
            val extra_tcs = subtract (op aconv) tcs ftcs
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   998
            val extra_tc_thms = map simplify_nested_tc extra_tcs
16853
33b886cbdc8f replaced itlist by fold_rev;
wenzelm
parents: 16505
diff changeset
   999
            val (r1,ind1) = fold simplify_tc tcs (r,ind)
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1000
            val r2 = R.FILTER_DISCH_ALL(not o S.is_WFR) r1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1001
        in loop(rst, nthms@extra_tc_thms, r2::R, ind1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1002
        end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1003
   val rules_tcs = ListPair.zip (R.CONJUNCTS rules1, TCs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1004
   val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1005
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1006
  {induction = ind2, rules = R.LIST_CONJ rules2, nested_tcs = extras}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1007
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1008
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1009
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
  1010
end;