src/HOL/Tools/reconstruction.ML
author haftmann
Fri Dec 16 09:00:11 2005 +0100 (2005-12-16)
changeset 18418 bf448d999b7e
parent 17488 67376a311a2b
child 18708 4b3dadb4fe33
permissions -rw-r--r--
re-arranged tuples (theory * 'a) to ('a * theory) in Pure
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
berghofe@15794
    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
(**** attributes ****)
paulson@15151
    27
paulson@15151
    28
(** Binary resolution **)
paulson@15151
    29
paulson@15151
    30
fun binary_rule ((cl1, lit1), (cl2 , lit2)) =
paulson@15151
    31
     select_literal (lit1 + 1) cl1
paulson@15151
    32
     RSN ((lit2 + 1), cl2);
paulson@15151
    33
paulson@15151
    34
fun binary_syntax ((i, B), j) (x, A) = (x, binary_rule ((A,i), (B,j)));
paulson@15151
    35
paulson@15384
    36
fun gen_binary thm = syntax
paulson@15151
    37
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> binary_syntax);
paulson@15384
    38
val binary_global = gen_binary global_thm;
paulson@15384
    39
val binary_local = gen_binary local_thm;
paulson@15151
    40
paulson@15151
    41
(*I have not done the MRR rule because it seems to be identifical to 
paulson@15384
    42
binary*)
paulson@15151
    43
paulson@15151
    44
paulson@15151
    45
fun inst_single sign t1 t2 cl =
paulson@15151
    46
    let val ct1 = cterm_of sign t1 and ct2 = cterm_of sign t2
paulson@15151
    47
    in  hd (Seq.list_of(distinct_subgoals_tac
paulson@15151
    48
			    (cterm_instantiate [(ct1,ct2)] cl)))  
paulson@15151
    49
    end;
paulson@15151
    50
paulson@15151
    51
fun inst_subst sign substs cl =
paulson@15151
    52
    if (is_Var (fst(hd(substs))))
paulson@15151
    53
    then inst_single sign (fst (hd substs)) (snd (hd substs)) cl
paulson@15151
    54
    else if (is_Var (snd(hd(substs))))
paulson@15151
    55
    then inst_single sign (snd (hd substs)) (fst (hd substs)) cl
paulson@15151
    56
    else raise THM ("inst_subst", 0, [cl]);
paulson@15151
    57
paulson@15151
    58
paulson@15151
    59
(** Factoring **)
paulson@15151
    60
paulson@15151
    61
fun factor_rule (cl, lit1, lit2) =
paulson@15151
    62
    let
paulson@15151
    63
       val prems = prems_of cl
paulson@15151
    64
       val fac1 = List.nth (prems,lit1)
paulson@15151
    65
       val fac2 = List.nth (prems,lit2)
paulson@15151
    66
       val sign = sign_of_thm cl
paulson@15151
    67
       val unif_env = Unify.unifiers (sign, Envir.empty 0, [(fac1, fac2)])
paulson@17488
    68
       val newenv = ReconTranslateProof.getnewenv unif_env
paulson@15151
    69
       val envlist = Envir.alist_of newenv
paulson@15151
    70
     in
paulson@15151
    71
       inst_subst sign (mksubstlist envlist []) cl
paulson@15151
    72
    end;
paulson@15151
    73
paulson@15151
    74
fun factor_syntax (i, j) (x, A) = (x, factor_rule (A,i,j));
paulson@15151
    75
paulson@15384
    76
fun factor x = syntax ((Scan.lift (Args.nat -- Args.nat)) >> factor_syntax) x;
paulson@15151
    77
paulson@15151
    78
paulson@15151
    79
(** Paramodulation **)
paulson@15151
    80
paulson@15449
    81
(*subst with premises exchanged: that way, side literals of the equality will appear
paulson@15449
    82
  as the second to last premises of the result.*)
paulson@15449
    83
val rev_subst = rotate_prems 1 subst;
paulson@15449
    84
paulson@15499
    85
