src/ZF/add_ind_def.ML
author lcp
Fri, 25 Nov 1994 11:08:12 +0100
changeset 750 019aadf0e315
parent 727 711e4eb8c213
child 1109 380e9eb40db7
permissions -rw-r--r--
checks that the recursive sets are Consts before taking them apart! Bug was introduced during the translation to theory sections.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
516
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     1
(*  Title: 	ZF/add_ind_def.ML
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     2
    ID:         $Id$
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     4
    Copyright   1994  University of Cambridge
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     5
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     6
Fixedpoint definition module -- for Inductive/Coinductive Definitions
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     7
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     8
Features:
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
     9
* least or greatest fixedpoints
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    10
* user-specified product and sum constructions
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    11
* mutually recursive definitions
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    12
* definitions involving arbitrary monotone operators
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    13
* automatically proves introduction and elimination rules
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    14
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    15
The recursive sets must *already* be declared as constants in parent theory!
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    16
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    17
  Introduction rules have the form
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    18
  [| ti:M(Sj), ..., P(x), ... |] ==> t: Sk |]
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    19
  where M is some monotone operator (usually the identity)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    20
  P(x) is any (non-conjunctive) side condition on the free variables
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    21
  ti, t are any terms
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    22
  Sj, Sk are two of the sets being defined in mutual recursion
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    23
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    24
Sums are used only for mutual recursion;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    25
Products are used only to derive "streamlined" induction rules for relations
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    26
*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    27
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    28
signature FP =		(** Description of a fixed point operator **)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    29
  sig
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    30
  val oper	: term			(*fixed point operator*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    31
  val bnd_mono	: term			(*monotonicity predicate*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    32
  val bnd_monoI	: thm			(*intro rule for bnd_mono*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    33
  val subs	: thm			(*subset theorem for fp*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    34
  val Tarski	: thm			(*Tarski's fixed point theorem*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    35
  val induct	: thm			(*induction/coinduction rule*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    36
  end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    37
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    38
signature PR =			(** Description of a Cartesian product **)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    39
  sig
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    40
  val sigma	: term			(*Cartesian product operator*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    41
  val pair	: term			(*pairing operator*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    42
  val split_const  : term		(*splitting operator*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    43
  val fsplit_const : term		(*splitting operator for formulae*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    44
  val pair_iff	: thm			(*injectivity of pairing, using <->*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    45
  val split_eq	: thm			(*equality rule for split*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    46
  val fsplitI	: thm			(*intro rule for fsplit*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    47
  val fsplitD	: thm			(*destruct rule for fsplit*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    48
  val fsplitE	: thm			(*elim rule for fsplit*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    49
  end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    50
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    51
signature SU =			(** Description of a disjoint sum **)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    52
  sig
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    53
  val sum	: term			(*disjoint sum operator*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    54
  val inl	: term			(*left injection*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    55
  val inr	: term			(*right injection*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    56
  val elim	: term			(*case operator*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    57
  val case_inl	: thm			(*inl equality rule for case*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    58
  val case_inr	: thm			(*inr equality rule for case*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    59
  val inl_iff	: thm			(*injectivity of inl, using <->*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    60
  val inr_iff	: thm			(*injectivity of inr, using <->*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    61
  val distinct	: thm			(*distinctness of inl, inr using <->*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    62
  val distinct'	: thm			(*distinctness of inr, inl using <->*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    63
  end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    64
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    65
signature ADD_INDUCTIVE_DEF =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    66
  sig 
727
711e4eb8c213 ZF INDUCTIVE DEFINITIONS: Simplifying the type checking for mutually
lcp
parents: 612
diff changeset
    67
  val add_fp_def_i : term list * term * term list -> theory -> theory
516
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    68
  val add_constructs_def :
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    69
        string list * ((string*typ*mixfix) * 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    70
                       string * term list * term list) list list ->
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    71
        theory -> theory
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    72
  end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    73
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    74
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    75
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    76
(*Declares functions to add fixedpoint/constructor defs to a theory*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    77
functor Add_inductive_def_Fun 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    78
    (structure Fp: FP and Pr : PR and Su : SU) : ADD_INDUCTIVE_DEF =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    79
struct
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    80
open Logic Ind_Syntax;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    81
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    82
(*internal version*)
727
711e4eb8c213 ZF INDUCTIVE DEFINITIONS: Simplifying the type checking for mutually
lcp
parents: 612
diff changeset
    83
fun add_fp_def_i (rec_tms, dom_sum, intr_tms) thy = 
516
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    84
  let
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    85
    val sign = sign_of thy;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    86
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    87
    (*recT and rec_params should agree for all mutually recursive components*)
750
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    88
    val rec_hds = map head_of rec_tms;
516
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    89
750
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    90
    val _ = assert_all is_Const rec_hds
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    91
	    (fn t => "Recursive set not previously declared as constant: " ^ 
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    92
	             Sign.string_of_term sign t);
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    93
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    94
    (*Now we know they are all Consts, so get their names, type and params*)
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    95
    val rec_names = map (#1 o dest_Const) rec_hds
019aadf0e315 checks that the recursive sets are Consts before taking
lcp
parents: 727
diff changeset
    96
    and (Const(_,recT),rec_params) = strip_comb (hd rec_tms);
516
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    97
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    98
    val _ = assert_all Syntax.is_identifier rec_names
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
    99
       (fn a => "Name of recursive set not an identifier: " ^ a);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   100
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   101
    local (*Checking the introduction rules*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   102
      val intr_sets = map (#2 o rule_concl_msg sign) intr_tms;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   103
      fun intr_ok set =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   104
	  case head_of set of Const(a,recT) => a mem rec_names | _ => false;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   105
    in
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   106
      val _ =  assert_all intr_ok intr_sets
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   107
	 (fn t => "Conclusion of rule does not name a recursive set: " ^ 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   108
		  Sign.string_of_term sign t);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   109
    end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   110
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   111
    val _ = assert_all is_Free rec_params
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   112
	(fn t => "Param in recursion term not a free variable: " ^
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   113
		 Sign.string_of_term sign t);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   114
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   115
    (*** Construct the lfp definition ***)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   116
    val mk_variant = variant (foldr add_term_names (intr_tms,[]));
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   117
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   118
    val z' = mk_variant"z" and X' = mk_variant"X" and w' = mk_variant"w";
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   119
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   120
    fun dest_tprop (Const("Trueprop",_) $ P) = P
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   121
      | dest_tprop Q = error ("Ill-formed premise of introduction rule: " ^ 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   122
			      Sign.string_of_term sign Q);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   123
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   124
    (*Makes a disjunct from an introduction rule*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   125
    fun lfp_part intr = (*quantify over rule's free vars except parameters*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   126
      let val prems = map dest_tprop (strip_imp_prems intr)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   127
	  val _ = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   128
	  val exfrees = term_frees intr \\ rec_params
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   129
	  val zeq = eq_const $ (Free(z',iT)) $ (#1 (rule_concl intr))
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   130
      in foldr mk_exists (exfrees, fold_bal (app conj) (zeq::prems)) end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   131
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   132
    (*The Part(A,h) terms -- compose injections to make h*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   133
    fun mk_Part (Bound 0) = Free(X',iT)	(*no mutual rec, no Part needed*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   134
      | mk_Part h         = Part_const $ Free(X',iT) $ Abs(w',iT,h);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   135
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   136
    (*Access to balanced disjoint sums via injections*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   137
    val parts = 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   138
	map mk_Part (accesses_bal (ap Su.inl, ap Su.inr, Bound 0) 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   139
				  (length rec_tms));
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   140
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   141
    (*replace each set by the corresponding Part(A,h)*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   142
    val part_intrs = map (subst_free (rec_tms ~~ parts) o lfp_part) intr_tms;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   143
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   144
    val lfp_abs = absfree(X', iT, 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   145
		     mk_Collect(z', dom_sum, fold_bal (app disj) part_intrs));
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   146
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   147
    val lfp_rhs = Fp.oper $ dom_sum $ lfp_abs
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   148
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   149
    val _ = seq (fn rec_hd => deny (rec_hd occs lfp_rhs) 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   150
			       "Illegal occurrence of recursion operator")
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   151
	     rec_hds;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   152
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   153
    (*** Make the new theory ***)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   154
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   155
    (*A key definition:
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   156
      If no mutual recursion then it equals the one recursive set.
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   157
      If mutual recursion then it differs from all the recursive sets. *)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   158
    val big_rec_name = space_implode "_" rec_names;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   159
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   160
    (*Big_rec... is the union of the mutually recursive sets*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   161
    val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   162
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   163
    (*The individual sets must already be declared*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   164
    val axpairs = map mk_defpair 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   165
	  ((big_rec_tm, lfp_rhs) ::
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   166
	   (case parts of 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   167
	       [_] => [] 			(*no mutual recursion*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   168
	     | _ => rec_tms ~~		(*define the sets as Parts*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   169
		    map (subst_atomic [(Free(X',iT),big_rec_tm)]) parts));
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   170
591
5a6b0ed377cb ZF/add_ind_def/add_fp_def_i: now prints the fixedpoint defs at the terminal
lcp
parents: 567
diff changeset
   171
    val _ = seq (writeln o Sign.string_of_term sign o #2) axpairs
5a6b0ed377cb ZF/add_ind_def/add_fp_def_i: now prints the fixedpoint defs at the terminal
lcp
parents: 567
diff changeset
   172
  
567
01c043f61077 replaced add_defns_i by add_defs_i;
wenzelm
parents: 516
diff changeset
   173
  in  thy |> add_defs_i axpairs  end
516
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   174
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   175
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   176
(*Expects the recursive sets to have been defined already.
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   177
  con_ty_lists specifies the constructors in the form (name,prems,mixfix) *)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   178
fun add_constructs_def (rec_names, con_ty_lists) thy = 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   179
  let
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   180
    val _ = writeln"  Defining the constructor functions...";
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   181
    val case_name = "f";		(*name for case variables*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   182
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   183
    (** Define the constructors **)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   184
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   185
    (*The empty tuple is 0*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   186
    fun mk_tuple [] = Const("0",iT)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   187
      | mk_tuple args = foldr1 (app Pr.pair) args;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   188
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   189
    fun mk_inject n k u = access_bal(ap Su.inl, ap Su.inr, u) n k;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   190
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   191
    val npart = length rec_names;	(*total # of mutually recursive parts*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   192
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   193
    (*Make constructor definition; kpart is # of this mutually recursive part*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   194
    fun mk_con_defs (kpart, con_ty_list) = 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   195
      let val ncon = length con_ty_list	   (*number of constructors*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   196
	  fun mk_def (((id,T,syn), name, args, prems), kcon) =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   197
		(*kcon is index of constructor*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   198
	      mk_defpair (list_comb (Const(name,T), args),
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   199
			  mk_inject npart kpart
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   200
			  (mk_inject ncon kcon (mk_tuple args)))
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   201
      in  map mk_def (con_ty_list ~~ (1 upto ncon))  end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   202
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   203
    (** Define the case operator **)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   204
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   205
    (*Combine split terms using case; yields the case operator for one part*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   206
    fun call_case case_list = 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   207
      let fun call_f (free,args) = 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   208
	      ap_split Pr.split_const free (map (#2 o dest_Free) args)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   209
      in  fold_bal (app Su.elim) (map call_f case_list)  end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   210
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   211
    (** Generating function variables for the case definition
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   212
	Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   213
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   214
    (*Treatment of a single constructor*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   215
    fun add_case (((id,T,syn), name, args, prems), (opno,cases)) =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   216
	if Syntax.is_identifier id
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   217
	then (opno,   
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   218
	      (Free(case_name ^ "_" ^ id, T), args) :: cases)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   219
	else (opno+1, 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   220
	      (Free(case_name ^ "_op_" ^ string_of_int opno, T), args) :: 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   221
	      cases)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   222
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   223
    (*Treatment of a list of constructors, for one part*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   224
    fun add_case_list (con_ty_list, (opno,case_lists)) =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   225
	let val (opno',case_list) = foldr add_case (con_ty_list, (opno,[]))
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   226
	in (opno', case_list :: case_lists) end;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   227
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   228
    (*Treatment of all parts*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   229
    val (_, case_lists) = foldr add_case_list (con_ty_lists, (1,[]));
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   230
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   231
    val big_case_typ = flat (map (map (#2 o #1)) con_ty_lists) ---> (iT-->iT);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   232
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   233
    val big_rec_name = space_implode "_" rec_names;
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   234
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   235
    val big_case_name = big_rec_name ^ "_case";
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   236
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   237
    (*The list of all the function variables*)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   238
    val big_case_args = flat (map (map #1) case_lists);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   239
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   240
    val big_case_tm = 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   241
	list_comb (Const(big_case_name, big_case_typ), big_case_args); 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   242
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   243
    val big_case_def = mk_defpair  
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   244
	(big_case_tm, fold_bal (app Su.elim) (map call_case case_lists)); 
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   245
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   246
    (** Build the new theory **)
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   247
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   248
    val const_decs =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   249
	(big_case_name, big_case_typ, NoSyn) :: map #1 (flat con_ty_lists);
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   250
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   251
    val axpairs =
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   252
	big_case_def :: flat (map mk_con_defs ((1 upto npart) ~~ con_ty_lists))
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   253
567
01c043f61077 replaced add_defns_i by add_defs_i;
wenzelm
parents: 516
diff changeset
   254
    in  thy |> add_consts_i const_decs |> add_defs_i axpairs  end;
516
1957113f0d7d installation of new inductive/datatype sections
lcp
parents:
diff changeset
   255
end;