src/ZF/ind_syntax.ML
author clasohm
Mon Jul 11 13:15:05 1994 +0200 (1994-07-11 ago)
changeset 454 0d19ab250cc9
parent 444 3ca9d49fd662
child 466 08d1cce222e1
permissions -rw-r--r--
removed flatten_term and replaced add_axioms by add_axioms_i
clasohm@0
     1
(*  Title: 	ZF/ind-syntax.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Abstract Syntax functions for Inductive Definitions
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
clasohm@0
    10
(*SHOULD BE ABLE TO DELETE THESE!*)
clasohm@0
    11
fun flatten_typ sign T = 
clasohm@0
    12
    let val {syn,...} = Sign.rep_sg sign 
clasohm@0
    13
    in  Pretty.str_of (Syntax.pretty_typ syn T)
clasohm@0
    14
    end;
clasohm@0
    15
clasohm@0
    16
(*Make a definition, lhs==rhs, checking that vars on lhs contain *)
clasohm@454
    17
fun mk_defpair sign (lhs, rhs) = 
clasohm@454
    18
  let val Const(name, _) = head_of lhs
clasohm@0
    19
      val dummy = assert (term_vars rhs subset term_vars lhs
clasohm@454
    20
		          andalso
clasohm@454
    21
		          term_frees rhs subset term_frees lhs
clasohm@454
    22
		          andalso
clasohm@454
    23
		          term_tvars rhs subset term_tvars lhs
clasohm@454
    24
		          andalso
clasohm@454
    25
		          term_tfrees rhs subset term_tfrees lhs)
clasohm@0
    26
	          ("Extra variables on RHS in definition of " ^ name)
clasohm@454
    27
  in (name ^ "_def", Logic.mk_equals (lhs, rhs)) end;
clasohm@0
    28
clasohm@0
    29
fun lookup_const sign a = Symtab.lookup(#const_tab (Sign.rep_sg sign), a);
clasohm@0
    30
clasohm@0
    31
(*export to Pure/library?  *)
clasohm@0
    32
fun assert_all pred l msg_fn = 
clasohm@0
    33
  let fun asl [] = ()
clasohm@0
    34
	| asl (x::xs) = if pred x then asl xs
clasohm@0
    35
	                else error (msg_fn x)
clasohm@0
    36
  in  asl l  end;
clasohm@0
    37
clasohm@0
    38
clasohm@0
    39
(** Abstract syntax definitions for FOL and ZF **)
clasohm@0
    40
clasohm@0
    41
val iT = Type("i",[])
clasohm@0
    42
and oT = Type("o",[]);
clasohm@0
    43
clasohm@0
    44
fun ap t u = t$u;
clasohm@0
    45
fun app t (u1,u2) = t $ u1 $ u2;
clasohm@0
    46
clasohm@0
    47
(*Given u expecting arguments of types [T1,...,Tn], create term of 
clasohm@0
    48
  type T1*...*Tn => i using split*)
clasohm@0
    49
fun ap_split split u [ ]   = Abs("null", iT, u)
clasohm@0
    50
  | ap_split split u [_]   = u
clasohm@0
    51
  | ap_split split u [_,_] = split $ u
clasohm@0
    52
  | ap_split split u (T::Ts) = 
clasohm@0
    53
      split $ (Abs("v", T, ap_split split (u $ Bound(length Ts - 2)) Ts));
clasohm@0
    54
clasohm@0
    55
val conj = Const("op &", [oT,oT]--->oT)
clasohm@0
    56
and disj = Const("op |", [oT,oT]--->oT)
clasohm@0
    57
and imp = Const("op -->", [oT,oT]--->oT);
clasohm@0
    58
clasohm@0
    59
val eq_const = Const("op =", [iT,iT]--->oT);
clasohm@0
    60
clasohm@0
    61
val mem_const = Const("op :", [iT,iT]--->oT);
clasohm@0
    62
clasohm@0
    63
val exists_const = Const("Ex", [iT-->oT]--->oT);
clasohm@0
    64
fun mk_exists (Free(x,T),P) = exists_const $ (absfree (x,T,P));
clasohm@0
    65
clasohm@0
    66
val all_const = Const("All", [iT-->oT]--->oT);
clasohm@0
    67
fun mk_all (Free(x,T),P) = all_const $ (absfree (x,T,P));
clasohm@0
    68
clasohm@0
    69
(*Creates All(%v.v:A --> P(v)) rather than Ball(A,P) *)
clasohm@0
    70
fun mk_all_imp (A,P) = 
clasohm@0
    71
    all_const $ Abs("v", iT, imp $ (mem_const $ Bound 0 $ A) $ (P $ Bound 0));
clasohm@0
    72
clasohm@0
    73
clasohm@0
    74
val Part_const = Const("Part", [iT,iT-->iT]--->iT);
clasohm@0
    75
clasohm@0
    76
val Collect_const = Const("Collect", [iT,iT-->oT]--->iT);
clasohm@0
    77
fun mk_Collect (a,D,t) = Collect_const $ D $ absfree(a, iT, t);
clasohm@0
    78
clasohm@0
    79
val Trueprop = Const("Trueprop",oT-->propT);
clasohm@0
    80
fun mk_tprop P = Trueprop $ P;
clasohm@0
    81
clasohm@0
    82
(*Prove a goal stated as a term, with exception handling*)
clasohm@0
    83
fun prove_term sign defs (P,tacsf) = 
lcp@231
    84
    let val ct = cterm_of sign P
clasohm@0
    85
    in  prove_goalw_cterm defs ct tacsf
clasohm@0
    86
	handle e => (writeln ("Exception in proof of\n" ^
lcp@231
    87
			       string_of_cterm ct); 
clasohm@0
    88
		     raise e)
clasohm@0
    89
    end;
clasohm@0
    90
clasohm@0
    91
(*Read an assumption in the given theory*)
lcp@231
    92
fun assume_read thy a = assume (read_cterm (sign_of thy) (a,propT));
clasohm@0
    93
clasohm@0
    94
(*Make distinct individual variables a1, a2, a3, ..., an. *)
clasohm@0
    95
fun mk_frees a [] = []
clasohm@0
    96
  | mk_frees a (T::Ts) = Free(a,T) :: mk_frees (bump_string a) Ts;
clasohm@0
    97
clasohm@0
    98
(*Used by intr-elim.ML and in individual datatype definitions*)
clasohm@0
    99
val basic_monos = [subset_refl, imp_refl, disj_mono, conj_mono, 
clasohm@0
   100
		   ex_mono, Collect_mono, Part_mono, in_mono];
clasohm@0
   101
lcp@14
   102
(*Return the conclusion of a rule, of the form t:X*)
clasohm@0
   103
fun rule_concl rl = 
lcp@435
   104
    let val Const("Trueprop",_) $ (Const("op :",_) $ t $ X) = 
lcp@435
   105
		Logic.strip_imp_concl rl
lcp@435
   106
    in  (t,X)  end;
lcp@435
   107
lcp@435
   108
(*As above, but return error message if bad*)
lcp@435
   109
fun rule_concl_msg sign rl = rule_concl rl
lcp@435
   110
    handle Bind => error ("Ill-formed conclusion of introduction rule: " ^ 
lcp@435
   111
			  Sign.string_of_term sign rl);
clasohm@0
   112
clasohm@0
   113
(*For deriving cases rules.  CollectD2 discards the domain, which is redundant;
clasohm@0
   114
  read_instantiate replaces a propositional variable by a formula variable*)
clasohm@0
   115
val equals_CollectD = 
clasohm@0
   116
    read_instantiate [("W","?Q")]
clasohm@0
   117
        (make_elim (equalityD1 RS subsetD RS CollectD2));
clasohm@0
   118
clasohm@0
   119
clasohm@0
   120
(*From HOL/ex/meson.ML: raises exception if no rules apply -- unlike RL*)
clasohm@0
   121
fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
clasohm@0
   122
  | tryres (th, []) = raise THM("tryres", 0, [th]);
clasohm@0
   123
clasohm@0
   124
fun gen_make_elim elim_rls rl = 
clasohm@0
   125
      standard (tryres (rl, elim_rls @ [revcut_rl]));
clasohm@0
   126
clasohm@0
   127
(** For constructor.ML **)
clasohm@0
   128
clasohm@0
   129
(*Avoids duplicate definitions by removing constants already declared mixfix*)
clasohm@0
   130
fun remove_mixfixes None decs = decs
clasohm@0
   131
  | remove_mixfixes (Some sext) decs =
clasohm@0
   132
      let val mixtab = Symtab.st_of_declist(Syntax.constants sext, Symtab.null)
clasohm@0
   133
	  fun is_mix c = case Symtab.lookup(mixtab,c) of
clasohm@0
   134
			     None=>false | Some _ => true
clasohm@0
   135
      in  map (fn (cs,styp)=> (filter_out is_mix cs, styp)) decs
clasohm@0
   136
      end;
clasohm@0
   137
clasohm@0
   138
fun ext_constants None        = []
clasohm@0
   139
  | ext_constants (Some sext) = Syntax.constants sext;
clasohm@0
   140
clasohm@0
   141
clasohm@0
   142
(*Could go to FOL, but it's hardly general*)
clasohm@0
   143
val [def] = goal IFOL.thy "a==b ==> a=c <-> c=b";
clasohm@0
   144
by (rewtac def);
clasohm@0
   145
by (rtac iffI 1);
clasohm@0
   146
by (REPEAT (etac sym 1));
clasohm@0
   147
val def_swap_iff = result();
clasohm@0
   148
clasohm@0
   149
val def_trans = prove_goal IFOL.thy "[| f==g;  g(a)=b |] ==> f(a)=b"
clasohm@0
   150
  (fn [rew,prem] => [ rewtac rew, rtac prem 1 ]);
clasohm@0
   151
lcp@55
   152
(*Delete needless equality assumptions*)
lcp@55
   153
val refl_thin = prove_goal IFOL.thy "!!P. [| a=a;  P |] ==> P"
lcp@55
   154
     (fn _ => [assume_tac 1]);
clasohm@0
   155