src/ZF/ind_syntax.ML
author clasohm
Thu, 16 Sep 1993 12:20:38 +0200
changeset 0 a5a9c433f639
child 6 8ce8c4d13d4d
permissions -rw-r--r--
Initial revision
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
(*export to Pure/sign?  Used in Provers/simp.ML...*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
fun lookup_const sign a = Symtab.lookup(#const_tab (Sign.rep_sg sign), a);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
(*export to Pure/library?  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
fun assert_all pred l msg_fn = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
  let fun asl [] = ()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
	| asl (x::xs) = if pred x then asl xs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
	                else error (msg_fn x)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
  in  asl l  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
(** Abstract syntax definitions for FOL and ZF **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
val iT = Type("i",[])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
and oT = Type("o",[]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
fun ap t u = t$u;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
fun app t (u1,u2) = t $ u1 $ u2;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
(*Given u expecting arguments of types [T1,...,Tn], create term of 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
  type T1*...*Tn => i using split*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
fun ap_split split u [ ]   = Abs("null", iT, u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
  | ap_split split u [_]   = u
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
  | ap_split split u [_,_] = split $ u
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
  | ap_split split u (T::Ts) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
      split $ (Abs("v", T, ap_split split (u $ Bound(length Ts - 2)) Ts));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
val conj = Const("op &", [oT,oT]--->oT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
and disj = Const("op |", [oT,oT]--->oT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
and imp = Const("op -->", [oT,oT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
val eq_const = Const("op =", [iT,iT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
val mem_const = Const("op :", [iT,iT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
val exists_const = Const("Ex", [iT-->oT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
fun mk_exists (Free(x,T),P) = exists_const $ (absfree (x,T,P));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
val all_const = Const("All", [iT-->oT]--->oT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
fun mk_all (Free(x,T),P) = all_const $ (absfree (x,T,P));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
(*Creates All(%v.v:A --> P(v)) rather than Ball(A,P) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
fun mk_all_imp (A,P) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
    all_const $ Abs("v", iT, imp $ (mem_const $ Bound 0 $ A) $ (P $ Bound 0));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
val Part_const = Const("Part", [iT,iT-->iT]--->iT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
val Collect_const = Const("Collect", [iT,iT-->oT]--->iT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
fun mk_Collect (a,D,t) = Collect_const $ D $ absfree(a, iT, t);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
val Trueprop = Const("Trueprop",oT-->propT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
fun mk_tprop P = Trueprop $ P;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
fun dest_tprop (Const("Trueprop",_) $ P) = P;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
(*** Tactic for folding constructor definitions ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
(*The depth of injections in a constructor function*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
fun inject_depth (Const _ $ t) = 1 + inject_depth t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
  | inject_depth t             = 0;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
val rhs_of_thm = #2 o Logic.dest_equals o #prop o rep_thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
(*There are critical pairs!  E.g. K == Inl(0),  S == Inr(Inl(0))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
  Folds longest definitions first to avoid folding subexpressions of an rhs.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
fun fold_con_tac defs =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
  let val keylist = make_keylist (inject_depth o rhs_of_thm) defs;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
      val keys = distinct (sort op> (map #2 keylist));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
      val deflists = map (keyfilter keylist) keys
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
  in  EVERY (map fold_tac deflists)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
(*Prove a goal stated as a term, with exception handling*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
fun prove_term sign defs (P,tacsf) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
    let val ct = Sign.cterm_of sign P
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
    in  prove_goalw_cterm defs ct tacsf
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
	handle e => (writeln ("Exception in proof of\n" ^
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
			       Sign.string_of_cterm ct); 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
		     raise e)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
(*Read an assumption in the given theory*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
fun assume_read thy a = assume (Sign.read_cterm (sign_of thy) (a,propT));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
(*Make distinct individual variables a1, a2, a3, ..., an. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
fun mk_frees a [] = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
  | mk_frees a (T::Ts) = Free(a,T) :: mk_frees (bump_string a) Ts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
(*Used by intr-elim.ML and in individual datatype definitions*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
val basic_monos = [subset_refl, imp_refl, disj_mono, conj_mono, 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
		   ex_mono, Collect_mono, Part_mono, in_mono];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
fun rule_concl rl = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
  let val Const("op :",_) $ t $ X = dest_tprop (Logic.strip_imp_concl rl)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
  in (t,X) end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
  handle _ => error "Conclusion of rule should be a set membership";
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