src/Pure/drule.ML
author lcp
Fri, 24 Sep 1993 10:52:55 +0200
changeset 11 d0e17c42dbb4
parent 0 a5a9c433f639
child 67 8380bc0adde7
permissions -rw-r--r--
Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a rule's premises against a list of other proofs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title: 	drule
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
Derived rules and other operations on theorems and theories
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
11
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
     9
infix 0 RS RSN RL RLN MRS MRL COMP;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
signature DRULE =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
  sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
  structure Thm : THM
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
  local open Thm  in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
  val asm_rl: thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
  val assume_ax: theory -> string -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
  val COMP: thm * thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  val compose: thm * int * thm -> thm list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  val cterm_instantiate: (Sign.cterm*Sign.cterm)list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
  val cut_rl: thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  val equal_abs_elim: Sign.cterm  -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  val equal_abs_elim_list: Sign.cterm list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
  val eq_sg: Sign.sg * Sign.sg -> bool
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
  val eq_thm: thm * thm -> bool
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
  val eq_thm_sg: thm * thm -> bool
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
  val flexpair_abs_elim_list: Sign.cterm list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
  val forall_intr_list: Sign.cterm list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
  val forall_intr_frees: thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
  val forall_elim_list: Sign.cterm list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
  val forall_elim_var: int -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
  val forall_elim_vars: int -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
  val implies_elim_list: thm -> thm list -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
  val implies_intr_list: Sign.cterm list -> thm -> thm
11
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
    34
  val MRL: thm list list * thm list -> thm list
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
    35
  val MRS: thm list * thm -> thm
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
  val print_cterm: Sign.cterm -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
  val print_ctyp: Sign.ctyp -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
  val print_goals: int -> thm -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
  val print_sg: Sign.sg -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
  val print_theory: theory -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
  val pprint_sg: Sign.sg -> pprint_args -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
  val pprint_theory: theory -> pprint_args -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
  val print_thm: thm -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
  val prth: thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
  val prthq: thm Sequence.seq -> thm Sequence.seq
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
  val prths: thm list -> thm list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
  val read_instantiate: (string*string)list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
  val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
  val reflexive_thm: thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
  val revcut_rl: thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
  val rewrite_goal_rule: (meta_simpset -> thm -> thm option) -> meta_simpset ->
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
        int -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
  val rewrite_goals_rule: thm list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
  val rewrite_rule: thm list -> thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
  val RS: thm * thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
  val RSN: thm * (int * thm) -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
  val RL: thm list * thm list -> thm list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
  val RLN: thm list * (int * thm list) -> thm list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
  val show_hyps: bool ref
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
  val size_of_thm: thm -> int
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
  val standard: thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
  val string_of_thm: thm -> string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
  val symmetric_thm: thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
  val pprint_thm: thm -> pprint_args -> unit
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
  val transitive_thm: thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
  val triv_forall_equality: thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
  val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
  val zero_var_indexes: thm -> thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
functor DruleFun (structure Logic: LOGIC and Thm: THM) : DRULE = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
structure Thm = Thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
structure Sign = Thm.Sign;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
structure Type = Sign.Type;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
structure Pretty = Sign.Syntax.Pretty
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
local open Thm
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
(**** More derived rules and operations on theorems ****)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
     Used for establishing default types (of variables) and sorts (of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
     type variables) when reading another term.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
