src/HOL/Tools/reconstruction.ML
author paulson
Fri Jan 21 13:53:30 2005 +0100 (2005-01-21)
changeset 15449 a27c81bd838d
parent 15384 b13eb8a8897d
child 15466 dce7827f8d75
permissions -rw-r--r--
fixed the treatment of demodulation and paramodulation
paulson@15151
     1
(*  Title:      HOL/Reconstruction.thy
paulson@15151
     2
    ID: $Id$
paulson@15151
     3
    Author:     Lawrence C Paulson and Claire Quigley
paulson@15151
     4
    Copyright   2004  University of Cambridge
paulson@15151
     5
*)
paulson@15151
     6
paulson@15151
     7
(*Attributes for reconstructing external resolution proofs*)
paulson@15151
     8
paulson@15151
     9
structure Reconstruction =
paulson@15151
    10
let open Attrib
paulson@15151
    11
in
paulson@15151
    12
struct
paulson@15151
    13
paulson@15151
    14
(**************************************************************)
paulson@15151
    15
(* extra functions necessary for factoring and paramodulation *)
paulson@15151
    16
(**************************************************************)
paulson@15151
    17
paulson@15151
    18
fun mksubstlist [] sublist = sublist
paulson@15151
    19
  | mksubstlist ((a,b)::rest) sublist = 
paulson@15151
    20
      let val vartype = type_of b
paulson@15151
    21
          val avar = Var(a,vartype)
paulson@15151
    22
          val newlist = ((avar,b)::sublist) 
paulson@15151
    23
      in mksubstlist rest newlist end;
paulson@15151
    24
paulson@15151
    25
paulson@15151
    26
fun get_unif_comb t eqterm =
paulson@15151
    27
    if ((type_of t) = (type_of eqterm))
paulson@15151
    28
    then t
paulson@15151
    29
    else
paulson@15151
    30
        let val _ $ rand = t
paulson@15151
    31
        in get_unif_comb rand eqterm end;
paulson@15151
    32
paulson@15151
    33
fun get_unif_lit t eqterm =
paulson@15151
    34
    if (can HOLogic.dest_eq t)
paulson@15151
    35
    then
paulson@15151
    36
	let val (lhs,rhs) = HOLogic.dest_eq(HOLogic.dest_Trueprop eqterm)
paulson@15151
    37
	in lhs end
paulson@15151
    38
    else
paulson@15151
    39
	get_unif_comb t eqterm;
paulson@15151
    40
paulson@15151
    41
paulson@15151
    42
paulson@15151
    43
(**** attributes ****)
paulson@15151
    44
paulson@15151
    45
(** Binary resolution **)
paulson@15151
    46
paulson@15151
    47
fun binary_rule ((cl1, lit1), (cl2 , lit2)) =
paulson@15151
    48
     select_literal (lit1 + 1) cl1
paulson@15151
    49
     RSN ((lit2 + 1), cl2);
paulson@15151
    50
paulson@15151
    51
fun binary_syntax ((i, B), j) (x, A) = (x, binary_rule ((A,i), (B,j)));
paulson@15151
    52
paulson@15384
    53
fun gen_binary thm = syntax
paulson@15151
    54
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> binary_syntax);
paulson@15384
    55
val binary_global = gen_binary global_thm;
paulson@15384
    56
val binary_local = gen_binary local_thm;
paulson@15151
    57
paulson@15151
    58
(*I have not done the MRR rule because it seems to be identifical to 
paulson@15384
    59
binary*)
paulson@15151
    60
paulson@15151
    61
paulson@15151
    62
fun inst_single sign t1 t2 cl =
paulson@15151
    63
    let val ct1 = cterm_of sign t1 and ct2 = cterm_of sign t2
paulson@15151
    64
    in  hd (Seq.list_of(distinct_subgoals_tac
paulson@15151
    65
			    (cterm_instantiate [(ct1,ct2)] cl)))  
paulson@15151
    66
    end;
paulson@15151
    67
paulson@15151
    68
fun inst_subst sign substs cl =
paulson@15151
    69
    if (is_Var (fst(hd(substs))))
paulson@15151
    70
    then inst_single sign (fst (hd substs)) (snd (hd substs)) cl
paulson@15151
    71
    else if (is_Var (snd(hd(substs))))
paulson@15151
    72
    then inst_single sign (snd (hd substs)) (fst (hd substs)) cl
paulson@15151
    73
    else raise THM ("inst_subst", 0, [cl]);
paulson@15151
    74
paulson@15151
    75
(*Grabs the environment from the result of Unify.unifiers*)
paulson@15151
    76
fun getnewenv thisseq = fst (hd (Seq.list_of thisseq));
paulson@15151
    77
paulson@15151
    78
(** Factoring **)
paulson@15151
    79
paulson@15151
    80
fun factor_rule (cl, lit1, lit2) =
paulson@15151
    81
    let
paulson@15151
    82
       val prems = prems_of cl
paulson@15151
    83
       val fac1 = List.nth (prems,lit1)
paulson@15151
    84
       val fac2 = List.nth (prems,lit2)
paulson@15151
    85
       val sign = sign_of_thm cl
paulson@15151
    86
       val unif_env = Unify.unifiers (sign, Envir.empty 0, [(fac1, fac2)])
