src/ZF/ind-syntax.ML
author paulson
Fri, 16 Feb 1996 18:00:47 +0100
changeset 1512 ce37c64244c0
parent 70 8a29f8b4aca1
permissions -rw-r--r--
Elimination of fully-functorial style. Type tactic changed to a type abbrevation (from a datatype). Constructor tactic and function apply deleted.
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
(*Prove a goal stated as a term, with exception handling*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
fun prove_term sign defs (P,tacsf) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
    let val ct = Sign.cterm_of sign P
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
    in  prove_goalw_cterm defs ct tacsf
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
	handle e => (writeln ("Exception in proof of\n" ^
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
			       Sign.string_of_cterm ct); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
		     raise e)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
(*Read an assumption in the given theory*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
fun assume_read thy a = assume (Sign.read_cterm (sign_of thy) (a,propT));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
(*Make distinct individual variables a1, a2, a3, ..., an. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
fun mk_frees a [] = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
  | mk_frees a (T::Ts) = Free(a,T) :: mk_frees (bump_string a) Ts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
(*Used by intr-elim.ML and in individual datatype definitions*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
val basic_monos = [subset_refl, imp_refl, disj_mono, conj_mono, 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
		   ex_mono, Collect_mono, Part_mono, in_mono];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
14
1c0926788772 ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents: 6
diff changeset
   114
(*Return the conclusion of a rule, of the form t:X*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
fun rule_concl rl = 
14
1c0926788772 ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents: 6
diff changeset
   116
    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
   117
        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
   118
      | _ => error "Conclusion of rule should be a set membership";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
(*For deriving cases rules.  CollectD2 discards the domain, which is redundant;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
  read_instantiate replaces a propositional variable by a formula variable*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
val equals_CollectD = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
    read_instantiate [("W","?Q")]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
        (make_elim (equalityD1 RS subsetD RS CollectD2));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
(*From HOL/ex/meson.ML: raises exception if no rules apply -- unlike RL*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
  | tryres (th, []) = raise THM("tryres", 0, [th]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
fun gen_make_elim elim_rls rl = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
      standard (tryres (rl, elim_rls @ [revcut_rl]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
(** For constructor.ML **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
(*Avoids duplicate definitions by removing constants already declared mixfix*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
fun remove_mixfixes None decs = decs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
  | remove_mixfixes (Some sext) decs =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
      let val mixtab = Symtab.st_of_declist(Syntax.constants sext, Symtab.null)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
	  fun is_mix c = case Symtab.lookup(mixtab,c) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
			     None=>false | Some _ => true
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
      in  map (fn (cs,styp)=> (filter_out is_mix cs, styp)) decs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
      end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
fun ext_constants None        = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
  | ext_constants (Some sext) = Syntax.constants sext;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
(*Could go to FOL, but it's hardly general*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
val [def] = goal IFOL.thy "a==b ==> a=c <-> c=b";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
by (rewtac def);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
by (rtac iffI 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
by (REPEAT (etac sym 1));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
val def_swap_iff = result();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
val def_trans = prove_goal IFOL.thy "[| f==g;  g(a)=b |] ==> f(a)=b"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
  (fn [rew,prem] => [ rewtac rew, rtac prem 1 ]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
55
331d93292ee0 ZF/ind-syntax/refl_thin: new
lcp
parents: 14
diff changeset
   159
(*Delete needless equality assumptions*)
331d93292ee0 ZF/ind-syntax/refl_thin: new
lcp
parents: 14
diff changeset
   160
val refl_thin = prove_goal IFOL.thy "!!P. [| a=a;  P |] ==> P"
331d93292ee0 ZF/ind-syntax/refl_thin: new
lcp
parents: 14
diff changeset
   161
     (fn _ => [assume_tac 1]);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162