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