paulson@15151
    87
       val newenv = getnewenv unif_env
paulson@15151
    88
       val envlist = Envir.alist_of newenv
paulson@15151
    89
     in
paulson@15151
    90
       inst_subst sign (mksubstlist envlist []) cl
paulson@15151
    91
    end;
paulson@15151
    92
paulson@15151
    93
fun factor_syntax (i, j) (x, A) = (x, factor_rule (A,i,j));
paulson@15151
    94
paulson@15384
    95
fun factor x = syntax ((Scan.lift (Args.nat -- Args.nat)) >> factor_syntax) x;
paulson@15151
    96
paulson@15151
    97
paulson@15151
    98
(** Paramodulation **)
paulson@15151
    99
paulson@15449
   100
(*subst with premises exchanged: that way, side literals of the equality will appear
paulson@15449
   101
  as the second to last premises of the result.*)
paulson@15449
   102
val rev_subst = rotate_prems 1 subst;
paulson@15449
   103
paulson@15151
   104
(*Get rid of a Not if it is present*)
paulson@15151
   105
fun maybe_dest_not (Const ("Not", _) $ t) = t
paulson@15151
   106
  | maybe_dest_not t = t;
paulson@15151
   107
paulson@15151
   108
fun paramod_rule ((cl1, lit1), (cl2 , lit2)) =
paulson@15449
   109
    let  val eq_lit_th = select_literal (lit1+1) cl1
paulson@15449
   110
         val mod_lit_th = select_literal (lit2+1) cl2
paulson@15449
   111
         val eqsubst = eq_lit_th RSN (2,rev_subst)
paulson@15449
   112
         val newth = Seq.hd (biresolution false [(false, mod_lit_th)] 1 eqsubst)
paulson@15449
   113
    in negated_asm_of_head newth end
paulson@15151
   114
paulson@15151
   115
paulson@15151
   116
fun paramod_syntax ((i, B), j) (x, A) = (x, paramod_rule ((A,i), (B,j)));
paulson@15151
   117
paulson@15384
   118
fun gen_paramod thm = syntax
paulson@15151
   119
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> paramod_syntax);
paulson@15384
   120
val paramod_global = gen_paramod global_thm;
paulson@15384
   121
val paramod_local = gen_paramod local_thm;
paulson@15151
   122
paulson@15151
   123
paulson@15449
   124
(** Demodulation: rewriting of a single literal (Non-Unit Rewriting, SPASS) **)
paulson@15151
   125
paulson@15449
   126
(*currently identical to paramod_rule: the "match" argument of biresolution cannot be used
paulson@15449
   127
  to prevent instantiation of the rewritten literal, in mod_lit_th: it could only prevent
paulson@15449
   128
  instantiation of eq_lit_th, which we do not want.*)
paulson@15449
   129
fun demod_rule ((cl1, lit1), (cl2 , lit2)) = 
paulson@15151
   130
    let  val eq_lit_th = select_literal (lit1+1) cl1
paulson@15449
   131
         val mod_lit_th = select_literal (lit2+1) cl2
paulson@15449
   132
         val eqsubst = eq_lit_th RSN (2,rev_subst)
paulson@15449
   133
         val newth = Seq.hd (biresolution false [(false, mod_lit_th)] 1 eqsubst)
paulson@15449
   134
    in negated_asm_of_head newth end
paulson@15449
   135
    handle _ => raise THM ("select_literal", lit1, [cl1,cl2]);
paulson@15151
   136
paulson@15449
   137
fun demod_syntax ((i, B), j) (x, A) = (x, demod_rule ((A,i), (B,j)));
paulson@15151
   138
paulson@15449
   139
fun gen_demod thm = syntax
paulson@15449
   140
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> demod_syntax);
paulson@15384
   141
val demod_global = gen_demod global_thm;
paulson@15384
   142
val demod_local = gen_demod local_thm;
paulson@15151
   143
paulson@15151
   144
paulson@15359
   145
(** Conversion of a theorem into clauses **)
paulson@15359
   146
paulson@15359
   147
fun clausify_rule (A,i) =
paulson@15359
   148
  standard
paulson@15359
   149
    (make_meta_clause
paulson@15365
   150
      (List.nth(ResAxioms.cnf_axiom A,i)));
paulson@15359
   151
paulson@15359
   152
fun clausify_syntax i (x, A) = (x, clausify_rule (A,i));
paulson@15359
   153
paulson@15384
   154
fun clausify x = syntax ((Scan.lift Args.nat) >> clausify_syntax) x;
paulson@15359
   155
paulson@15359
   156
paulson@15151
   157
(** theory setup **)
paulson@15151
   158
paulson@15151
   159
val setup =
paulson@15151
   160
  [Attrib.add_attributes
paulson@15384
   161
     [("binary", (binary_global, binary_local), "binary resolution"),
paulson@15384
   162
      ("paramod", (paramod_global, paramod_local), "paramodulation"),
paulson@15384
   163
      ("demod", (demod_global, demod_local), "demodulation"),
paulson@15384
   164
      ("factor", (factor, factor), "factoring"),
paulson@15384
   165
      ("clausify", (clausify, clausify), "conversion to clauses")]];
paulson@15151
   166
paulson@15151
   167
end
paulson@15151
   168
end
paulson@15151
   169
paulson@15151
   170