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