src/HOL/add_ind_def.ML
author nipkow
Tue, 09 May 1995 22:10:08 +0200
changeset 1114 c8dfb56a7e95
parent 923 ff1574a81019
child 1189 c17a8fd0c95d
permissions -rw-r--r--
Prod is now a parent of Lfp. Added thm induct2 to Lfp. Changed the way patterns in abstractions are pretty printed. It has become simpler now but fails if split has more than one argument because then the ast-translation does not match.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     1
(*  Title: 	HOL/add_ind_def.ML
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    32
signature FP =		(** Description of a fixed point operator **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    33
  sig
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    34
  val oper	: string * typ * term -> term	(*fixed point operator*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    35
  val Tarski	: thm			(*Tarski's fixed point theorem*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    36
  val induct	: thm			(*induction/coinduction rule*)
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    50
open Logic Ind_Syntax;
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    57
    (*recT and rec_params should agree for all mutually recursive components*)
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    61
	    (fn t => "Recursive set not previously declared as constant: " ^ 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    62
	             Sign.string_of_term sign t);
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 =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    74
	  case head_of set of Const(a,_) => a mem rec_names | _ => false;
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    77
	 (fn t => "Conclusion of rule does not name a recursive set: " ^ 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    78
		  Sign.string_of_term sign t);
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    82
	(fn t => "Param in recursion term not a free variable: " ^
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    83
		 Sign.string_of_term sign t);
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    90
    (*Probably INCORRECT for mutual recursion!*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    91
    val domTs = summands(dest_setT (body_type recT));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    92
    val dom_sumT = fold_bal mk_sum domTs;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    93
    val dom_set   = mk_setT dom_sumT;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    94
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    95
    val freez   = Free(z, dom_sumT)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    96
    and freeX   = Free(X, dom_set);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    97
    (*type of w may be any of the domTs*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    98
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    99
    fun dest_tprop (Const("Trueprop",_) $ P) = P
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   100
      | dest_tprop Q = error ("Ill-formed premise of introduction rule: " ^ 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   101
			      Sign.string_of_term sign Q);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   102
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   103
    (*Makes a disjunct from an introduction rule*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   104
    fun lfp_part intr = (*quantify over rule's free vars except parameters*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   105
      let val prems = map dest_tprop (strip_imp_prems intr)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   106
	  val _ = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   107
	  val exfrees = term_frees intr \\ rec_params
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   108
	  val zeq = eq_const dom_sumT $ freez $ (#1 (rule_concl intr))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   109
      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
   110
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   111
    (*The Part(A,h) terms -- compose injections to make h*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   112
    fun mk_Part (Bound 0, _) = freeX	(*no mutual rec, no Part needed*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   113
      | mk_Part (h, domT)    = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   114
	  let val goodh = mend_sum_types (h, dom_sumT)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   115
              and Part_const = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   116
		  Const("Part", [dom_set, domT-->dom_sumT]---> dom_set)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   117
          in  Part_const $ freeX $ Abs(w,domT,goodh)  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   118
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   119
    (*Access to balanced disjoint sums via injections*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   120
    val parts = map mk_Part
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   121
	        (accesses_bal (ap Inl, ap Inr, Bound 0) (length domTs) ~~
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   122
		 domTs);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   123
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   124
    (*replace each set by the corresponding Part(A,h)*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   125
    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
   126
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   127
    val lfp_rhs = Fp.oper(X, dom_sumT, 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   128
			  mk_Collect(z, dom_sumT, 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   129
				     fold_bal (app disj) part_intrs))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   130
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   131
    val _ = seq (fn rec_hd => deny (rec_hd occs lfp_rhs) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   132
			       "Illegal occurrence of recursion operator")
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   133
	     rec_hds;
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
    (*** Make the new theory ***)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   136
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   137
    (*A key definition:
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   138
      If no mutual recursion then it equals the one recursive set.
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   139
      If mutual recursion then it differs from all the recursive sets. *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   140
    val big_rec_name = space_implode "_" rec_names;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   141
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   142
    (*Big_rec... is the union of the mutually recursive sets*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   143
    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
   144
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   145
    (*The individual sets must already be declared*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   146
    val axpairs = map mk_defpair 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   147
	  ((big_rec_tm, lfp_rhs) ::
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   148
	   (case parts of 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   149
	       [_] => [] 			(*no mutual recursion*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   150
	     | _ => rec_tms ~~		(*define the sets as Parts*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   151
		    map (subst_atomic [(freeX, big_rec_tm)]) parts));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   152
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   153
    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
   154
  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   155
  in  thy |> add_defs_i axpairs  end
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   156
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   157
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   158
(****************************************************************OMITTED
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   159
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   160
(*Expects the recursive sets to have been defined already.
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   161
  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
   162
fun add_constructs_def (rec_names, con_ty_lists) thy = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   163
* let
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   164
*   val _ = writeln"  Defining the constructor functions...";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   165
*   val case_name = "f";		(*name for case variables*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   166
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   167
*   (** Define the constructors **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   168
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   169
*   (*The empty tuple is 0*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   170
*   fun mk_tuple [] = Const("0",iT)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   171
*     | mk_tuple args = foldr1 mk_Pair args;
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
*   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
   174
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   175
*   val npart = length rec_names;	(*total # of mutually recursive parts*)
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
*   (*Make constructor definition; kpart is # of this mutually recursive part*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   178
*   fun mk_con_defs (kpart, con_ty_list) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   179
*     let val ncon = length con_ty_list	   (*number of constructors*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   180
	  fun mk_def (((id,T,syn), name, args, prems), kcon) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   181
		(*kcon is index of constructor*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   182
	      mk_defpair (list_comb (Const(name,T), args),
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   183
			  mk_inject npart kpart
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   184
			  (mk_inject ncon kcon (mk_tuple args)))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   185
*     in  map mk_def (con_ty_list ~~ (1 upto ncon))  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   186
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   187
*   (** Define the case operator **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   188
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   189
*   (*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
   190
*   fun call_case case_list = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   191
*     let fun call_f (free,args) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   192
	      ap_split T free (map (#2 o dest_Free) args)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   193
*     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
   194
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   195
*   (** Generating function variables for the case definition
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   196
	Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   197
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   198
*   (*Treatment of a single constructor*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   199
*   fun add_case (((id,T,syn), name, args, prems), (opno,cases)) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   200
	if Syntax.is_identifier id
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   201
	then (opno,   
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   202
	      (Free(case_name ^ "_" ^ id, T), args) :: cases)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   203
	else (opno+1, 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   204
	      (Free(case_name ^ "_op_" ^ string_of_int opno, T), args) :: 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   205
	      cases)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   206
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   207
*   (*Treatment of a list of constructors, for one part*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   208
*   fun add_case_list (con_ty_list, (opno,case_lists)) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   209
	let val (opno',case_list) = foldr add_case (con_ty_list, (opno,[]))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   210
	in (opno', case_list :: case_lists) end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   211
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   212
*   (*Treatment of all parts*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   213
*   val (_, case_lists) = foldr add_case_list (con_ty_lists, (1,[]));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   214
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   215
*   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
   216
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   217
*   val big_rec_name = space_implode "_" rec_names;
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_name = big_rec_name ^ "_case";
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
*   (*The list of all the function variables*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   222
*   val big_case_args = flat (map (map #1) case_lists);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   223
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   224
*   val big_case_tm = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   225
	list_comb (Const(big_case_name, big_case_typ), big_case_args); 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   226
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   227
*   val big_case_def = mk_defpair  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   228
	(big_case_tm, fold_bal (app sum_case) (map call_case case_lists)); 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   229
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   230
*   (** Build the new theory **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   231
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   232
*   val const_decs =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   233
	(big_case_name, big_case_typ, NoSyn) :: map #1 (flat con_ty_lists);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   234
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   235
*   val axpairs =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   236
	big_case_def :: flat (map mk_con_defs ((1 upto npart) ~~ con_ty_lists))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   237
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   238
*   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
   239
****************************************************************)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   240
end;
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
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