src/HOL/Tools/reconstruction.ML
author chaieb
Fri, 19 Nov 2004 14:00:31 +0100
changeset 15298 a5bea99352d6
parent 15151 429666b09783
child 15359 8bad1f42fec0
permissions -rw-r--r--
Barith removed VS: ----------------------------------------------------------------------
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     1
(*  Title:      HOL/Reconstruction.thy
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     2
    ID: $Id$
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson and Claire Quigley
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     4
    Copyright   2004  University of Cambridge
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     5
*)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     6
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     7
(*Attributes for reconstructing external resolution proofs*)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     8
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
     9
structure Reconstruction =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    10
let open Attrib
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    11
in
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    12
struct
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    13
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    14
(**************************************************************)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    15
(* extra functions necessary for factoring and paramodulation *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    16
(**************************************************************)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    17
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    18
fun mksubstlist [] sublist = sublist
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    19
  | mksubstlist ((a,b)::rest) sublist = 
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    20
      let val vartype = type_of b
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    21
          val avar = Var(a,vartype)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    22
          val newlist = ((avar,b)::sublist) 
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    23
      in mksubstlist rest newlist end;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    24
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    25
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    26
fun get_unif_comb t eqterm =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    27
    if ((type_of t) = (type_of eqterm))
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    28
    then t
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    29
    else
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    30
        let val _ $ rand = t
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    31
        in get_unif_comb rand eqterm end;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    32
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    33
fun get_unif_lit t eqterm =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    34
    if (can HOLogic.dest_eq t)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    35
    then
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    36
	let val (lhs,rhs) = HOLogic.dest_eq(HOLogic.dest_Trueprop eqterm)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    37
	in lhs end
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    38
    else
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    39
	get_unif_comb t eqterm;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    40
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    41
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    42
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    43
(**** attributes ****)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    44
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    45
(** Binary resolution **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    46
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    47
fun binary_rule ((cl1, lit1), (cl2 , lit2)) =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    48
     select_literal (lit1 + 1) cl1
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    49
     RSN ((lit2 + 1), cl2);
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    50
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    51
fun binary_syntax ((i, B), j) (x, A) = (x, binary_rule ((A,i), (B,j)));
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    52
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    53
fun gen_BINARY thm = syntax
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    54
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> binary_syntax);
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    55
val BINARY_global = gen_BINARY global_thm;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    56
val BINARY_local = gen_BINARY local_thm;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    57
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    58
(*I have not done the MRR rule because it seems to be identifical to 
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    59
BINARY*)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    60
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    61
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    62
fun inst_single sign t1 t2 cl =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    63
    let val ct1 = cterm_of sign t1 and ct2 = cterm_of sign t2
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    64
    in  hd (Seq.list_of(distinct_subgoals_tac
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    65
			    (cterm_instantiate [(ct1,ct2)] cl)))  
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    66
    end;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    67
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    68
fun inst_subst sign substs cl =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    69
    if (is_Var (fst(hd(substs))))
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    70
    then inst_single sign (fst (hd substs)) (snd (hd substs)) cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    71
    else if (is_Var (snd(hd(substs))))
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    72
    then inst_single sign (snd (hd substs)) (fst (hd substs)) cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    73
    else raise THM ("inst_subst", 0, [cl]);
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    74
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    75
(*Grabs the environment from the result of Unify.unifiers*)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    76
fun getnewenv thisseq = fst (hd (Seq.list_of thisseq));
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    77
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    78
(** Factoring **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    79
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    80
fun factor_rule (cl, lit1, lit2) =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    81
    let
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    82
       val prems = prems_of cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    83
       val fac1 = List.nth (prems,lit1)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    84
       val fac2 = List.nth (prems,lit2)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    85
       val sign = sign_of_thm cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    86
       val unif_env = Unify.unifiers (sign, Envir.empty 0, [(fac1, fac2)])
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    87
       val newenv = getnewenv unif_env
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    88
       val envlist = Envir.alist_of newenv
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    89
     in
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    90
       inst_subst sign (mksubstlist envlist []) cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    91
    end;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    92
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    93
fun factor_syntax (i, j) (x, A) = (x, factor_rule (A,i,j));
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    94
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    95
fun FACTOR x = syntax ((Scan.lift (Args.nat -- Args.nat)) >> factor_syntax) x;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    96
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    97
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    98
(** Paramodulation **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    99
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   100
(*Get rid of a Not if it is present*)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   101
fun maybe_dest_not (Const ("Not", _) $ t) = t
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   102
  | maybe_dest_not t = t;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   103
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   104
fun paramod_rule ((cl1, lit1), (cl2 , lit2)) =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   105
    let val prems1 = prems_of cl1
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   106
	val prems2 = prems_of cl2
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   107
        val sign = Sign.merge (sign_of_thm cl1, sign_of_thm cl2)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   108
	(* want to get first element of equality *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   109
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   110
	val fac1 = List.nth (prems1,lit1) 
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   111
	val (lhs, rhs) = HOLogic.dest_eq(maybe_dest_not
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   112
					     (HOLogic.dest_Trueprop fac1))
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   113
	(* get other literal involved in the paramodulation *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   114
	val fac2 = List.nth (prems2,lit2) 
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   115
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   116
       (* get bit of th2 to unify with lhs of cl1 *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   117
	val unif_lit = get_unif_lit (HOLogic.dest_Trueprop fac2) lhs
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   118
	val unif_env = Unify.unifiers (sign, Envir.empty 0, [(unif_lit, lhs)])
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   119
	val newenv = getnewenv unif_env
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   120
	val envlist = Envir.alist_of newenv
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   121
       (* instantiate cl2 with unifiers *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   122
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   123
	val newth1 = inst_subst sign (mksubstlist envlist []) cl1
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   124
       (*rewrite cl2 with the equality bit of cl2 i.e. lit2 *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   125
	  val facthm' = select_literal (lit1 + 1) newth1
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   126
	  val equal_lit = concl_of facthm'
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   127
	  val cterm_eq = cterm_of sign equal_lit
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   128
	  val eq_thm = assume cterm_eq
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   129
	  val meta_eq_thm = mk_meta_eq eq_thm
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   130
	  val newth2= rewrite_rule [meta_eq_thm] cl2
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   131
       (*thin lit2 from cl2 *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   132
       (* get cl1 with lit1 as concl, then resolve with thin_rl *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   133
	  val thm' = facthm' RS thin_rl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   134
       (* now resolve cl2 with last premise of thm' *)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   135
	  val newthm = newth2  RSN ((length prems1), thm')
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   136
     in newthm end
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   137
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   138
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   139
fun paramod_syntax ((i, B), j) (x, A) = (x, paramod_rule ((A,i), (B,j)));
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   140
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   141
fun gen_PARAMOD thm = syntax
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   142
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> paramod_syntax);
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   143
val PARAMOD_global = gen_PARAMOD global_thm;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   144
val PARAMOD_local = gen_PARAMOD local_thm;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   145
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   146
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   147
(** Demodulation, i.e. rewriting **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   148
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   149
fun demod_rule (cl1,lit1,cl2) =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   150
    let  val eq_lit_th = select_literal (lit1+1) cl1
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   151
	 val equal_lit = concl_of eq_lit_th
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   152
         val sign = Sign.merge (sign_of_thm cl1, sign_of_thm cl2)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   153
	 val cterm_eq = cterm_of sign equal_lit
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   154
	 val eq_thm = assume cterm_eq
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   155
	 val meta_eq_thm = mk_meta_eq eq_thm
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   156
	 val newth2= rewrite_rule [meta_eq_thm] cl2
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   157
    in newth2 end;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   158
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   159
fun demod_syntax (i, B) (x, A) = (x, demod_rule (A,i,B));
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   160
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   161
fun gen_DEMOD thm = syntax ((Scan.lift Args.nat -- thm) >> demod_syntax);
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   162
val DEMOD_global = gen_DEMOD global_thm;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   163
val DEMOD_local = gen_DEMOD local_thm;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   164
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   165
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   166
(** theory setup **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   167
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   168
val setup =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   169
  [Attrib.add_attributes
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   170
     [("BINARY", (BINARY_global, BINARY_local), "binary resolution"),
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   171
      ("PARAMOD", (PARAMOD_global, PARAMOD_local), "paramodulation"),
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   172
      ("DEMOD", (DEMOD_global, DEMOD_local), "demodulation"),
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   173
      ("FACTOR", (FACTOR, FACTOR), "factoring")]];
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   174
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   175
end
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   176
end
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   177
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   178