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