src/Tools/coherent.ML
author wenzelm
Sat Feb 28 14:02:12 2009 +0100 (2009-02-28)
changeset 30160 5f7b17941730
parent 29273 src/Provers/coherent.ML@285c00993bc2
child 30164 9321f7b70450
permissions -rw-r--r--
moved some generic tools to src/Tools/ -- src/Provers is essentially obsolete;
wenzelm@30160
     1
(*  Title:      Tools/coherent.ML
berghofe@28326
     2
    Author:     Stefan Berghofer, TU Muenchen
wenzelm@29273
     3
    Author:     Marc Bezem, Institutt for Informatikk, Universitetet i Bergen 
berghofe@28326
     4
berghofe@28326
     5
Prover for coherent logic, see e.g.
berghofe@28326
     6
berghofe@28326
     7
  Marc Bezem and Thierry Coquand, Automating Coherent Logic, LPAR 2005
berghofe@28326
     8
berghofe@28326
     9
for a description of the algorithm.
berghofe@28326
    10
*)
berghofe@28326
    11
berghofe@28326
    12
signature COHERENT_DATA =
berghofe@28326
    13
sig
berghofe@28326
    14
  val atomize_elimL: thm
berghofe@28326
    15
  val atomize_exL: thm
berghofe@28326
    16
  val atomize_conjL: thm
berghofe@28326
    17
  val atomize_disjL: thm
berghofe@28326
    18
  val operator_names: string list
berghofe@28326
    19
end;
berghofe@28326
    20
berghofe@28326
    21
signature COHERENT =
berghofe@28326
    22
sig
berghofe@28326
    23
  val verbose: bool ref
berghofe@28326
    24
  val show_facts: bool ref
berghofe@28326
    25
  val coherent_tac: thm list -> Proof.context -> int -> tactic
berghofe@28326
    26
  val coherent_meth: thm list -> Proof.context -> Proof.method
berghofe@28326
    27
  val setup: theory -> theory
berghofe@28326
    28
end;
berghofe@28326
    29
berghofe@28326
    30
functor CoherentFun(Data: COHERENT_DATA) : COHERENT =
berghofe@28326
    31
struct
berghofe@28326
    32
berghofe@28326
    33
val verbose = ref false;
berghofe@28326
    34
berghofe@28326
    35
fun message f = if !verbose then tracing (f ()) else ();
berghofe@28326
    36
berghofe@28326
    37
datatype cl_prf =
berghofe@28326
    38
  ClPrf of thm * (Type.tyenv * Envir.tenv) * ((indexname * typ) * term) list *
berghofe@28326
    39
  int list * (term list * cl_prf) list;
berghofe@28326
    40
wenzelm@29273
    41
