src/HOL/add_ind_def.ML
author paulson
Mon, 23 Sep 1996 18:18:18 +0200
changeset 2010 0a22b9d63a18
parent 1465 5d7a7e439cec
child 2270 d7513875b2b8
permissions -rw-r--r--
Simplification of definition of synth
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
     1
(*  Title:      HOL/add_ind_def.ML
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     4
    Copyright   1994  University of Cambridge
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     5
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     6
Fixedpoint definition module -- for Inductive/Coinductive Definitions
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     7
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     8
Features:
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     9
* least or greatest fixedpoints
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    10
* user-specified product and sum constructions
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    11
* mutually recursive definitions
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    12
* definitions involving arbitrary monotone operators
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    13
* automatically proves introduction and elimination rules
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    14
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    15
The recursive sets must *already* be declared as constants in parent theory!
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    16
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    17
  Introduction rules have the form
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    18
  [| ti:M(Sj), ..., P(x), ... |] ==> t: Sk |]
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    19
  where M is some monotone operator (usually the identity)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    20
  P(x) is any (non-conjunctive) side condition on the free variables
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    21
  ti, t are any terms
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    22
  Sj, Sk are two of the sets being defined in mutual recursion
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    23
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    24
Sums are used only for mutual recursion;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    25
Products are used only to derive "streamlined" induction rules for relations
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    26
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    27
Nestings of disjoint sum types:
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    28
   (a+(b+c)) for 3,  ((a+b)+(c+d)) for 4,  ((a+b)+(c+(d+e))) for 5,
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    29
   ((a+(b+c))+(d+(e+f))) for 6
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    30
*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    31
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    32
signature FP =          (** Description of a fixed point operator **)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    33
  sig
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    34
  val oper      : string * typ * term -> term   (*fixed point operator*)
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    35
  val Tarski    : thm                   (*Tarski's fixed point theorem*)
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    36
  val induct    : thm                   (*induction/coinduction rule*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    37
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    38
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    39
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    40
signature ADD_INDUCTIVE_DEF =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    41
  sig 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    42
  val add_fp_def_i : term list * term list -> theory -> theory
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    43
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    44
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    45
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    46
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    47
(*Declares functions to add fixedpoint/constructor defs to a theory*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    48
functor Add_inductive_def_Fun (Fp: FP) : ADD_INDUCTIVE_DEF =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    49
struct
1397
b010f04fcb9c Improved error message, suggesting addition of
paulson
parents: 1189
diff changeset
    50
open Ind_Syntax;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    51
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    52
(*internal version*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    53
fun add_fp_def_i (rec_tms, intr_tms) thy = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    54
  let
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    55
    val sign = sign_of thy;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    56
1189
c17a8fd0c95d Changed comments
lcp
parents: 923
diff changeset
    57
    (*rec_params should agree for all mutually recursive components*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    58
    val rec_hds = map head_of rec_tms;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    59
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    60
    val _ = assert_all is_Const rec_hds
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    61
            (fn t => "Recursive set not previously declared as constant: " ^ 
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    62
                     Sign.string_of_term sign t);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    63
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    64
    (*Now we know they are all Consts, so get their names, type and params*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    65
    val rec_names = map (#1 o dest_Const) rec_hds
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    66
    and (Const(_,recT),rec_params) = strip_comb (hd rec_tms);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    67
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    68
    val _ = assert_all Syntax.is_identifier rec_names
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    69
       (fn a => "Name of recursive set not an identifier: " ^ a);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    70
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    71
    local (*Checking the introduction rules*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    72
      val intr_sets = map (#2 o rule_concl_msg sign) intr_tms;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    73
      fun intr_ok set =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    74
          case head_of set of Const(a,_) => a mem rec_names | _ => false;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    75
    in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    76
      val _ =  assert_all intr_ok intr_sets
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    77
         (fn t => "Conclusion of rule does not name a recursive set: " ^ 
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    78
                  Sign.string_of_term sign t);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    79
    end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    80
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    81
    val _ = assert_all is_Free rec_params
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    82
        (fn t => "Param in recursion term not a free variable: " ^
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    83
                 Sign.string_of_term sign t);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    84
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    85
    (*** Construct the lfp definition ***)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    86
    val mk_variant = variant (foldr add_term_names (intr_tms,[]));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    87
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    88
    val z = mk_variant"z" and X = mk_variant"X" and w = mk_variant"w";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    89
1189
c17a8fd0c95d Changed comments
lcp
parents: 923
diff changeset
    90
    (*Mutual recursion ?? *)
c17a8fd0c95d Changed comments
lcp
parents: 923
diff changeset
    91
    val domTs = summands (dest_setT (body_type recT));
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
    92
                (*alternative defn: map (dest_setT o fastype_of) rec_tms *)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    93
    val dom_sumT = fold_bal mk_sum domTs;
1397
b010f04fcb9c Improved error message, suggesting addition of
paulson
parents: 1189
diff changeset
    94
    val dom_set  = mk_setT dom_sumT;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    95
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    96
    val freez   = Free(z, dom_sumT)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    97
    and freeX   = Free(X, dom_set);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    98
    (*type of w may be any of the domTs*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    99
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   100
    fun dest_tprop (Const("Trueprop",_) $ P) = P
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   101
      | dest_tprop Q = error ("Ill-formed premise of introduction rule: " ^ 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   102
                              Sign.string_of_term sign Q);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   103
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   104
    (*Makes a disjunct from an introduction rule*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   105
    fun lfp_part intr = (*quantify over rule's free vars except parameters*)
1397
b010f04fcb9c Improved error message, suggesting addition of
paulson
parents: 1189
diff changeset
   106
      let val prems = map dest_tprop (Logic.strip_imp_prems intr)
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   107
          val _ = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   108
          val exfrees = term_frees intr \\ rec_params
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   109
          val zeq = eq_const dom_sumT $ freez $ (#1 (rule_concl intr))
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   110
      in foldr mk_exists (exfrees, fold_bal (app conj) (zeq::prems)) end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   111
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   112
    (*The Part(A,h) terms -- compose injections to make h*)
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   113
    fun mk_Part (Bound 0, _) = freeX    (*no mutual rec, no Part needed*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   114
      | mk_Part (h, domT)    = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   115
          let val goodh = mend_sum_types (h, dom_sumT)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   116
              and Part_const = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   117
                  Const("Part", [dom_set, domT-->dom_sumT]---> dom_set)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   118
          in  Part_const $ freeX $ Abs(w,domT,goodh)  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   119
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   120
    (*Access to balanced disjoint sums via injections*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   121
    val parts = map mk_Part
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   122
                (accesses_bal (ap Inl, ap Inr, Bound 0) (length domTs) ~~
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   123
                 domTs);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   124
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   125
    (*replace each set by the corresponding Part(A,h)*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   126
    val part_intrs = map (subst_free (rec_tms ~~ parts) o lfp_part) intr_tms;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   127
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   128
    val lfp_rhs = Fp.oper(X, dom_sumT, 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   129
                          mk_Collect(z, dom_sumT, 
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   130
                                     fold_bal (app disj) part_intrs))
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   131
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   132
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   133
    (*** Make the new theory ***)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   134
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   135
    (*A key definition:
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   136
      If no mutual recursion then it equals the one recursive set.
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   137
      If mutual recursion then it differs from all the recursive sets. *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   138
    val big_rec_name = space_implode "_" rec_names;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   139
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   140
    (*Big_rec... is the union of the mutually recursive sets*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   141
    val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   142
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   143
    (*The individual sets must already be declared*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   144
    val axpairs = map mk_defpair 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   145
          ((big_rec_tm, lfp_rhs) ::
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   146
           (case parts of 
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   147
               [_] => []                        (*no mutual recursion*)
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   148
             | _ => rec_tms ~~          (*define the sets as Parts*)
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   149
                    map (subst_atomic [(freeX, big_rec_tm)]) parts));
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   150
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   151
    val _ = seq (writeln o Sign.string_of_term sign o #2) axpairs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   152
  
1397
b010f04fcb9c Improved error message, suggesting addition of
paulson
parents: 1189
diff changeset
   153
    (*Detect occurrences of operator, even with other types!*)
b010f04fcb9c Improved error message, suggesting addition of
paulson
parents: 1189
diff changeset
   154
    val _ = (case rec_names inter (add_term_names (lfp_rhs,[])) of
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   155
               [] => ()
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   156
             | x::_ => error ("Illegal occurrence of recursion op: " ^ x ^
1397
b010f04fcb9c Improved error message, suggesting addition of
paulson
parents: 1189
diff changeset
   157
                               "\n\t*Consider adding type constraints*"))
b010f04fcb9c Improved error message, suggesting addition of
paulson
parents: 1189
diff changeset
   158
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   159
  in  thy |> add_defs_i axpairs  end
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   160
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   161
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   162
(****************************************************************OMITTED
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   163
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   164
(*Expects the recursive sets to have been defined already.
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   165
  con_ty_lists specifies the constructors in the form (name,prems,mixfix) *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   166
fun add_constructs_def (rec_names, con_ty_lists) thy = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   167
* let
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   168
*   val _ = writeln"  Defining the constructor functions...";
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   169
*   val case_name = "f";                (*name for case variables*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   170
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   171
*   (** Define the constructors **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   172
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   173
*   (*The empty tuple is 0*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   174
*   fun mk_tuple [] = Const("0",iT)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   175
*     | mk_tuple args = foldr1 mk_Pair args;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   176
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   177
*   fun mk_inject n k u = access_bal(ap Inl, ap Inr, u) n k;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   178
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   179
*   val npart = length rec_names;       (*total # of mutually recursive parts*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   180
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   181
*   (*Make constructor definition; kpart is # of this mutually recursive part*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   182
*   fun mk_con_defs (kpart, con_ty_list) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   183
*     let val ncon = length con_ty_list    (*number of constructors*)
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   184
          fun mk_def (((id,T,syn), name, args, prems), kcon) =
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   185
                (*kcon is index of constructor*)
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   186
              mk_defpair (list_comb (Const(name,T), args),
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   187
                          mk_inject npart kpart
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   188
                          (mk_inject ncon kcon (mk_tuple args)))
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   189
*     in  map mk_def (con_ty_list ~~ (1 upto ncon))  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   190
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   191
*   (** Define the case operator **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   192
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   193
*   (*Combine split terms using case; yields the case operator for one part*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   194
*   fun call_case case_list = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   195
*     let fun call_f (free,args) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   196
              ap_split T free (map (#2 o dest_Free) args)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   197
*     in  fold_bal (app sum_case) (map call_f case_list)  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   198
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   199
*   (** Generating function variables for the case definition
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   200
        Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   201
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   202
*   (*Treatment of a single constructor*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   203
*   fun add_case (((id,T,syn), name, args, prems), (opno,cases)) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   204
        if Syntax.is_identifier id
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   205
        then (opno,   
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   206
              (Free(case_name ^ "_" ^ id, T), args) :: cases)
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   207
        else (opno+1, 
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   208
              (Free(case_name ^ "_op_" ^ string_of_int opno, T), args) :: 
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   209
              cases)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   210
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   211
*   (*Treatment of a list of constructors, for one part*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   212
*   fun add_case_list (con_ty_list, (opno,case_lists)) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   213
        let val (opno',case_list) = foldr add_case (con_ty_list, (opno,[]))
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   214
        in (opno', case_list :: case_lists) end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   215
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   216
*   (*Treatment of all parts*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   217
*   val (_, case_lists) = foldr add_case_list (con_ty_lists, (1,[]));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   218
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   219
*   val big_case_typ = flat (map (map (#2 o #1)) con_ty_lists) ---> (iT-->iT);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   220
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   221
*   val big_rec_name = space_implode "_" rec_names;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   222
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   223
*   val big_case_name = big_rec_name ^ "_case";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   224
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   225
*   (*The list of all the function variables*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   226
*   val big_case_args = flat (map (map #1) case_lists);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   227
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   228
*   val big_case_tm = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   229
        list_comb (Const(big_case_name, big_case_typ), big_case_args); 
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   230
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   231
*   val big_case_def = mk_defpair  
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   232
        (big_case_tm, fold_bal (app sum_case) (map call_case case_lists)); 
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   233
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   234
*   (** Build the new theory **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   235
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   236
*   val const_decs =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   237
        (big_case_name, big_case_typ, NoSyn) :: map #1 (flat con_ty_lists);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   238
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   239
*   val axpairs =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1397
diff changeset
   240
        big_case_def :: flat (map mk_con_defs ((1 upto npart) ~~ con_ty_lists))
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   241
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   242
*   in  thy |> add_consts_i const_decs |> add_defs_i axpairs  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   243
****************************************************************)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   244
end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   245
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   246
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   247
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   248