TFL/tfl.sml
author paulson
Fri, 18 Oct 1996 12:41:04 +0200
changeset 2112 3902e9af752f
child 3191 14bd6e5985f1
permissions -rw-r--r--
Konrad Slind's TFL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     1
functor TFL(structure Rules : Rules_sig
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     2
            structure Thry  : Thry_sig
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     3
            structure Thms  : Thms_sig
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     4
            sharing type Rules.binding = Thry.binding = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     5
                         Thry.USyntax.binding = Mask.binding
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     6
            sharing type Rules.Type = Thry.Type = Thry.USyntax.Type
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     7
            sharing type Rules.Preterm = Thry.Preterm = Thry.USyntax.Preterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     8
            sharing type Rules.Term = Thry.Term = Thry.USyntax.Term
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     9
            sharing type Thms.Thm = Rules.Thm = Thry.Thm) : TFL_sig  =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    10
struct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
(* Declarations *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    13
structure Thms = Thms;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    14
structure Rules = Rules;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
structure Thry = Thry;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    16
structure USyntax = Thry.USyntax;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    17
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    18
type Preterm = Thry.USyntax.Preterm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
type Term = Thry.USyntax.Term;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
type Thm = Thms.Thm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
type Thry = Thry.Thry;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
type Tactic = Rules.Tactic;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    23
   
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    24
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
(* Abbreviations *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    26
structure R = Rules;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    27
structure S = USyntax;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
structure U = S.Utils;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    29
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    30
(* Declares 'a binding datatype *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    31
open Mask;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    32
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
nonfix mem --> |-> ##;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
val --> = S.-->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    35
val ##    = U.##;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    36
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    37
infixr 3 -->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    38
infixr 7 |->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    39
infix  4 ##; 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
val concl = #2 o R.dest_thm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    42
val hyp = #1 o R.dest_thm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    44
val list_mk_type = U.end_itlist (U.curry(op -->));
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    45
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    46
fun flatten [] = []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    47
  | flatten (h::t) = h@flatten t;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    48
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    49
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    50
fun gtake f =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    51
  let fun grab(0,rst) = ([],rst)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    52
        | grab(n, x::rst) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    53
             let val (taken,left) = grab(n-1,rst)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    54
             in (f x::taken, left) end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    55
  in grab
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    56
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    57
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    58
fun enumerate L = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    59
 rev(#1(U.rev_itlist (fn x => fn (alist,i) => ((x,i)::alist, i+1)) L ([],0)));
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
fun stringize [] = ""
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
  | stringize [i] = U.int_to_string i
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    63
  | stringize (h::t) = (U.int_to_string h^", "^stringize t);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    65
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    66
fun TFL_ERR{func,mesg} = U.ERR{module = "Tfl", func = func, mesg = mesg};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    67
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    69
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    70
 * The next function is common to pattern-match translation and 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    71
 * proof of completeness of cases for the induction theorem.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    72
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    73
 * "gvvariant" make variables that are guaranteed not to be in vlist and
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    74
 * furthermore, are guaranteed not to be equal to each other. The names of
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    75
 * the variables will start with "v" and end in a number.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    76
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
local val counter = ref 0
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    78
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
fun gvvariant vlist =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    80
  let val slist = ref (map (#Name o S.dest_var) vlist)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    81
      val mem = U.mem (U.curry (op=))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    82
      val _ = counter := 0
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    83
      fun pass str = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    84
         if (mem str (!slist)) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    85
         then ( counter := !counter + 1;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
                pass (U.concat"v" (U.int_to_string(!counter))))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    87
         else (slist := str :: !slist; str)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    88
  in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    89
  fn ty => S.mk_var{Name=pass "v",  Ty=ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    90
  end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    91
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    92
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    93
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    94
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    95
 * Used in induction theorem production. This is the simple case of
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    96
 * partitioning up pattern rows by the leading constructor.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    97
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    98
fun ipartition gv (constructors,rows) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    99
  let fun pfail s = raise TFL_ERR{func = "partition.part", mesg = s}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   100
      fun part {constrs = [],   rows = [],   A} = rev A
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   101
        | part {constrs = [],   rows = _::_, A} = pfail"extra cases in defn"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   102
        | part {constrs = _::_, rows = [],   A} = pfail"cases missing in defn"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   103
        | part {constrs = c::crst, rows,     A} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   104
          let val {Name,Ty} = S.dest_const c
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   105
              val (L,_) = S.strip_type Ty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   106
              val (in_group, not_in_group) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   107
               U.itlist (fn (row as (p::rst, rhs)) =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   108
                         fn (in_group,not_in_group) =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   109
                  let val (pc,args) = S.strip_comb p
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   110
                  in if (#Name(S.dest_const pc) = Name)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   111
                     then ((args@rst, rhs)::in_group, not_in_group)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   112
                     else (in_group, row::not_in_group)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   113
                  end)      rows ([],[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   114
              val col_types = U.take S.type_of (length L, #1(hd in_group))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   115
          in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   116
          part{constrs = crst, rows = not_in_group, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   117
               A = {constructor = c, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   118
                    new_formals = map gv col_types,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   119
                    group = in_group}::A}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   120
          end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   121
  in part{constrs = constructors, rows = rows, A = []}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   122
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   123
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   124
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   125
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   126
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   127
 * This datatype carries some information about the origin of a
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   128
 * clause in a function definition.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   129
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   130
datatype pattern = GIVEN   of S.Preterm * int
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   131
                 | OMITTED of S.Preterm * int
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   132
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   133
fun psubst theta (GIVEN (tm,i)) = GIVEN(S.subst theta tm, i)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   134
  | psubst theta (OMITTED (tm,i)) = OMITTED(S.subst theta tm, i);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   135
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   136
fun dest_pattern (GIVEN (tm,i)) = ((GIVEN,i),tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   137
  | dest_pattern (OMITTED (tm,i)) = ((OMITTED,i),tm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   138
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   139
val pat_of = #2 o dest_pattern;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   140
val row_of_pat = #2 o #1 o dest_pattern;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   141
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   142
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   143
 * Produce an instance of a constructor, plus genvars for its arguments.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   144
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   145
fun fresh_constr ty_match colty gv c =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   146
  let val {Ty,...} = S.dest_const c
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   147
      val (L,ty) = S.strip_type Ty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   148
      val ty_theta = ty_match ty colty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   149
      val c' = S.inst ty_theta c
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   150
      val gvars = map (S.inst ty_theta o gv) L
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   151
  in (c', gvars)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   152
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   153
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   154
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   155
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   156
 * Goes through a list of rows and picks out the ones beginning with a
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   157
 * pattern with constructor = Name.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   158
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   159
fun mk_group Name rows =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   160
  U.itlist (fn (row as ((prefix, p::rst), rhs)) =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   161
            fn (in_group,not_in_group) =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   162
               let val (pc,args) = S.strip_comb p
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   163
               in if ((#Name(S.dest_const pc) = Name) handle _ => false)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   164
                  then (((prefix,args@rst), rhs)::in_group, not_in_group)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   165
                  else (in_group, row::not_in_group) end)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   166
      rows ([],[]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   167
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   168
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   169
 * Partition the rows. Not efficient: we should use hashing.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   170
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   171
fun partition _ _ (_,_,_,[]) = raise TFL_ERR{func="partition", mesg="no rows"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   172
  | partition gv ty_match
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   173
              (constructors, colty, res_ty, rows as (((prefix,_),_)::_)) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   174
let val fresh = fresh_constr ty_match colty gv
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   175
     fun part {constrs = [],      rows, A} = rev A
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   176
       | part {constrs = c::crst, rows, A} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   177
         let val (c',gvars) = fresh c
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   178
             val {Name,Ty} = S.dest_const c'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   179
             val (in_group, not_in_group) = mk_group Name rows
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   180
             val in_group' =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   181
                 if (null in_group)  (* Constructor not given *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   182
                 then [((prefix, #2(fresh c)), OMITTED (S.ARB res_ty, ~1))]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   183
                 else in_group
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   184
         in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   185
         part{constrs = crst, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   186
              rows = not_in_group, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   187
              A = {constructor = c', 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   188
                   new_formals = gvars,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   189
                   group = in_group'}::A}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   190
         end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   191
in part{constrs=constructors, rows=rows, A=[]}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   192
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   193
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   194
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   195
 * Misc. routines used in mk_case
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   196
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   197
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   198
fun mk_pat c =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   199
  let val L = length(#1(S.strip_type(S.type_of c)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   200
      fun build (prefix,tag,plist) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   201
          let val (args,plist') = gtake U.I (L, plist)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   202
           in (prefix,tag,S.list_mk_comb(c,args)::plist') end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   203
  in map build 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   204
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   205
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   206
fun v_to_prefix (prefix, v::pats) = (v::prefix,pats)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   207
  | v_to_prefix _ = raise TFL_ERR{func="mk_case", mesg="v_to_prefix"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   208
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   209
fun v_to_pats (v::prefix,tag, pats) = (prefix, tag, v::pats)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   210
  | v_to_pats _ = raise TFL_ERR{func="mk_case", mesg="v_to_pats"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   211
 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   212
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   213
(*----------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   214
 * Translation of pattern terms into nested case expressions.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   215
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   216
 * This performs the translation and also builds the full set of patterns. 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   217
 * Thus it supports the construction of induction theorems even when an 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   218
 * incomplete set of patterns is given.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   219
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   220
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   221
fun mk_case ty_info ty_match FV range_ty =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   222
 let 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   223
 fun mk_case_fail s = raise TFL_ERR{func = "mk_case", mesg = s}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   224
 val fresh_var = gvvariant FV 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   225
 val divide = partition fresh_var ty_match
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   226
 fun expand constructors ty ((_,[]), _) = mk_case_fail"expand_var_row"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   227
   | expand constructors ty (row as ((prefix, p::rst), rhs)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   228
       if (S.is_var p) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   229
       then let val fresh = fresh_constr ty_match ty fresh_var
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   230
                fun expnd (c,gvs) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   231
                  let val capp = S.list_mk_comb(c,gvs)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   232
                  in ((prefix, capp::rst), psubst[p |-> capp] rhs)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   233
                  end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   234
            in map expnd (map fresh constructors)  end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   235
       else [row]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   236
 fun mk{rows=[],...} = mk_case_fail"no rows"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   237
   | mk{path=[], rows = ((prefix, []), rhs)::_} =  (* Done *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   238
        let val (tag,tm) = dest_pattern rhs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   239
        in ([(prefix,tag,[])], tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   240
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   241
   | mk{path=[], rows = _::_} = mk_case_fail"blunder"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   242
   | mk{path as u::rstp, rows as ((prefix, []), rhs)::rst} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   243
        mk{path = path, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   244
           rows = ((prefix, [fresh_var(S.type_of u)]), rhs)::rst}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   245
   | mk{path = u::rstp, rows as ((_, p::_), _)::_} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   246
     let val (pat_rectangle,rights) = U.unzip rows
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   247
         val col0 = map(hd o #2) pat_rectangle
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   248
     in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   249
     if (U.all S.is_var col0) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   250
     then let val rights' = map(fn(v,e) => psubst[v|->u] e) (U.zip col0 rights)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   251
              val pat_rectangle' = map v_to_prefix pat_rectangle
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   252
              val (pref_patl,tm) = mk{path = rstp,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   253
                                      rows = U.zip pat_rectangle' rights'}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   254
          in (map v_to_pats pref_patl, tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   255
          end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   256
     else
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   257
     let val pty = S.type_of p
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   258
         val ty_name = (#Tyop o S.dest_type) pty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   259
     in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   260
     case (ty_info ty_name)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   261
     of U.NONE => mk_case_fail("Not a known datatype: "^ty_name)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   262
      | U.SOME{case_const,constructors} =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   263
        let val case_const_name = #Name(S.dest_const case_const)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   264
            val nrows = flatten (map (expand constructors pty) rows)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   265
            val subproblems = divide(constructors, pty, range_ty, nrows)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   266
            val groups      = map #group subproblems
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   267
            and new_formals = map #new_formals subproblems
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   268
            and constructors' = map #constructor subproblems
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   269
            val news = map (fn (nf,rows) => {path = nf@rstp, rows=rows})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   270
                           (U.zip new_formals groups)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   271
            val rec_calls = map mk news
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   272
            val (pat_rect,dtrees) = U.unzip rec_calls
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   273
            val case_functions = map S.list_mk_abs(U.zip new_formals dtrees)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   274
            val types = map S.type_of (case_functions@[u]) @ [range_ty]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   275
            val case_const' = S.mk_const{Name = case_const_name,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   276
                                         Ty   = list_mk_type types}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   277
            val tree = S.list_mk_comb(case_const', case_functions@[u])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   278
            val pat_rect1 = flatten(U.map2 mk_pat constructors' pat_rect)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   279
        in (pat_rect1,tree)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   280
        end 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   281
     end end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   282
 in mk
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   283
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   284
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   285
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   286
(* Repeated variable occurrences in a pattern are not allowed. *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   287
fun FV_multiset tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   288
   case (S.dest_term tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   289
     of S.VAR v => [S.mk_var v]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   290
      | S.CONST _ => []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   291
      | S.COMB{Rator, Rand} => FV_multiset Rator @ FV_multiset Rand
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   292
      | S.LAMB _ => raise TFL_ERR{func = "FV_multiset", mesg = "lambda"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   293
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   294
fun no_repeat_vars thy pat =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   295
 let fun check [] = true
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   296
       | check (v::rst) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   297
         if (U.mem S.aconv v rst) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   298
         then raise TFL_ERR{func = "no_repeat_vars",
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   299
             mesg = U.concat(U.quote(#Name(S.dest_var v)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   300
                     (U.concat" occurs repeatedly in the pattern "
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   301
                         (U.quote(S.Term_to_string (Thry.typecheck thy pat))))}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   302
         else check rst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   303
 in check (FV_multiset pat)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   304
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   305
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   306
local fun paired1{lhs,rhs} = (lhs,rhs) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   307
      and paired2{Rator,Rand} = (Rator,Rand)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   308
      fun mk_functional_err s = raise TFL_ERR{func = "mk_functional", mesg=s}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   309
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   310
fun mk_functional thy eqs =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   311
 let val clauses = S.strip_conj eqs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   312
     val (L,R) = U.unzip (map (paired1 o S.dest_eq o U.snd o S.strip_forall)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   313
                              clauses)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   314
     val (funcs,pats) = U.unzip(map (paired2 o S.dest_comb) L)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   315
     val [f] = U.mk_set (S.aconv) funcs 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   316
               handle _ => mk_functional_err "function name not unique"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   317
     val _ = map (no_repeat_vars thy) pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   318
     val rows = U.zip (map (fn x => ([],[x])) pats) (map GIVEN (enumerate R))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   319
     val fvs = S.free_varsl R
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   320
     val a = S.variant fvs (S.mk_var{Name="a", Ty = S.type_of(hd pats)})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   321
     val FV = a::fvs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   322
     val ty_info = Thry.match_info thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   323
     val ty_match = Thry.match_type thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   324
     val range_ty = S.type_of (hd R)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   325
     val (patts, case_tm) = mk_case ty_info ty_match FV range_ty 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   326
                                    {path=[a], rows=rows}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   327
     val patts1 = map (fn (_,(tag,i),[pat]) => tag (pat,i)) patts handle _ 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   328
                  => mk_functional_err "error in pattern-match translation"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   329
     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
   330
     val finals = map row_of_pat patts2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   331
     val originals = map (row_of_pat o #2) rows
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   332
     fun int_eq i1 (i2:int) =  (i1=i2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   333
     val _ = case (U.set_diff int_eq originals finals)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   334
             of [] => ()
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   335
          | L => mk_functional_err("The following rows (counting from zero)\
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   336
                                   \ are inaccessible: "^stringize L)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   337
 in {functional = S.list_mk_abs ([f,a], case_tm),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   338
     pats = patts2}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   339
end end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   340
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   341
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   342
(*----------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   343
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   344
 *                    PRINCIPLES OF DEFINITION
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   345
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   346
 *---------------------------------------------------------------------------*)
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
 * This basic principle of definition takes a functional M and a relation R
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   351
 * and specializes the following theorem
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   352
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   353
 *    |- !M R f. (f = WFREC R M) ==> WF R ==> !x. f x = M (f%R,x) x
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   354
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   355
 * to them (getting "th1", say). Then we make the definition "f = WFREC R M" 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   356
 * and instantiate "th1" to the constant "f" (getting th2). Then we use the
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   357
 * definition to delete the first antecedent to th2. Hence the result in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   358
 * the "corollary" field is 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   359
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   360
 *    |-  WF R ==> !x. f x = M (f%R,x) x
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   361
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   362
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   363
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   364
fun prim_wfrec_definition thy {R, functional} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   365
 let val tych = Thry.typecheck thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   366
     val {Bvar,...} = S.dest_abs functional
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   367
     val {Name,...} = S.dest_var Bvar  (* Intended name of definition *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   368
     val cor1 = R.ISPEC (tych functional) Thms.WFREC_COROLLARY
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   369
     val cor2 = R.ISPEC (tych R) cor1
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   370
     val f_eq_WFREC_R_M = (#ant o S.dest_imp o #Body 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   371
                           o S.dest_forall o concl) cor2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   372
     val {lhs,rhs} = S.dest_eq f_eq_WFREC_R_M
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   373
     val {Ty, ...} = S.dest_var lhs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   374
     val def_term = S.mk_eq{lhs = S.mk_var{Name=Name,Ty=Ty}, rhs=rhs}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   375
     val (def_thm,thy1) = Thry.make_definition thy 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   376
                                  (U.concat Name "_def") def_term
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   377
     val (_,[f,_]) = (S.strip_comb o concl) def_thm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   378
     val cor3 = R.ISPEC (Thry.typecheck thy1 f) cor2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   379
 in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   380
 {theory = thy1, def=def_thm, corollary=R.MP cor3 def_thm}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   381
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   382
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   383
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   384
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   385
 * This structure keeps track of congruence rules that aren't derived
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   386
 * from a datatype definition.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   387
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   388
structure Context =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   389
struct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   390
  val non_datatype_context = ref []:Rules.Thm list ref
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   391
  fun read() = !non_datatype_context
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   392
  fun write L = (non_datatype_context := L)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   393
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   394
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   395
fun extraction_thms thy = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   396
 let val {case_rewrites,case_congs} = Thry.extract_info thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   397
 in (case_rewrites, case_congs@Context.read())
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   398
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   399
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   400
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   401
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   402
 * Pair patterns with termination conditions. The full list of patterns for
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   403
 * a definition is merged with the TCs arising from the user-given clauses.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   404
 * There can be fewer clauses than the full list, if the user omitted some 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   405
 * cases. This routine is used to prepare input for mk_induction.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   406
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   407
fun merge full_pats TCs =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   408
let fun insert (p,TCs) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   409
      let fun insrt ((x as (h,[]))::rst) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   410
                 if (S.aconv p h) then (p,TCs)::rst else x::insrt rst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   411
            | insrt (x::rst) = x::insrt rst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   412
            | insrt[] = raise TFL_ERR{func="merge.insert",mesg="pat not found"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   413
      in insrt end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   414
    fun pass ([],ptcl_final) = ptcl_final
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   415
      | pass (ptcs::tcl, ptcl) = pass(tcl, insert ptcs ptcl)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   416
in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   417
  pass (TCs, map (fn p => (p,[])) full_pats)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   418
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   419
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   420
fun not_omitted (GIVEN(tm,_)) = tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   421
  | not_omitted (OMITTED _) = raise TFL_ERR{func="not_omitted",mesg=""}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   422
val givens = U.mapfilter not_omitted;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   423
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   424
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   425
(*--------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   426
 * This is a wrapper for "prim_wfrec_definition": it builds a functional,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   427
 * calls "prim_wfrec_definition", then specializes the result. This gives a
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   428
 * list of rewrite rules where the right hand sides are quite ugly, so we 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   429
 * simplify to get rid of the case statements. In essence, this function
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   430
 * performs pre- and post-processing for patterns. As well, after
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   431
 * simplification, termination conditions are extracted.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   432
 *-------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   433
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   434
fun gen_wfrec_definition thy {R, eqs} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   435
 let val {functional,pats}  = mk_functional thy eqs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   436
     val given_pats = givens pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   437
     val {def,corollary,theory} = prim_wfrec_definition thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   438
                                        {R=R, functional=functional}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   439
     val tych = Thry.typecheck theory 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   440
     val {lhs=f,...} = S.dest_eq(concl def)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   441
     val WFR = #ant(S.dest_imp(concl corollary))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   442
     val corollary' = R.UNDISCH corollary  (* put WF R on assums *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   443
     val corollaries = map (U.C R.SPEC corollary' o tych) given_pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   444
     val (case_rewrites,context_congs) = extraction_thms thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   445
     val corollaries' = map(R.simplify case_rewrites) corollaries
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   446
     fun xtract th = R.CONTEXT_REWRITE_RULE(f,R)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   447
                         {thms = [(R.ISPECL o map tych)[f,R] Thms.CUT_LEMMA],
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   448
                         congs = context_congs,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   449
                            th = th}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   450
     val (rules, TCs) = U.unzip (map xtract corollaries')
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   451
     val rules0 = map (R.simplify [Thms.CUT_DEF]) rules
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   452
     val mk_cond_rule = R.FILTER_DISCH_ALL(not o S.aconv WFR)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   453
     val rules1 = R.LIST_CONJ(map mk_cond_rule rules0)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   454
 in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   455
 {theory = theory,   (* holds def, if it's needed *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   456
  rules = rules1,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   457
  full_pats_TCs = merge (map pat_of pats) (U.zip given_pats TCs), 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   458
  TCs = TCs, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   459
  patterns = pats}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   460
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   461
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   462
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   463
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   464
 * Perform the extraction without making the definition. Definition and
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   465
 * extraction commute for the non-nested case. For hol90 users, this 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   466
 * function can be invoked without being in draft mode.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   467
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   468
fun wfrec_eqns thy eqns =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   469
 let val {functional,pats} = mk_functional thy eqns
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   470
     val given_pats = givens pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   471
     val {Bvar = f, Body} = S.dest_abs functional
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   472
     val {Bvar = x, ...} = S.dest_abs Body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   473
     val {Name,Ty = fty} = S.dest_var f
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   474
     val {Tyop="fun", Args = [f_dty, f_rty]} = S.dest_type fty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   475
     val (case_rewrites,context_congs) = extraction_thms thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   476
     val tych = Thry.typecheck thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   477
     val WFREC_THM0 = R.ISPEC (tych functional) Thms.WFREC_COROLLARY
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   478
     val R = S.variant(S.free_vars eqns) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   479
                      (#Bvar(S.dest_forall(concl WFREC_THM0)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   480
     val WFREC_THM = R.ISPECL [tych R, tych f] WFREC_THM0
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   481
     val ([proto_def, WFR],_) = S.strip_imp(concl WFREC_THM)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   482
     val R1 = S.rand WFR
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   483
     val corollary' = R.UNDISCH(R.UNDISCH WFREC_THM)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   484
     val corollaries = map (U.C R.SPEC corollary' o tych) given_pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   485
     val corollaries' = map (R.simplify case_rewrites) corollaries
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   486
     fun extract th = R.CONTEXT_REWRITE_RULE(f,R1)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   487
                        {thms = [(R.ISPECL o map tych)[f,R1] Thms.CUT_LEMMA], 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   488
                        congs = context_congs,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   489
                          th  = th}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   490
 in {proto_def=proto_def, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   491
     WFR=WFR, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   492
     pats=pats,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   493
     extracta = map extract corollaries'}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   494
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   495
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   496
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   497
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   498
 * Define the constant after extracting the termination conditions. The 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   499
 * wellfounded relation used in the definition is computed by using the
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   500
 * choice operator on the extracted conditions (plus the condition that
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   501
 * such a relation must be wellfounded).
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   502
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   503
fun lazyR_def thy eqns =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   504
 let val {proto_def,WFR,pats,extracta} = wfrec_eqns thy eqns
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   505
     val R1 = S.rand WFR
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   506
     val f = S.lhs proto_def
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   507
     val {Name,...} = S.dest_var f
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   508
     val (extractants,TCl) = U.unzip extracta
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   509
     val TCs = U.Union S.aconv TCl
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   510
     val full_rqt = WFR::TCs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   511
     val R' = S.mk_select{Bvar=R1, Body=S.list_mk_conj full_rqt}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   512
     val R'abs = S.rand R'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   513
     val (def,theory) = Thry.make_definition thy (U.concat Name "_def") 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   514
                                                 (S.subst[R1 |-> R'] proto_def)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   515
     val fconst = #lhs(S.dest_eq(concl def)) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   516
     val tych = Thry.typecheck theory
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   517
     val baz = R.DISCH (tych proto_def)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   518
                 (U.itlist (R.DISCH o tych) full_rqt (R.LIST_CONJ extractants))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   519
     val def' = R.MP (R.SPEC (tych fconst) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   520
                             (R.SPEC (tych R') (R.GENL[tych R1, tych f] baz)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   521
                     def
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   522
     val body_th = R.LIST_CONJ (map (R.ASSUME o tych) full_rqt)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   523
     val bar = R.MP (R.BETA_RULE(R.ISPECL[tych R'abs, tych R1] Thms.SELECT_AX))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   524
                     body_th
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   525
 in {theory = theory, R=R1,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   526
     rules = U.rev_itlist (U.C R.MP) (R.CONJUNCTS bar) def',
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   527
     full_pats_TCs = merge (map pat_of pats) (U.zip (givens pats) TCl),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   528
     patterns = pats}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   529
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   530
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   531
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   532
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   533
(*----------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   534
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   535
 *                           INDUCTION THEOREM
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   536
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   537
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   538
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   539
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   540
(*------------------------  Miscellaneous function  --------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   541
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   542
 *           [x_1,...,x_n]     ?v_1...v_n. M[v_1,...,v_n]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   543
 *     -----------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   544
 *     ( 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
   545
 *                        ... 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   546
 *                        (x_j,?v_n. M[x_1,...,x_(n-1),v_n])] )
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   547
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   548
 * This function is totally ad hoc. Used in the production of the induction 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   549
 * theorem. The nchotomy theorem can have clauses that look like
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   550
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   551
 *     ?v1..vn. z = C vn..v1
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   552
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   553
 * in which the order of quantification is not the order of occurrence of the
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   554
 * quantified variables as arguments to C. Since we have no control over this
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   555
 * aspect of the nchotomy theorem, we make the correspondence explicit by
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   556
 * pairing the incoming new variable with the term it gets beta-reduced into.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   557
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   558
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   559
fun alpha_ex_unroll xlist tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   560
  let val (qvars,body) = S.strip_exists tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   561
      val vlist = #2(S.strip_comb (S.rhs body))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   562
      val plist = U.zip vlist xlist
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   563
      val args = map (U.C (U.assoc1 (U.uncurry S.aconv)) plist) qvars
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   564
      val args' = map (fn U.SOME(_,v) => v 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   565
                        | U.NONE => raise TFL_ERR{func = "alpha_ex_unroll",
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   566
                                       mesg = "no correspondence"}) args
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   567
      fun build ex [] = []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   568
        | build ex (v::rst) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   569
           let val ex1 = S.beta_conv(S.mk_comb{Rator=S.rand ex, Rand=v})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   570
           in ex1::build ex1 rst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   571
           end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   572
     val (nex::exl) = rev (tm::build tm args')
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   573
  in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   574
  (nex, U.zip args' (rev exl))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   575
  end;
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
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   579
(*----------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   580
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   581
 *             PROVING COMPLETENESS OF PATTERNS
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   582
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   583
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   584
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   585
fun mk_case ty_info FV thy =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   586
 let 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   587
 val divide = ipartition (gvvariant FV)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   588
 val tych = Thry.typecheck thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   589
 fun tych_binding(x|->y) = (tych x |-> tych y)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   590
 fun fail s = raise TFL_ERR{func = "mk_case", mesg = s}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   591
 fun mk{rows=[],...} = fail"no rows"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   592
   | mk{path=[], rows = [([], (thm, bindings))]} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   593
                         R.IT_EXISTS (map tych_binding bindings) thm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   594
   | mk{path = u::rstp, rows as (p::_, _)::_} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   595
     let val (pat_rectangle,rights) = U.unzip rows
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   596
         val col0 = map hd pat_rectangle
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   597
         val pat_rectangle' = map tl pat_rectangle
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   598
     in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   599
     if (U.all S.is_var col0) (* column 0 is all variables *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   600
     then let val rights' = map (fn ((thm,theta),v) => (thm,theta@[u|->v]))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   601
                                (U.zip rights col0)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   602
          in mk{path = rstp, rows = U.zip pat_rectangle' rights'}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   603
          end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   604
     else                     (* column 0 is all constructors *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   605
     let val ty_name = (#Tyop o S.dest_type o S.type_of) p
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   606
     in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   607
     case (ty_info ty_name)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   608
     of U.NONE => fail("Not a known datatype: "^ty_name)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   609
      | U.SOME{constructors,nchotomy} =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   610
        let val thm' = R.ISPEC (tych u) nchotomy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   611
            val disjuncts = S.strip_disj (concl thm')
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   612
            val subproblems = divide(constructors, rows)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   613
            val groups      = map #group subproblems
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   614
            and new_formals = map #new_formals subproblems
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   615
            val existentials = U.map2 alpha_ex_unroll new_formals disjuncts
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   616
            val constraints = map #1 existentials
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   617
            val vexl = map #2 existentials
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   618
            fun expnd tm (pats,(th,b)) = (pats,(R.SUBS[R.ASSUME(tych tm)]th,b))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   619
            val news = map (fn (nf,rows,c) => {path = nf@rstp, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   620
                                               rows = map (expnd c) rows})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   621
                           (U.zip3 new_formals groups constraints)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   622
            val recursive_thms = map mk news
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   623
            val build_exists = U.itlist(R.CHOOSE o (tych##(R.ASSUME o tych)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   624
            val thms' = U.map2 build_exists vexl recursive_thms
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   625
            val same_concls = R.EVEN_ORS thms'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   626
        in R.DISJ_CASESL thm' same_concls
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   627
        end 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   628
     end end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   629
 in mk
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   630
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   631
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   632
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   633
fun complete_cases thy =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   634
 let val tych = Thry.typecheck thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   635
     fun pmk_var n ty = S.mk_var{Name = n,Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   636
     val ty_info = Thry.induct_info thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   637
 in fn pats =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   638
 let val FV0 = S.free_varsl pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   639
     val a = S.variant FV0 (pmk_var "a" (S.type_of(hd pats)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   640
     val v = S.variant (a::FV0) (pmk_var "v" (S.type_of a))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   641
     val FV = a::v::FV0
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   642
     val a_eq_v = S.mk_eq{lhs = a, rhs = v}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   643
     val ex_th0 = R.EXISTS ((tych##tych) (S.mk_exists{Bvar=v,Body=a_eq_v},a))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   644
                           (R.REFL (tych a))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   645
     val th0 = R.ASSUME (tych a_eq_v)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   646
     val rows = map (fn x => ([x], (th0,[]))) pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   647
 in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   648
 R.GEN (tych a) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   649
       (R.RIGHT_ASSOC
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   650
          (R.CHOOSE(tych v, ex_th0)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   651
                (mk_case ty_info FV thy {path=[v], rows=rows})))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   652
 end end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   653
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   654
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   655
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   656
 * Constructing induction hypotheses: one for each recursive call.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   657
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   658
 * Note. R will never occur as a variable in the ind_clause, because
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   659
 * 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
   660
 * allow nested defns to have R variable.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   661
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   662
 * Note. When the context is empty, there can be no local variables.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   663
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   664
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   665
local nonfix ^ ;   infix 9 ^  ;     infix 5 ==>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   666
      fun (tm1 ^ tm2)   = S.mk_comb{Rator = tm1, Rand = tm2}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   667
      fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   668
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   669
fun build_ih f P (pat,TCs) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   670
 let val globals = S.free_vars_lr pat
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   671
     fun nested tm = U.can(S.find_term (S.aconv f)) tm handle _ => false
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   672
     fun dest_TC tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   673
         let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   674
             val (R,y,_) = S.dest_relation R_y_pat
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   675
             val P_y = if (nested tm) then R_y_pat ==> P^y else P^y
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   676
         in case cntxt 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   677
              of [] => (P_y, (tm,[]))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   678
               | _  => let 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   679
                    val imp = S.list_mk_conj cntxt ==> P_y
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   680
                    val lvs = U.set_diff S.aconv (S.free_vars_lr imp) globals
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   681
                    val locals = #2(U.pluck (S.aconv P) lvs) handle _ => lvs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   682
                    in (S.list_mk_forall(locals,imp), (tm,locals)) end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   683
         end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   684
 in case TCs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   685
    of [] => (S.list_mk_forall(globals, P^pat), [])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   686
     |  _ => let val (ihs, TCs_locals) = U.unzip(map dest_TC TCs)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   687
                 val ind_clause = S.list_mk_conj ihs ==> P^pat
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   688
             in (S.list_mk_forall(globals,ind_clause), TCs_locals)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   689
             end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   690
 end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   691
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   692
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   693
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   694
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   695
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   696
 * This function makes good on the promise made in "build_ih: we prove
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   697
 * <something>.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   698
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   699
 * Input  is tm = "(!y. R y pat ==> P y) ==> P pat",  
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   700
 *           TCs = TC_1[pat] ... TC_n[pat]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   701
 *           thm = ih1 /\ ... /\ ih_n |- ih[pat]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   702
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   703
fun prove_case f thy (tm,TCs_locals,thm) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   704
 let val tych = Thry.typecheck thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   705
     val antc = tych(#ant(S.dest_imp tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   706
     val thm' = R.SPEC_ALL thm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   707
     fun nested tm = U.can(S.find_term (S.aconv f)) tm handle _ => false
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   708
     fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC)))))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   709
     fun mk_ih ((TC,locals),th2,nested) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   710
         R.GENL (map tych locals)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   711
            (if nested 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   712
              then R.DISCH (get_cntxt TC) th2 handle _ => th2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   713
               else if S.is_imp(concl TC) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   714
                     then R.IMP_TRANS TC th2 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   715
                      else R.MP th2 TC)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   716
 in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   717
 R.DISCH antc
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   718
 (if S.is_imp(concl thm') (* recursive calls in this clause *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   719
  then let val th1 = R.ASSUME antc
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   720
           val TCs = map #1 TCs_locals
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   721
           val ylist = map (#2 o S.dest_relation o #2 o S.strip_imp o 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   722
                            #2 o S.strip_forall) TCs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   723
           val TClist = map (fn(TC,lvs) => (R.SPEC_ALL(R.ASSUME(tych TC)),lvs))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   724
                            TCs_locals
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   725
           val th2list = map (U.C R.SPEC th1 o tych) ylist
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   726
           val nlist = map nested TCs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   727
           val triples = U.zip3 TClist th2list nlist
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   728
           val Pylist = map mk_ih triples
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   729
       in R.MP thm' (R.LIST_CONJ Pylist) end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   730
  else thm')
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   731
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   732
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   733
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   734
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   735
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   736
 *         x = (v1,...,vn)  |- M[x]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   737
 *    ---------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   738
 *      ?v1 ... vn. x = (v1,...,vn) |- M[x]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   739
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   740
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   741
fun LEFT_ABS_VSTRUCT tych thm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   742
  let fun CHOOSER v (tm,thm) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   743
        let val ex_tm = S.mk_exists{Bvar=v,Body=tm}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   744
        in (ex_tm, R.CHOOSE(tych v, R.ASSUME (tych ex_tm)) thm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   745
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   746
      val [veq] = U.filter (U.can S.dest_eq) (#1 (R.dest_thm thm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   747
      val {lhs,rhs} = S.dest_eq veq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   748
      val L = S.free_vars_lr rhs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   749
  in U.snd(U.itlist CHOOSER L (veq,thm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   750
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   751
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   752
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   753
fun combize M N = S.mk_comb{Rator=M,Rand=N};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   754
fun eq v tm = S.mk_eq{lhs=v,rhs=tm};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   755
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   756
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   757
(*----------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   758
 * Input : f, R,  and  [(pat1,TCs1),..., (patn,TCsn)]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   759
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   760
 * Instantiates WF_INDUCTION_THM, getting Sinduct and then tries to prove
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   761
 * recursion induction (Rinduct) by proving the antecedent of Sinduct from 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   762
 * the antecedent of Rinduct.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   763
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   764
fun mk_induction thy f R pat_TCs_list =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   765
let val tych = Thry.typecheck thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   766
    val Sinduction = R.UNDISCH (R.ISPEC (tych R) Thms.WF_INDUCTION_THM)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   767
    val (pats,TCsl) = U.unzip pat_TCs_list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   768
    val case_thm = complete_cases thy pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   769
    val domain = (S.type_of o hd) pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   770
    val P = S.variant (S.all_varsl (pats@flatten TCsl))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   771
                      (S.mk_var{Name="P", Ty=domain --> S.bool})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   772
    val Sinduct = R.SPEC (tych P) Sinduction
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   773
    val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   774
    val Rassums_TCl' = map (build_ih f P) pat_TCs_list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   775
    val (Rassums,TCl') = U.unzip Rassums_TCl'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   776
    val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   777
    val cases = map (S.beta_conv o combize Sinduct_assumf) pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   778
    val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   779
    val proved_cases = map (prove_case f thy) tasks
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   780
    val v = S.variant (S.free_varsl (map concl proved_cases))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   781
                      (S.mk_var{Name="v", Ty=domain})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   782
    val vtyped = tych v
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   783
    val substs = map (R.SYM o R.ASSUME o tych o eq v) pats
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   784
    val proved_cases1 = U.map2 (fn th => R.SUBS[th]) substs proved_cases
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   785
    val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   786
    val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   787
    val dc = R.MP Sinduct dant
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   788
    val Parg_ty = S.type_of(#Bvar(S.dest_forall(concl dc)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   789
    val vars = map (gvvariant[P]) (S.strip_prod_type Parg_ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   790
    val dc' = U.itlist (R.GEN o tych) vars
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   791
                       (R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   792
in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   793
   R.GEN (tych P) (R.DISCH (tych(concl Rinduct_assum)) dc')
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   794
end 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   795
handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   796
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   797
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   798
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
 *                        POST PROCESSING
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   802
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   803
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   804
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   805
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   806
fun simplify_induction thy hth ind = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   807
  let val tych = Thry.typecheck thy
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   808
      val (asl,_) = R.dest_thm ind
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   809
      val (_,tc_eq_tc') = R.dest_thm hth
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   810
      val tc = S.lhs tc_eq_tc'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   811
      fun loop [] = ind
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   812
        | loop (asm::rst) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   813
          if (U.can (Thry.match_term thy asm) tc)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   814
          then R.UNDISCH
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   815
                 (R.MATCH_MP
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   816
                     (R.MATCH_MP Thms.simp_thm (R.DISCH (tych asm) ind)) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   817
                     hth)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   818
         else loop rst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   819
  in loop asl
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   820
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   821
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   822
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   823
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   824
 * The termination condition is an antecedent to the rule, and an 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   825
 * assumption to the theorem.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   826
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   827
fun elim_tc tcthm (rule,induction) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   828
   (R.MP rule tcthm, R.PROVE_HYP tcthm induction)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   829
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   830
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   831
fun postprocess{WFtac, terminator, simplifier} theory {rules,induction,TCs} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   832
let val tych = Thry.typecheck theory
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   833
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   834
   (*---------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   835
    * Attempt to eliminate WF condition. It's the only assumption of rules
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   836
    *---------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   837
   val (rules1,induction1)  = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   838
       let val thm = R.prove(tych(hd(#1(R.dest_thm rules))),WFtac)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   839
       in (R.PROVE_HYP thm rules,  R.PROVE_HYP thm induction)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   840
       end handle _ => (rules,induction)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   841
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   842
   (*----------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   843
    * The termination condition (tc) is simplified to |- tc = tc' (there
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   844
    * might not be a change!) and then 3 attempts are made:
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   845
    *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   846
    *   1. if |- tc = T, then eliminate it with eqT; otherwise,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   847
    *   2. apply the terminator to tc'. If |- tc' = T then eliminate; else
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   848
    *   3. replace tc by tc' in both the rules and the induction theorem.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   849
    *---------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   850
   fun simplify_tc tc (r,ind) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   851
       let val tc_eq = simplifier (tych tc)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   852
       in 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   853
       elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   854
       handle _ => 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   855
        (elim_tc (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   856
                            (R.prove(tych(S.rhs(concl tc_eq)),terminator)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   857
                 (r,ind)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   858
         handle _ => 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   859
          (R.UNDISCH(R.MATCH_MP (R.MATCH_MP Thms.simp_thm r) tc_eq), 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   860
           simplify_induction theory tc_eq ind))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   861
       end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   862
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   863
   (*----------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   864
    * Nested termination conditions are harder to get at, since they are
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   865
    * left embedded in the body of the function (and in induction 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   866
    * theorem hypotheses). Our "solution" is to simplify them, and try to 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   867
    * prove termination, but leave the application of the resulting theorem 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   868
    * to a higher level. So things go much as in "simplify_tc": the 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   869
    * termination condition (tc) is simplified to |- tc = tc' (there might 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   870
    * not be a change) and then 2 attempts are made:
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   871
    *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   872
    *   1. if |- tc = T, then return |- tc; otherwise,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   873
    *   2. apply the terminator to tc'. If |- tc' = T then return |- tc; else
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   874
    *   3. return |- tc = tc'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   875
    *---------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   876
   fun simplify_nested_tc tc =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   877
      let val tc_eq = simplifier (tych (#2 (S.strip_forall tc)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   878
      in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   879
      R.GEN_ALL
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   880
       (R.MATCH_MP Thms.eqT tc_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   881
        handle _
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   882
        => (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   883
                      (R.prove(tych(S.rhs(concl tc_eq)),terminator))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   884
            handle _ => tc_eq))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   885
      end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   886
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   887
   (*-------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   888
    * Attempt to simplify the termination conditions in each rule and 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   889
    * in the induction theorem.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   890
    *-------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   891
   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
   892
   fun loop ([],extras,R,ind) = (rev R, ind, extras)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   893
     | loop ((r,ftcs)::rst, nthms, R, ind) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   894
        let val tcs = #1(strip_imp (concl r))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   895
            val extra_tcs = U.set_diff S.aconv ftcs tcs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   896
            val extra_tc_thms = map simplify_nested_tc extra_tcs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   897
            val (r1,ind1) = U.rev_itlist simplify_tc tcs (r,ind)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   898
            val r2 = R.FILTER_DISCH_ALL(not o S.is_WFR) r1
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   899
        in loop(rst, nthms@extra_tc_thms, r2::R, ind1)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   900
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   901
   val rules_tcs = U.zip (R.CONJUNCTS rules1) TCs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   902
   val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   903
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   904
  {induction = ind2, rules = R.LIST_CONJ rules2, nested_tcs = extras}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   905
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   906
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   907
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   908
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   909
 * Extract termination goals so that they can be put it into a goalstack, or 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   910
 * have a tactic directly applied to them.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   911
 *--------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   912
local exception IS_NEG 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   913
      fun strip_imp tm = if S.is_neg tm then raise IS_NEG else S.strip_imp tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   914
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   915
fun termination_goals rules = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   916
    U.itlist (fn th => fn A =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   917
        let val tcl = (#1 o S.strip_imp o #2 o S.strip_forall o concl) th
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   918
        in tcl@A
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   919
        end handle _ => A) (R.CONJUNCTS rules) (hyp rules)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   920
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   921
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   922
end; (* TFL *)