src/HOL/Tools/reconstruction.ML
author wenzelm
Thu, 28 Sep 2006 23:42:39 +0200
changeset 20769 5d538d3d5e2a
parent 20762 a7a5157c5e75
permissions -rw-r--r--
replaced syntax/translations by abbreviation; fixed translations: CONST;
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
struct
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    11
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    12
(**** attributes ****)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    13
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    14
(** Binary resolution **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    15
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    16
fun binary_rule ((cl1, lit1), (cl2 , lit2)) =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    17
     select_literal (lit1 + 1) cl1
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    18
     RSN ((lit2 + 1), cl2);
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    19
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    20
val binary = Attrib.syntax
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    21
  (Scan.lift Args.nat -- Attrib.thm -- Scan.lift Args.nat
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    22
    >> (fn ((i, B), j) => Thm.rule_attribute (fn _ => fn A => binary_rule ((A, i), (B, j)))));
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    23
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    24
19963
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    25
(** Factoring **)
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    26
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    27
(*NB this code did not work at all before 29/6/2006. Even now its behaviour may
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    28
  not be as expected. It unifies the designated literals
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    29
  and then deletes ALL duplicates of literals (not just those designated)*)
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    30
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    31
fun mksubstlist [] sublist = sublist
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    32
  | mksubstlist ((a, (T, b)) :: rest) sublist =
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    33
      mksubstlist rest ((Var(a,T), b)::sublist);
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    34
19963
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    35
fun reorient (x,y) = 
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    36
      if is_Var x then (x,y)
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    37
      else if is_Var y then (y,x)
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    38
      else error "Reconstruction.reorient: neither term is a Var";
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    39
19963
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    40
fun inst_subst sign subst cl =
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    41
  let val subst' = map (pairself (cterm_of sign) o reorient) subst
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    42
  in 
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    43
      Seq.hd(distinct_subgoals_tac (cterm_instantiate subst' cl))
806eaa2a2a5e fixed the "factor" method
paulson
parents: 18729
diff changeset
    44
  end;
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    45
20762
a7a5157c5e75 clearout of obsolete code
paulson
parents: 20258
diff changeset
    46
fun getnewenv seq = fst (fst (the (Seq.pull seq)));
a7a5157c5e75 clearout of obsolete code
paulson
parents: 20258
diff changeset
    47
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    48
fun factor_rule (cl, lit1, lit2) =
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    49
    let
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    50
       val prems = prems_of cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    51
       val fac1 = List.nth (prems,lit1)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    52
       val fac2 = List.nth (prems,lit2)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    53
       val sign = sign_of_thm cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    54
       val unif_env = Unify.unifiers (sign, Envir.empty 0, [(fac1, fac2)])
20762
a7a5157c5e75 clearout of obsolete code
paulson
parents: 20258
diff changeset
    55
       val newenv = getnewenv unif_env
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    56
       val envlist = Envir.alist_of newenv
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    57
     in
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    58
       inst_subst sign (mksubstlist envlist []) cl
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    59
    end;
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    60
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    61
val factor = Attrib.syntax (Scan.lift (Args.nat -- Args.nat)
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    62
  >> (fn (i, j) => Thm.rule_attribute (fn _ => fn A => factor_rule (A, i, j))));
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    63
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    64
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    65
(** Paramodulation **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    66
15449
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    67
(*subst with premises exchanged: that way, side literals of the equality will appear
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    68
  as the second to last premises of the result.*)
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    69
val rev_subst = rotate_prems 1 subst;
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    70
15499
419dc5ffe8bc clausification and proof reconstruction
paulson
parents: 15495
diff changeset
    71
fun paramod_rule ((cl1, lit1), (cl2, lit2)) =
15449
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    72
    let  val eq_lit_th = select_literal (lit1+1) cl1
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    73
         val mod_lit_th = select_literal (lit2+1) cl2
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    74
         val eqsubst = eq_lit_th RSN (2,rev_subst)
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    75
         val newth = Seq.hd (biresolution false [(false, mod_lit_th)] 1 eqsubst)
15499
419dc5ffe8bc clausification and proof reconstruction
paulson
parents: 15495
diff changeset
    76
         val newth' = Seq.hd (flexflex_rule newth)
15579
32bee18c675f Tools/meson.ML: signature, structure and "open" rather than "local"
paulson
parents: 15531
diff changeset
    77
    in Meson.negated_asm_of_head newth' end;
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    78
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    79
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    80
val paramod = Attrib.syntax (Scan.lift Args.nat -- Attrib.thm -- Scan.lift Args.nat
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    81
  >> (fn ((i, B), j) => Thm.rule_attribute (fn _ => fn A => paramod_rule ((A, i), (B, j)))));
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    82
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    83
15449
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    84
(** Demodulation: rewriting of a single literal (Non-Unit Rewriting, SPASS) **)
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    85
20258
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    86
fun demod_rule ctxt ((cl1, lit1), (cl2 , lit2)) =
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    87
    let  val eq_lit_th = select_literal (lit1+1) cl1
15449
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    88
         val mod_lit_th = select_literal (lit2+1) cl2
20258
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    89
         val ((_, [fmod_th]), ctxt') = Variable.import true [mod_lit_th] ctxt
15449
a27c81bd838d fixed the treatment of demodulation and paramodulation
paulson
parents: 15384
diff changeset
    90
         val eqsubst = eq_lit_th RSN (2,rev_subst)
20258
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    91
         val newth =
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    92
           Seq.hd (biresolution false [(false, fmod_th)] 1 eqsubst)
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    93
           |> singleton (Variable.export ctxt' ctxt)
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    94
    in Meson.negated_asm_of_head newth end;
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    95
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    96
val demod = Attrib.syntax (Scan.lift Args.nat -- Attrib.thm -- Scan.lift Args.nat
20258
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    97
  >> (fn ((i, B), j) => Thm.rule_attribute (fn context => fn A =>
4fe3c0911907 demod_rule: depend on context, proper Variable.import/export;
wenzelm
parents: 19963
diff changeset
    98
      demod_rule (Context.proof_of context) ((A, i), (B, j)))));
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
    99
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   100
15359
8bad1f42fec0 new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents: 15151
diff changeset
   101
(** Conversion of a theorem into clauses **)
8bad1f42fec0 new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents: 15151
diff changeset
   102
15955
87cf2ce8ede8 memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents: 15794
diff changeset
   103
(*For efficiency, we rely upon memo-izing in ResAxioms.*)
87cf2ce8ede8 memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents: 15794
diff changeset
   104
fun clausify_rule (th,i) = List.nth (ResAxioms.meta_cnf_axiom th, i)
15466
dce7827f8d75 implemented cache for conversion to clauses
paulson
parents: 15449
diff changeset
   105
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   106
val clausify = Attrib.syntax (Scan.lift Args.nat
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   107
  >> (fn i => Thm.rule_attribute (fn _ => fn th => clausify_rule (th, i))));
15359
8bad1f42fec0 new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents: 15151
diff changeset
   108
8bad1f42fec0 new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents: 15151
diff changeset
   109
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   110
(** theory setup **)
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   111
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   112
val setup =
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 17488
diff changeset
   113
  Attrib.add_attributes
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   114
    [("binary", binary, "binary resolution"),
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   115
     ("paramod", paramod, "paramodulation"),
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   116
     ("demod", demod, "demodulation"),
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   117
     ("factor", factor, "factoring"),
216e31270509 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   118
     ("clausify", clausify, "conversion to clauses")];
15151
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   119
429666b09783 proof reconstruction for external ATPs
paulson
parents:
diff changeset
   120
end