fun types_sorts thm =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
    let val {prop,hyps,...} = rep_thm thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
	val big = list_comb(prop,hyps); (* bogus term! *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
	val vars = map dest_Var (term_vars big);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
	val frees = map dest_Free (term_frees big);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
	val tvars = term_tvars big;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
	val tfrees = term_tfrees big;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
	fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
	fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
    in (typ,sort) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
(** Standardization of rules **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
(*Generalization over a list of variables, IGNORING bad ones*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
fun forall_intr_list [] th = th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
  | forall_intr_list (y::ys) th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
	let val gth = forall_intr_list ys th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
	in  forall_intr y gth   handle THM _ =>  gth  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
(*Generalization over all suitable Free variables*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
fun forall_intr_frees th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
    let val {prop,sign,...} = rep_thm th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
    in  forall_intr_list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
         (map (Sign.cterm_of sign) (sort atless (term_frees prop))) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
         th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
(*Replace outermost quantified variable by Var of given index.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
    Could clash with Vars already present.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
fun forall_elim_var i th = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
    let val {prop,sign,...} = rep_thm th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
    in case prop of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
	  Const("all",_) $ Abs(a,T,_) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
	      forall_elim (Sign.cterm_of sign (Var((a,i), T)))  th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
	| _ => raise THM("forall_elim_var", i, [th])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
(*Repeat forall_elim_var until all outer quantifiers are removed*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
fun forall_elim_vars i th = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
    forall_elim_vars i (forall_elim_var i th)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
	handle THM _ => th;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
(*Specialization over a list of cterms*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
(* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
(* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
(*Reset Var indexes to zero, renaming to preserve distinctness*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
fun zero_var_indexes th = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
    let val {prop,sign,...} = rep_thm th;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
        val vars = term_vars prop
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
        val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
	val inrs = add_term_tvars(prop,[]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
	val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
	val tye = map (fn ((v,rs),a) => (v, TVar((a,0),rs))) (inrs ~~ nms')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
	val ctye = map (fn (v,T) => (v,Sign.ctyp_of sign T)) tye;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
	fun varpairs([],[]) = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
	  | varpairs((var as Var(v,T)) :: vars, b::bs) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
		let val T' = typ_subst_TVars tye T
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
		in (Sign.cterm_of sign (Var(v,T')),
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
		    Sign.cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
		end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
	  | varpairs _ = raise TERM("varpairs", []);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
    in instantiate (ctye, varpairs(vars,rev bs)) th end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
(*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
    all generality expressed by Vars having index 0.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
fun standard th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
    let val {maxidx,...} = rep_thm th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
    in  varifyT (zero_var_indexes (forall_elim_vars(maxidx+1) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
                         (forall_intr_frees(implies_intr_hyps th))))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
(*Assume a new formula, read following the same conventions as axioms. 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
  Generalizes over Free variables,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
  creates the assumption, and then strips quantifiers.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
  Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
	     [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
fun assume_ax thy sP =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
    let val sign = sign_of thy
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
	val prop = Logic.close_form (Sign.term_of (Sign.read_cterm sign
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
			 (sP, propT)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
    in forall_elim_vars 0 (assume (Sign.cterm_of sign prop))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
(*Resolution: exactly one resolvent must be produced.*) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
fun tha RSN (i,thb) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
  case Sequence.chop (2, biresolution false [(false,tha)] i thb) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
      ([th],_) => th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
    | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   183
    |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
(*resolution: P==>Q, Q==>R gives P==>R. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
fun tha RS thb = tha RSN (1,thb);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
(*For joining lists of rules*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
fun thas RLN (i,thbs) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
  let val resolve = biresolution false (map (pair false) thas) i
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
      fun resb thb = Sequence.list_of_s (resolve thb) handle THM _ => []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
  in  flat (map resb thbs)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
fun thas RL thbs = thas RLN (1,thbs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
11
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   196
(*Resolve a list of rules against bottom_rl from right to left;
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   197
  makes proof trees*)
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   198
fun rls MRS bottom_rl = 
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   199
  let fun rs_aux i [] = bottom_rl
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   200
	| rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   201
  in  rs_aux 1 rls  end;
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   202
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   203
(*As above, but for rule lists*)
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   204
fun rlss MRL bottom_rls = 
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   205
  let fun rs_aux i [] = bottom_rls
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   206
	| rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   207
  in  rs_aux 1 rlss  end;
d0e17c42dbb4 Added MRS, MRL from ZF/ROOT.ML. These support forward proof, resolving a
lcp
parents: 0
diff changeset
   208
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
  with no lifting or renaming!  Q may contain ==> or meta-quants
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
  ALWAYS deletes premise i *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
fun compose(tha,i,thb) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
    Sequence.list_of_s (bicompose false (false,tha,0) i thb);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
fun tha COMP thb =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
    case compose(tha,1,thb) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
        [th] => th  
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
      | _ =>   raise THM("COMP", 1, [tha,thb]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
(*Instantiate theorem th, reading instantiations under signature sg*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
fun read_instantiate_sg sg sinsts th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
    let val ts = types_sorts th;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
        val instpair = Sign.read_insts sg ts ts sinsts
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
    in  instantiate instpair th  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
(*Instantiate theorem th, reading instantiations under theory of th*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
fun read_instantiate sinsts th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
    read_instantiate_sg (#sign (rep_thm th)) sinsts th;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
(*Left-to-right replacements: tpairs = [...,(vi,ti),...].
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
  Instantiates distinct Vars by terms, inferring type instantiations. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
local
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
  fun add_types ((ct,cu), (sign,tye)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
    let val {sign=signt, t=t, T= T, ...} = Sign.rep_cterm ct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
        and {sign=signu, t=u, T= U, ...} = Sign.rep_cterm cu
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
        val sign' = Sign.merge(sign, Sign.merge(signt, signu))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
	val tye' = Type.unify (#tsig(Sign.rep_sg sign')) ((T,U), tye)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
	  handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
    in  (sign', tye')  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
fun cterm_instantiate ctpairs0 th = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
  let val (sign,tye) = foldr add_types (ctpairs0, (#sign(rep_thm th),[]))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
      val tsig = #tsig(Sign.rep_sg sign);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
      fun instT(ct,cu) = let val inst = subst_TVars tye
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
			 in (Sign.cfun inst ct, Sign.cfun inst cu) end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
      fun ctyp2 (ix,T) = (ix, Sign.ctyp_of sign T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
  in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
  handle TERM _ => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
           raise THM("cterm_instantiate: incompatible signatures",0,[th])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
       | TYPE _ => raise THM("cterm_instantiate: types", 0, [th])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
(*** Printing of theorems ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
(*If false, hypotheses are printed as dots*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
val show_hyps = ref true;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
fun pretty_thm th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   262
let val {sign, hyps, prop,...} = rep_thm th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
    val hsymbs = if null hyps then []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
		 else if !show_hyps then
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
		      [Pretty.brk 2,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
		       Pretty.lst("[","]") (map (Sign.pretty_term sign) hyps)]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   267
		 else Pretty.str" [" :: map (fn _ => Pretty.str".") hyps @
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
		      [Pretty.str"]"];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
in Pretty.blk(0, Sign.pretty_term sign prop :: hsymbs) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   271
val string_of_thm = Pretty.string_of o pretty_thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
(** Top-level commands for printing theorems **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
val print_thm = writeln o string_of_thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
fun prth th = (print_thm th; th);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
(*Print and return a sequence of theorems, separated by blank lines. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
fun prthq thseq =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   283
    (Sequence.prints (fn _ => print_thm) 100000 thseq;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   284
     thseq);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
(*Print and return a list of theorems, separated by blank lines. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
fun prths ths = (print_list_ln print_thm ths; ths);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   289
(*Other printing commands*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
val print_cterm = writeln o Sign.string_of_cterm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   291
val print_ctyp = writeln o Sign.string_of_ctyp;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
fun pretty_sg sg = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
  Pretty.lst ("{", "}") (map (Pretty.str o !) (#stamps (Sign.rep_sg sg)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   295
val pprint_sg = Pretty.pprint o pretty_sg;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   297
val pprint_theory = pprint_sg o sign_of;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   299
val print_sg = writeln o Pretty.string_of o pretty_sg;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
val print_theory = print_sg o sign_of;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   301
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   302
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   303
(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   304
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   305
fun prettyprints es = writeln(Pretty.string_of(Pretty.blk(0,es)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   306
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   307
fun print_goals maxgoals th : unit =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   308
let val {sign, hyps, prop,...} = rep_thm th;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   309
    fun printgoals (_, []) = ()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   310
      | printgoals (n, A::As) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   311
	let val prettyn = Pretty.str(" " ^ string_of_int n ^ ". ");
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   312
	    val prettyA = Sign.pretty_term sign A
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   313
	in prettyprints[prettyn,prettyA]; 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   314
           printgoals (n+1,As) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   315
        end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   316
    fun prettypair(t,u) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   317
        Pretty.blk(0, [Sign.pretty_term sign t, Pretty.str" =?=", Pretty.brk 1,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   318
		       Sign.pretty_term sign u]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   319
    fun printff [] = ()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   320
      | printff tpairs =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   321
	 writeln("\nFlex-flex pairs:\n" ^
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   322
		 Pretty.string_of(Pretty.lst("","") (map prettypair tpairs)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   323
    val (tpairs,As,B) = Logic.strip_horn(prop);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   324
    val ngoals = length As
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   325
in 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   326
   writeln (Sign.string_of_term sign B);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   327
   if ngoals=0 then writeln"No subgoals!"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
   else if ngoals>maxgoals 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   329
        then (printgoals (1, take(maxgoals,As));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   330
	      writeln("A total of " ^ string_of_int ngoals ^ " subgoals..."))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
        else printgoals (1, As);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   332
   printff tpairs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   333
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   334
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   335
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   336
(** theorem equality test is exported and used by BEST_FIRST **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   337
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   338
(*equality of signatures means exact identity -- by ref equality*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   339
fun eq_sg (sg1,sg2) = (#stamps(Sign.rep_sg sg1) = #stamps(Sign.rep_sg sg2));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   340
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   341
(*equality of theorems uses equality of signatures and 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   342
  the a-convertible test for terms*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   343
fun eq_thm (th1,th2) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   344
    let val {sign=sg1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   345
	and {sign=sg2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   346
    in  eq_sg (sg1,sg2) andalso 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   347
        aconvs(hyps1,hyps2) andalso 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   348
        prop1 aconv prop2  
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   349
    end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   350
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   351
(*Do the two theorems have the same signature?*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   352
fun eq_thm_sg (th1,th2) = eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   353
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   354
(*Useful "distance" function for BEST_FIRST*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   355
val size_of_thm = size_of_term o #prop o rep_thm;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   356
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   357
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   358
(*** Meta-Rewriting Rules ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   359
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   360
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   361
val reflexive_thm =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   362
  let val cx = Sign.cterm_of Sign.pure (Var(("x",0),TVar(("'a",0),["logic"])))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   363
  in Thm.reflexive cx end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   364
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   365
val symmetric_thm =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   366
  let val xy = Sign.read_cterm Sign.pure ("x::'a::logic == y",propT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   367
  in standard(Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy))) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   368
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   369
val transitive_thm =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   370
  let val xy = Sign.read_cterm Sign.pure ("x::'a::logic == y",propT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   371
      val yz = Sign.read_cterm Sign.pure ("y::'a::logic == z",propT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   372
      val xythm = Thm.assume xy and yzthm = Thm.assume yz
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   373
  in standard(Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   374
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   375
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   376
(** Below, a "conversion" has type sign->term->thm **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   377
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   378
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   379
fun goals_conv pred cv sign = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   380
  let val triv = reflexive o Sign.cterm_of sign
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   381
      fun gconv i t =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   382
        let val (A,B) = Logic.dest_implies t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   383
	    val thA = if (pred i) then (cv sign A) else (triv A)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   384
	in  combination (combination (triv implies) thA)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   385
                        (gconv (i+1) B)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   386
        end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   387
        handle TERM _ => triv t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   388
  in gconv 1 end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   389
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   390
(*Use a conversion to transform a theorem*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   391
fun fconv_rule cv th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   392
  let val {sign,prop,...} = rep_thm th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   393
  in  equal_elim (cv sign prop) th  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   394
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   395
(*rewriting conversion*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   396
fun rew_conv prover mss sign t =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   397
  rewrite_cterm mss prover (Sign.cterm_of sign t);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   398
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   399
(*Rewrite a theorem*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   400
fun rewrite_rule thms = fconv_rule (rew_conv (K(K None)) (Thm.mss_of thms));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   401
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   402
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   403
fun rewrite_goals_rule thms =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   404
  fconv_rule (goals_conv (K true) (rew_conv (K(K None)) (Thm.mss_of thms)));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   405
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   406
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   407
fun rewrite_goal_rule prover mss i =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   408
      fconv_rule (goals_conv (fn j => j=i) (rew_conv prover mss));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   409
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   410
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   411
(** Derived rules mainly for METAHYPS **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   412
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   413
(*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   414
fun equal_abs_elim ca eqth =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   415
  let val {sign=signa, t=a, ...} = Sign.rep_cterm ca
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   416
      and combth = combination eqth (reflexive ca)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   417
      val {sign,prop,...} = rep_thm eqth
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   418
      val (abst,absu) = Logic.dest_equals prop
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   419
      val cterm = Sign.cterm_of (Sign.merge (sign,signa))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   420
  in  transitive (symmetric (beta_conversion (cterm (abst$a))))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   421
           (transitive combth (beta_conversion (cterm (absu$a))))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   422
  end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   423
  handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   424
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   425
(*Calling equal_abs_elim with multiple terms*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   426
fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   427
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   428
local
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   429
  open Logic
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   430
  val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   431
  fun err th = raise THM("flexpair_inst: ", 0, [th])
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   432
  fun flexpair_inst def th =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   433
    let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   434
	val cterm = Sign.cterm_of sign
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   435
	fun cvar a = cterm(Var((a,0),alpha))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   436
	val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)] 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   437
		   def
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   438
    in  equal_elim def' th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   439
    end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   440
    handle THM _ => err th | bind => err th
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   441
in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   442
val flexpair_intr = flexpair_inst (symmetric flexpair_def)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   443
and flexpair_elim = flexpair_inst flexpair_def
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   444
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   445
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   446
(*Version for flexflex pairs -- this supports lifting.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   447
fun flexpair_abs_elim_list cts = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   448
    flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   449
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   450
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   451
(*** Some useful meta-theorems ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   452
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   453
(*The rule V/V, obtains assumption solving for eresolve_tac*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   454
val asm_rl = trivial(Sign.read_cterm Sign.pure ("PROP ?psi",propT));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   455
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   456
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   457
val cut_rl = trivial(Sign.read_cterm Sign.pure 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   458
	("PROP ?psi ==> PROP ?theta", propT));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   459
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   460
(*Generalized elim rule for one conclusion; cut_rl with reversed premises: 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   461
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   462
val revcut_rl =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   463
  let val V = Sign.read_cterm Sign.pure ("PROP V", propT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   464
      and VW = Sign.read_cterm Sign.pure ("PROP V ==> PROP W", propT);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   465
  in  standard (implies_intr V 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   466
		(implies_intr VW
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   467
		 (implies_elim (assume VW) (assume V))))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   468
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   469
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   470
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   471
val triv_forall_equality =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   472
  let val V  = Sign.read_cterm Sign.pure ("PROP V", propT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   473
      and QV = Sign.read_cterm Sign.pure ("!!x::'a. PROP V", propT)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   474
      and x  = Sign.read_cterm Sign.pure ("x", TFree("'a",["logic"]));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   475
  in  standard (equal_intr (implies_intr QV (forall_elim x (assume QV)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   476
		           (implies_intr V  (forall_intr x (assume V))))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   477
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   478
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   479
end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   480
end;