val is_atomic = not o exists_Const (member (op =) Data.operator_names o #1);
berghofe@28326
    42
berghofe@28326
    43
local open Conv in
berghofe@28326
    44
berghofe@28326
    45
fun rulify_elim_conv ct =
berghofe@28326
    46
  if is_atomic (Logic.strip_imp_concl (term_of ct)) then all_conv ct
berghofe@28326
    47
  else concl_conv (length (Logic.strip_imp_prems (term_of ct)))
berghofe@28326
    48
    (rewr_conv (symmetric Data.atomize_elimL) then_conv
berghofe@28326
    49
     MetaSimplifier.rewrite true (map symmetric
berghofe@28326
    50
       [Data.atomize_exL, Data.atomize_conjL, Data.atomize_disjL])) ct
berghofe@28326
    51
berghofe@28326
    52
end;
berghofe@28326
    53
berghofe@28326
    54
fun rulify_elim th = MetaSimplifier.norm_hhf (Conv.fconv_rule rulify_elim_conv th);
berghofe@28326
    55
berghofe@28326
    56
(* Decompose elimination rule of the form
berghofe@28326
    57
   A1 ==> ... ==> Am ==> (!!xs1. Bs1 ==> P) ==> ... ==> (!!xsn. Bsn ==> P) ==> P
berghofe@28326
    58
*)
berghofe@28326
    59
fun dest_elim prop =
berghofe@28326
    60
  let
berghofe@28326
    61
    val prems = Logic.strip_imp_prems prop;
berghofe@28326
    62
    val concl = Logic.strip_imp_concl prop;
berghofe@28326
    63
    val (prems1, prems2) =
berghofe@28326
    64
      take_suffix (fn t => Logic.strip_assums_concl t = concl) prems;
berghofe@28326
    65
  in
berghofe@28326
    66
    (prems1,
berghofe@28326
    67
     if null prems2 then [([], [concl])]
berghofe@28326
    68
     else map (fn t =>
berghofe@28326
    69
       (map snd (Logic.strip_params t), Logic.strip_assums_hyp t)) prems2)
berghofe@28326
    70
  end;
berghofe@28326
    71
berghofe@28326
    72
fun mk_rule th =
berghofe@28326
    73
  let
berghofe@28326
    74
    val th' = rulify_elim th;
berghofe@28326
    75
    val (prems, cases) = dest_elim (prop_of th')
berghofe@28326
    76
  in (th', prems, cases) end;
berghofe@28326
    77
berghofe@28326
    78
fun mk_dom ts = fold (fn t =>
berghofe@28326
    79
  Typtab.map_default (fastype_of t, []) (fn us => us @ [t])) ts Typtab.empty;
berghofe@28326
    80
berghofe@28326
    81
val empty_env = (Vartab.empty, Vartab.empty);
berghofe@28326
    82
berghofe@28326
    83
(* Find matcher that makes conjunction valid in given state *)
berghofe@28326
    84
fun valid_conj ctxt facts env [] = Seq.single (env, [])
berghofe@28326
    85
  | valid_conj ctxt facts env (t :: ts) =
berghofe@28326
    86
      Seq.maps (fn (u, x) => Seq.map (apsnd (cons x))
berghofe@28326
    87
        (valid_conj ctxt facts
berghofe@28326
    88
           (Pattern.match (ProofContext.theory_of ctxt) (t, u) env) ts
berghofe@28326
    89
         handle Pattern.MATCH => Seq.empty))
berghofe@28326
    90
          (Seq.of_list (sort (int_ord o pairself snd) (Net.unify_term facts t)));
berghofe@28326
    91
berghofe@28326
    92
(* Instantiate variables that only occur free in conlusion *)
berghofe@28326
    93
fun inst_extra_vars ctxt dom cs =
berghofe@28326
    94
  let
berghofe@28326
    95
    val vs = fold Term.add_vars (maps snd cs) [];
berghofe@28326
    96
    fun insts [] inst = Seq.single inst
berghofe@28326
    97
      | insts ((ixn, T) :: vs') inst = Seq.maps
berghofe@28326
    98
          (fn t => insts vs' (((ixn, T), t) :: inst))
berghofe@28326
    99
          (Seq.of_list (case Typtab.lookup dom T of
berghofe@28326
   100
             NONE => error ("Unknown domain: " ^
berghofe@28326
   101
               Syntax.string_of_typ ctxt T ^ "\nfor term(s) " ^
berghofe@28326
   102
               commas (maps (map (Syntax.string_of_term ctxt) o snd) cs))
berghofe@28326
   103
           | SOME ts => ts))
berghofe@28326
   104
  in Seq.map (fn inst =>
berghofe@28326
   105
    (inst, map (apsnd (map (subst_Vars (map (apfst fst) inst)))) cs))
berghofe@28326
   106
      (insts vs [])
berghofe@28326
   107
  end;
berghofe@28326
   108
berghofe@28326
   109
(* Check whether disjunction is valid in given state *)
berghofe@28326
   110
fun is_valid_disj ctxt facts [] = false
berghofe@28326
   111
  | is_valid_disj ctxt facts ((Ts, ts) :: ds) =
berghofe@28326
   112
      let val vs = rev (map_index (fn (i, T) => Var (("x", i), T)) Ts)
berghofe@28326
   113
      in case Seq.pull (valid_conj ctxt facts empty_env
berghofe@28326
   114
        (map (fn t => subst_bounds (vs, t)) ts)) of
berghofe@28326
   115
          SOME _ => true
berghofe@28326
   116
        | NONE => is_valid_disj ctxt facts ds
berghofe@28326
   117
      end;
berghofe@28326
   118
berghofe@28326
   119
val show_facts = ref false;
berghofe@28326
   120
berghofe@28326
   121
fun string_of_facts ctxt s facts = space_implode "\n"
berghofe@28326
   122
  (s :: map (Syntax.string_of_term ctxt)
berghofe@28326
   123
     (map fst (sort (int_ord o pairself snd) (Net.content facts)))) ^ "\n\n";
berghofe@28326
   124
berghofe@28326
   125
fun print_facts ctxt facts =
berghofe@28326
   126
  if !show_facts then message (fn () => string_of_facts ctxt "Facts:" facts)
berghofe@28326
   127
  else ();
berghofe@28326
   128
berghofe@28326
   129
fun valid ctxt rules goal dom facts nfacts nparams =
berghofe@28326
   130
  let val seq = Seq.of_list rules |> Seq.maps (fn (th, ps, cs) =>
berghofe@28326
   131
    valid_conj ctxt facts empty_env ps |> Seq.maps (fn (env as (tye, _), is) =>
berghofe@28326
   132
      let val cs' = map (fn (Ts, ts) =>
berghofe@28326
   133
        (map (Envir.typ_subst_TVars tye) Ts, map (Envir.subst_vars env) ts)) cs
berghofe@28326
   134
      in
berghofe@28326
   135
        inst_extra_vars ctxt dom cs' |>
berghofe@28326
   136
          Seq.map_filter (fn (inst, cs'') =>
berghofe@28326
   137
            if is_valid_disj ctxt facts cs'' then NONE
berghofe@28326
   138
            else SOME (th, env, inst, is, cs''))
berghofe@28326
   139
      end))
berghofe@28326
   140
  in
berghofe@28326
   141
    case Seq.pull seq of
berghofe@28326
   142
      NONE => (tracing (string_of_facts ctxt "Countermodel found:" facts); NONE)
berghofe@28326
   143
    | SOME ((th, env, inst, is, cs), _) =>
berghofe@28326
   144
        if cs = [([], [goal])] then SOME (ClPrf (th, env, inst, is, []))
berghofe@28326
   145
        else
berghofe@28326
   146
          (case valid_cases ctxt rules goal dom facts nfacts nparams cs of
berghofe@28326
   147
             NONE => NONE
berghofe@28326
   148
           | SOME prfs => SOME (ClPrf (th, env, inst, is, prfs)))
berghofe@28326
   149
  end
berghofe@28326
   150
berghofe@28326
   151
and valid_cases ctxt rules goal dom facts nfacts nparams [] = SOME []
berghofe@28326
   152
  | valid_cases ctxt rules goal dom facts nfacts nparams ((Ts, ts) :: ds) =
berghofe@28326
   153
      let
berghofe@28326
   154
        val _ = message (fn () => "case " ^ commas (map (Syntax.string_of_term ctxt) ts));
berghofe@28326
   155
        val params = rev (map_index (fn (i, T) =>
berghofe@28326
   156
          Free ("par" ^ string_of_int (nparams + i), T)) Ts);
berghofe@28326
   157
        val ts' = map_index (fn (i, t) =>
berghofe@28326
   158
          (subst_bounds (params, t), nfacts + i)) ts;
berghofe@28326
   159
        val dom' = fold (fn (T, p) =>
berghofe@28326
   160
          Typtab.map_default (T, []) (fn ps => ps @ [p]))
berghofe@28326
   161
            (Ts ~~ params) dom;
berghofe@28326
   162
        val facts' = fold (fn (t, i) => Net.insert_term op =
berghofe@28326
   163
          (t, (t, i))) ts' facts
berghofe@28326
   164
      in
berghofe@28326
   165
        case valid ctxt rules goal dom' facts'
berghofe@28326
   166
          (nfacts + length ts) (nparams + length Ts) of
berghofe@28326
   167
          NONE => NONE
berghofe@28326
   168
        | SOME prf => (case valid_cases ctxt rules goal dom facts nfacts nparams ds of
berghofe@28326
   169
            NONE => NONE
berghofe@28326
   170
          | SOME prfs => SOME ((params, prf) :: prfs))
berghofe@28326
   171
      end;
berghofe@28326
   172
berghofe@28326
   173
(** proof replaying **)
berghofe@28326
   174
berghofe@28326
   175
fun thm_of_cl_prf thy goal asms (ClPrf (th, (tye, env), insts, is, prfs)) =
berghofe@28326
   176
  let
berghofe@28326
   177
    val _ = message (fn () => space_implode "\n"
berghofe@28326
   178
      ("asms:" :: map Display.string_of_thm asms) ^ "\n\n");
berghofe@28326
   179
    val th' = Drule.implies_elim_list
berghofe@28326
   180
      (Thm.instantiate
berghofe@28326
   181
         (map (fn (ixn, (S, T)) =>
berghofe@28326
   182
            (Thm.ctyp_of thy (TVar ((ixn, S))), Thm.ctyp_of thy T))
berghofe@28326
   183
               (Vartab.dest tye),
berghofe@28326
   184
          map (fn (ixn, (T, t)) =>
berghofe@28326
   185
            (Thm.cterm_of thy (Var (ixn, Envir.typ_subst_TVars tye T)),
berghofe@28326
   186
             Thm.cterm_of thy t)) (Vartab.dest env) @
berghofe@28326
   187
          map (fn (ixnT, t) =>
berghofe@28326
   188
            (Thm.cterm_of thy (Var ixnT), Thm.cterm_of thy t)) insts) th)
berghofe@28326
   189
      (map (nth asms) is);
berghofe@28326
   190
    val (_, cases) = dest_elim (prop_of th')
berghofe@28326
   191
  in
berghofe@28326
   192
    case (cases, prfs) of
berghofe@28326
   193
      ([([], [_])], []) => th'
berghofe@28326
   194
    | ([([], [_])], [([], prf)]) => thm_of_cl_prf thy goal (asms @ [th']) prf
berghofe@28326
   195
    | _ => Drule.implies_elim_list
berghofe@28326
   196
        (Thm.instantiate (Thm.match
berghofe@28326
   197
           (Drule.strip_imp_concl (cprop_of th'), goal)) th')
berghofe@28326
   198
        (map (thm_of_case_prf thy goal asms) (prfs ~~ cases))
berghofe@28326
   199
  end
berghofe@28326
   200
berghofe@28326
   201
and thm_of_case_prf thy goal asms ((params, prf), (_, asms')) =
berghofe@28326
   202
  let
berghofe@28326
   203
    val cparams = map (cterm_of thy) params;
berghofe@28326
   204
    val asms'' = map (cterm_of thy o curry subst_bounds (rev params)) asms'
berghofe@28326
   205
  in
berghofe@28326
   206
    Drule.forall_intr_list cparams (Drule.implies_intr_list asms''
berghofe@28326
   207
      (thm_of_cl_prf thy goal (asms @ map Thm.assume asms'') prf))
berghofe@28326
   208
  end;
berghofe@28326
   209
berghofe@28326
   210
berghofe@28326
   211
(** external interface **)
berghofe@28326
   212
berghofe@28326
   213
fun coherent_tac rules ctxt = SUBPROOF (fn {prems, concl, params, context, ...} =>
berghofe@28326
   214
  rtac (rulify_elim_conv concl RS equal_elim_rule2) 1 THEN
berghofe@28326
   215
  SUBPROOF (fn {prems = prems', concl, context, ...} =>
berghofe@28326
   216
    let val xs = map term_of params @
berghofe@28326
   217
      map (fn (_, s) => Free (s, the (Variable.default_type context s)))
berghofe@28326
   218
        (Variable.fixes_of context)
berghofe@28326
   219
    in
berghofe@28326
   220
      case valid context (map mk_rule (prems' @ prems @ rules)) (term_of concl)
berghofe@28326
   221
           (mk_dom xs) Net.empty 0 0 of
berghofe@28326
   222
         NONE => no_tac
berghofe@28326
   223
       | SOME prf =>
berghofe@28326
   224
           rtac (thm_of_cl_prf (ProofContext.theory_of context) concl [] prf) 1
berghofe@28339
   225
    end) context 1) ctxt;
berghofe@28326
   226
berghofe@28326
   227
fun coherent_meth rules ctxt =
berghofe@28326
   228
  Method.METHOD (fn facts => coherent_tac (facts @ rules) ctxt 1);
berghofe@28326
   229
berghofe@28326
   230
val setup = Method.add_method
berghofe@28326
   231
  ("coherent", Method.thms_ctxt_args coherent_meth, "Prove coherent formula");
berghofe@28326
   232
berghofe@28326
   233
end;