fun paramod_rule ((cl1, lit1), (cl2, lit2)) =
paulson@15449
    86
    let  val eq_lit_th = select_literal (lit1+1) cl1
paulson@15449
    87
         val mod_lit_th = select_literal (lit2+1) cl2
paulson@15449
    88
         val eqsubst = eq_lit_th RSN (2,rev_subst)
paulson@15449
    89
         val newth = Seq.hd (biresolution false [(false, mod_lit_th)] 1 eqsubst)
paulson@15499
    90
         val newth' = Seq.hd (flexflex_rule newth)
paulson@15579
    91
    in Meson.negated_asm_of_head newth' end;
paulson@15151
    92
paulson@15151
    93
paulson@15151
    94
fun paramod_syntax ((i, B), j) (x, A) = (x, paramod_rule ((A,i), (B,j)));
paulson@15151
    95
paulson@15384
    96
fun gen_paramod thm = syntax
paulson@15151
    97
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> paramod_syntax);
paulson@15384
    98
val paramod_global = gen_paramod global_thm;
paulson@15384
    99
val paramod_local = gen_paramod local_thm;
paulson@15151
   100
paulson@15151
   101
paulson@15449
   102
(** Demodulation: rewriting of a single literal (Non-Unit Rewriting, SPASS) **)
paulson@15151
   103
paulson@15449
   104
fun demod_rule ((cl1, lit1), (cl2 , lit2)) = 
paulson@15151
   105
    let  val eq_lit_th = select_literal (lit1+1) cl1
paulson@15449
   106
         val mod_lit_th = select_literal (lit2+1) cl2
paulson@15495
   107
	 val (fmod_th,thaw) = Drule.freeze_thaw_robust mod_lit_th
paulson@15449
   108
         val eqsubst = eq_lit_th RSN (2,rev_subst)
paulson@15495
   109
         val newth = Seq.hd(biresolution false [(false, fmod_th)] 1 eqsubst)
paulson@15499
   110
         val offset = #maxidx(rep_thm newth) + 1
paulson@15499
   111
	                  (*ensures "renaming apart" even when Vars are frozen*)
paulson@15579
   112
    in Meson.negated_asm_of_head (thaw offset newth) end;
paulson@15151
   113
paulson@15449
   114
fun demod_syntax ((i, B), j) (x, A) = (x, demod_rule ((A,i), (B,j)));
paulson@15151
   115
paulson@15449
   116
fun gen_demod thm = syntax
paulson@15449
   117
      ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> demod_syntax);
paulson@15384
   118
val demod_global = gen_demod global_thm;
paulson@15384
   119
val demod_local = gen_demod local_thm;
paulson@15151
   120
paulson@15151
   121
paulson@15359
   122
(** Conversion of a theorem into clauses **)
paulson@15359
   123
paulson@15955
   124
(*For efficiency, we rely upon memo-izing in ResAxioms.*)
paulson@15955
   125
fun clausify_rule (th,i) = List.nth (ResAxioms.meta_cnf_axiom th, i)
paulson@15466
   126
paulson@15955
   127
fun clausify_syntax i (x, th) = (x, clausify_rule (th,i));
paulson@15359
   128
paulson@15384
   129
fun clausify x = syntax ((Scan.lift Args.nat) >> clausify_syntax) x;
paulson@15359
   130
paulson@15359
   131
paulson@15151
   132
(** theory setup **)
paulson@15151
   133
paulson@15151
   134
val setup =
paulson@15151
   135
  [Attrib.add_attributes
paulson@15384
   136
     [("binary", (binary_global, binary_local), "binary resolution"),
paulson@15384
   137
      ("paramod", (paramod_global, paramod_local), "paramodulation"),
paulson@15384
   138
      ("demod", (demod_global, demod_local), "demodulation"),
paulson@15384
   139
      ("factor", (factor, factor), "factoring"),
paulson@15384
   140
      ("clausify", (clausify, clausify), "conversion to clauses")]];
paulson@15151
   141
paulson@15151
   142
end
paulson@15151
   143
end
paulson@15151
   144
paulson@15151
   145