src/ZF/ind-syntax.ML
author lcp
Thu, 30 Sep 1993 10:10:21 +0100
changeset 14 1c0926788772
parent 6 8ce8c4d13d4d
child 55 331d93292ee0
permissions -rw-r--r--
ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext domrange/image_subset,vimage_subset: deleted needless premise! misc: This slightly simplifies two proofs in Schroeder-Bernstein Theorem ind-syntax/rule_concl: recoded to avoid exceptions intr-elim: now checks conclusions of introduction rules func/fun_disjoint_Un: now uses ex_ex1I list-fn/hd,tl,drop: new simpdata/bquant_simps: new list/list_case_type: restored! bool.thy: changed 1 from a "def" to a translation Removed occurreces of one_def in bool.ML, nat.ML, univ.ML, ex/integ.ML nat/succ_less_induct: new induction principle arith/add_mono: new results about monotonicity simpdata/mem_simps: removed the ones for succ and cons; added succI1, consI2 to ZF_ss upair/succ_iff: new, for use with simp_tac (cons_iff already existed) ordinal/Ord_0_in_succ: renamed from Ord_0_mem_succ nat/nat_0_in_succ: new ex/prop-log/hyps_thms_if: split up the fast_tac call for more speed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title: 	ZF/ind-syntax.ML
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
Abstract Syntax functions for Inductive Definitions
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
(*SHOULD BE ABLE TO DELETE THESE!*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
fun flatten_typ sign T = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
    let val {syn,...} = Sign.rep_sg sign 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
    in  Pretty.str_of (Syntax.pretty_typ syn T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
fun flatten_term sign t = Pretty.str_of (Sign.pretty_term sign t);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
(*Add constants to a theory*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
infix addconsts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
fun thy addconsts const_decs = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
    extend_theory thy (space_implode "_" (flat (map #1 const_decs)) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
		       ^ "_Theory")
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
		  ([], [], [], [], const_decs, None) [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
(*Make a definition, lhs==rhs, checking that vars on lhs contain *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
fun mk_defpair sign (lhs,rhs) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
  let val Const(name,_) = head_of lhs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
      val dummy = assert (term_vars rhs subset term_vars lhs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
		       andalso
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
		       term_frees rhs subset term_frees lhs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
		       andalso
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
		       term_tvars rhs subset term_tvars lhs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
		       andalso
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
		       term_tfrees rhs subset term_tfrees lhs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
	          ("Extra variables on RHS in definition of " ^ name)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
  in  (name ^ "_def",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
       flatten_term sign (Logic.mk_equals (lhs,rhs)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
fun lookup_const sign a = Symtab.lookup(#const_tab (Sign.rep_sg sign), a);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
(*export to Pure/library?  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
fun assert_all pred l msg_fn = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
  let fun asl [] = ()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
	| asl (x::xs) = if pred x then asl xs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
	                else error (msg_fn x)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
  in  asl l  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
(** Abstract syntax definitions for FOL and ZF **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
val iT = Type("i",[])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
and oT = Type("o",[]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
fun ap t u = t$u;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
fun app t (u1,u2) = t $ u1 $ u2;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
(*Given u expecting arguments of types [T1,...,Tn], create term of 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
  type T1*...*Tn => i using split*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
fun ap_split split u [ ]   = Abs("null", iT, u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
  | ap_split split u [_]   = u
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
  | ap_split split u [_,_] = split $ u
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
  | ap_split split u (T::Ts) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
      split $ (Abs("v", T, ap_split split (u $ Bound(length Ts - 2)) Ts));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
val conj = Const("op &", [oT,oT]--->oT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
and disj = Const("op |", [oT,oT]--->oT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
and imp = Const("op -->", [oT,oT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
val eq_const = Const("op =", [iT,iT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
val mem_const = Const("op :", [iT,iT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
val exists_const = Const("Ex", [iT-->oT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
fun mk_exists (Free(x,T),P) = exists_const $ (absfree (x,T,P));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
val all_const = Const("All", [iT-->oT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
fun mk_all (Free(x,T),P) = all_const $ (absfree (x,T,P));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
(*Creates All(%v.v:A --> P(v)) rather than Ball(A,P) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
fun mk_all_imp (A,P) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
    all_const $ Abs("v", iT, imp $ (mem_const $ Bound 0 $ A) $ (P $ Bound 0));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
val Part_const = Const("Part", [iT,iT-->iT]--->iT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
val Collect_const = Const("Collect", [iT,iT-->oT]--->iT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
fun mk_Collect (a,D,t) = Collect_const $ D $ absfree(a, iT, t);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
val Trueprop = Const("Trueprop",oT-->propT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
fun mk_tprop P = Trueprop $ P;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
fun dest_tprop (Const("Trueprop",_) $ P) = P;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
(*** Tactic for folding constructor definitions ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
(*The depth of injections in a constructor function*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
fun inject_depth (Const _ $ t) = 1 + inject_depth t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
  | inject_depth t             = 0;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
val rhs_of_thm = #2 o Logic.dest_equals o #prop o rep_thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
(*There are critical pairs!  E.g. K == Inl(0),  S == Inr(Inl(0))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
  Folds longest definitions first to avoid folding subexpressions of an rhs.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
fun fold_con_tac defs =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
  let val keylist = make_keylist (inject_depth o rhs_of_thm) defs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
      val keys = distinct (sort op> (map #2 keylist));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
      val deflists = map (keyfilter keylist) keys
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
  in  EVERY (map fold_tac deflists)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
(*Prove a goal stated as a term, with exception handling*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
fun prove_term sign defs (P,tacsf) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
    let val ct = Sign.cterm_of sign P
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
    in  prove_goalw_cterm defs ct tacsf
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
	handle e => (writeln ("Exception in proof of\n" ^
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
			       Sign.string_of_cterm ct); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
		     raise e)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
(*Read an assumption in the given theory*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
fun assume_read thy a = assume (Sign.read_cterm (sign_of thy) (a,propT));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
(*Make distinct individual variables a1, a2, a3, ..., an. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
fun mk_frees a [] = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
  | mk_frees a (T::Ts) = Free(a,T) :: mk_frees (bump_string a) Ts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
(*Used by intr-elim.ML and in individual datatype definitions*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
val basic_monos = [subset_refl, imp_refl, disj_mono, conj_mono, 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
		   ex_mono, Collect_mono, Part_mono, in_mono];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
14
1c0926788772 ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents: 6
diff changeset
   130
(*Return the conclusion of a rule, of the form t:X*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
fun rule_concl rl = 
14
1c0926788772 ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents: 6
diff changeset
   132
    case dest_tprop (Logic.strip_imp_concl rl) of
1c0926788772 ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents: 6
diff changeset
   133
        Const("op :",_) $ t $ X => (t,X) 
1c0926788772 ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents: 6
diff changeset
   134
      | _ => error "Conclusion of rule should be a set membership";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
(*For deriving cases rules.  CollectD2 discards the domain, which is redundant;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
  read_instantiate replaces a propositional variable by a formula variable*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
val equals_CollectD = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
    read_instantiate [("W","?Q")]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
        (make_elim (equalityD1 RS subsetD RS CollectD2));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
(*From HOL/ex/meson.ML: raises exception if no rules apply -- unlike RL*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
  | tryres (th, []) = raise THM("tryres", 0, [th]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
fun gen_make_elim elim_rls rl = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
      standard (tryres (rl, elim_rls @ [revcut_rl]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
(** For constructor.ML **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
(*Avoids duplicate definitions by removing constants already declared mixfix*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
fun remove_mixfixes None decs = decs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
  | remove_mixfixes (Some sext) decs =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
      let val mixtab = Symtab.st_of_declist(Syntax.constants sext, Symtab.null)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
	  fun is_mix c = case Symtab.lookup(mixtab,c) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
			     None=>false | Some _ => true
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
      in  map (fn (cs,styp)=> (filter_out is_mix cs, styp)) decs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
      end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
fun ext_constants None        = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
  | ext_constants (Some sext) = Syntax.constants sext;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
(*Could go to FOL, but it's hardly general*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
val [def] = goal IFOL.thy "a==b ==> a=c <-> c=b";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
by (rewtac def);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
by (rtac iffI 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
by (REPEAT (etac sym 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
val def_swap_iff = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
val def_trans = prove_goal IFOL.thy "[| f==g;  g(a)=b |] ==> f(a)=b"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
  (fn [rew,prem] => [ rewtac rew, rtac prem 1 ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175