src/HOL/Decision_Procs/langford.ML
author haftmann
Thu, 26 Aug 2010 20:51:17 +0200
changeset 39019 e46e7a9cb622
parent 38783 32ad17fe2b9c
child 39028 848be46708dc
permissions -rw-r--r--
formerly unnamed infix impliciation now named HOL.implies
haftmann@37743
     1
(*  Title:      HOL/Decision_Procs/langford.ML
wenzelm@29269
     2
    Author:     Amine Chaieb, TU Muenchen
wenzelm@29269
     3
*)
wenzelm@29269
     4
chaieb@23906
     5
signature LANGFORD_QE = 
chaieb@23906
     6
sig
chaieb@23906
     7
  val dlo_tac : Proof.context -> int -> tactic
chaieb@23906
     8
  val dlo_conv : Proof.context -> cterm -> thm
chaieb@23906
     9
end
chaieb@23906
    10
chaieb@24083
    11
structure LangfordQE :LANGFORD_QE = 
chaieb@23906
    12
struct
chaieb@23906
    13
haftmann@30444
    14
val dest_set =
haftmann@30444
    15
 let 
haftmann@30444
    16
  fun h acc ct = 
haftmann@30444
    17
   case term_of ct of
haftmann@32247
    18
     Const (@{const_name Orderings.bot}, _) => acc
haftmann@30444
    19
   | Const (@{const_name insert}, _) $ _ $ t => h (Thm.dest_arg1 ct :: acc) (Thm.dest_arg ct)
haftmann@30444
    20
in h [] end;
haftmann@30444
    21
chaieb@23906
    22
fun prove_finite cT u = 
chaieb@23906
    23
let val [th0,th1] = map (instantiate' [SOME cT] []) @{thms "finite.intros"}
chaieb@23906
    24
    fun ins x th =
wenzelm@36945
    25
     Thm.implies_elim (instantiate' [] [(SOME o Thm.dest_arg o Thm.dest_arg)
chaieb@23906
    26
                                     (Thm.cprop_of th), SOME x] th1) th
chaieb@23906
    27
in fold ins u th0 end;
chaieb@23906
    28
chaieb@23906
    29
val simp_rule = Conv.fconv_rule (Conv.arg_conv (Simplifier.rewrite (HOL_basic_ss addsimps (@{thms "ball_simps"} @ simp_thms))));
chaieb@23906
    30
chaieb@23906
    31
fun basic_dloqe ctxt stupid dlo_qeth dlo_qeth_nolb dlo_qeth_noub gather ep = 
chaieb@23906
    32
 case term_of ep of 
haftmann@38783
    33
  Const(@{const_name Ex},_)$_ => 
chaieb@23906
    34
   let 
chaieb@23906
    35
     val p = Thm.dest_arg ep
chaieb@23906
    36
     val ths = simplify (HOL_basic_ss addsimps gather) (instantiate' [] [SOME p] stupid)
chaieb@23906
    37
     val (L,U) = 
chaieb@23906
    38
       let 
chaieb@23906
    39
         val (x,q) = Thm.dest_abs NONE (Thm.dest_arg (Thm.rhs_of ths))
chaieb@23906
    40
       in (Thm.dest_arg1 q |> Thm.dest_arg1, Thm.dest_arg q |> Thm.dest_arg1)
chaieb@23906
    41
       end
chaieb@23906
    42
     fun proveneF S =         
chaieb@23906
    43
       let val (a,A) = Thm.dest_comb S |>> Thm.dest_arg
chaieb@23906
    44
           val cT = ctyp_of_term a
chaieb@23906
    45
           val ne = instantiate' [SOME cT] [SOME a, SOME A] 
chaieb@23906
    46
                    @{thm insert_not_empty}
haftmann@30444
    47
           val f = prove_finite cT (dest_set S)
chaieb@23906
    48
       in (ne, f) end
chaieb@23906
    49
chaieb@23906
    50
     val qe = case (term_of L, term_of U) of 
haftmann@32247
    51
      (Const (@{const_name Orderings.bot}, _),_) =>  
chaieb@23906
    52
        let
chaieb@23906
    53
          val (neU,fU) = proveneF U 
wenzelm@36945
    54
        in simp_rule (Thm.transitive ths (dlo_qeth_nolb OF [neU, fU])) end
haftmann@32247
    55
    | (_,Const (@{const_name Orderings.bot}, _)) =>  
chaieb@23906
    56
        let
chaieb@23906
    57
          val (neL,fL) = proveneF L
wenzelm@36945
    58
        in simp_rule (Thm.transitive ths (dlo_qeth_noub OF [neL, fL])) end
chaieb@23906
    59
chaieb@23906
    60
    | (_,_) =>  
chaieb@23906
    61
      let 
chaieb@23906
    62
       val (neL,fL) = proveneF L
chaieb@23906
    63
       val (neU,fU) = proveneF U
wenzelm@36945
    64
      in simp_rule (Thm.transitive ths (dlo_qeth OF [neL, neU, fL, fU])) 
chaieb@23906
    65
      end
chaieb@23906
    66
   in qe end 
chaieb@23906
    67
 | _ => error "dlo_qe : Not an existential formula";
chaieb@23906
    68
chaieb@23906
    69
val all_conjuncts = 
chaieb@23906
    70
 let fun h acc ct = 
chaieb@23906
    71
  case term_of ct of
chaieb@23906
    72
   @{term "op &"}$_$_ => h (h acc (Thm.dest_arg ct)) (Thm.dest_arg1 ct)
chaieb@23906
    73
  | _ => ct::acc
chaieb@23906
    74
in h [] end;
chaieb@23906
    75
chaieb@23906
    76
fun conjuncts ct =
chaieb@23906
    77
 case term_of ct of
chaieb@23906
    78
  @{term "op &"}$_$_ => (Thm.dest_arg1 ct)::(conjuncts (Thm.dest_arg ct))
chaieb@23906
    79
| _ => [ct];
chaieb@23906
    80
chaieb@23906
    81
fun fold1 f = foldr1 (uncurry f);
chaieb@23906
    82
chaieb@23906
    83
val list_conj = fold1 (fn c => fn c' => Thm.capply (Thm.capply @{cterm "op &"} c) c') ;
chaieb@23906
    84
chaieb@23906
    85
fun mk_conj_tab th = 
chaieb@23906
    86
 let fun h acc th = 
chaieb@23906
    87
   case prop_of th of
chaieb@23906
    88
   @{term "Trueprop"}$(@{term "op &"}$p$q) => 
chaieb@23906
    89
     h (h acc (th RS conjunct2)) (th RS conjunct1)
chaieb@23906
    90
  | @{term "Trueprop"}$p => (p,th)::acc
chaieb@23906
    91
in fold (Termtab.insert Thm.eq_thm) (h [] th) Termtab.empty end;
chaieb@23906
    92
chaieb@23906
    93
fun is_conj (@{term "op &"}$_$_) = true
chaieb@23906
    94
  | is_conj _ = false;
chaieb@23906
    95
chaieb@23906
    96
fun prove_conj tab cjs = 
chaieb@23906
    97
 case cjs of 
chaieb@23906
    98
   [c] => if is_conj (term_of c) then prove_conj tab (conjuncts c) else tab c
chaieb@23906
    99
 | c::cs => conjI OF [prove_conj tab [c], prove_conj tab cs];
chaieb@23906
   100
chaieb@23906
   101
fun conj_aci_rule eq = 
chaieb@23906
   102
 let 
chaieb@23906
   103
  val (l,r) = Thm.dest_equals eq
wenzelm@36945
   104
  fun tabl c = the (Termtab.lookup (mk_conj_tab (Thm.assume l)) (term_of c))
wenzelm@36945
   105
  fun tabr c = the (Termtab.lookup (mk_conj_tab (Thm.assume r)) (term_of c))
chaieb@23906
   106
  val ll = Thm.dest_arg l
chaieb@23906
   107
  val rr = Thm.dest_arg r
chaieb@23906
   108
  
chaieb@23906
   109
  val thl  = prove_conj tabl (conjuncts rr) 
chaieb@23906
   110
                |> Drule.implies_intr_hyps
chaieb@23906
   111
  val thr  = prove_conj tabr (conjuncts ll) 
chaieb@23906
   112
                |> Drule.implies_intr_hyps
chaieb@23906
   113
  val eqI = instantiate' [] [SOME ll, SOME rr] @{thm iffI}
wenzelm@36945
   114
 in Thm.implies_elim (Thm.implies_elim eqI thl) thr |> mk_meta_eq end;
chaieb@23906
   115
wenzelm@29265
   116
fun contains x ct = member (op aconv) (OldTerm.term_frees (term_of ct)) (term_of x);
chaieb@23906
   117
chaieb@23906
   118
fun is_eqx x eq = case term_of eq of
haftmann@38774
   119
   Const(@{const_name "op ="},_)$l$r => l aconv term_of x orelse r aconv term_of x
chaieb@23906
   120
 | _ => false ;
chaieb@23906
   121
chaieb@23906
   122
local 
chaieb@23906
   123
fun proc ct = 
chaieb@23906
   124
 case term_of ct of
haftmann@38783
   125
  Const(@{const_name Ex},_)$Abs (xn,_,_) => 
chaieb@23906
   126
   let 
chaieb@23906
   127
    val e = Thm.dest_fun ct
chaieb@23906
   128
    val (x,p) = Thm.dest_abs (SOME xn) (Thm.dest_arg ct)
chaieb@23906
   129
    val Pp = Thm.capply @{cterm "Trueprop"} p 
wenzelm@30148
   130
    val (eqs,neqs) = List.partition (is_eqx x) (all_conjuncts p)
chaieb@23906
   131
   in case eqs of
chaieb@23906
   132
      [] => 
chaieb@23906
   133
        let 
wenzelm@30148
   134
         val (dx,ndx) = List.partition (contains x) neqs
chaieb@23906
   135
         in case ndx of [] => NONE
chaieb@23906
   136
                      | _ =>
chaieb@23906
   137
            conj_aci_rule (Thm.mk_binop @{cterm "op == :: prop => _"} Pp 
chaieb@23906
   138
                 (Thm.capply @{cterm Trueprop} (list_conj (ndx @dx))))
wenzelm@36945
   139
           |> Thm.abstract_rule xn x |> Drule.arg_cong_rule e 
chaieb@23906
   140
           |> Conv.fconv_rule (Conv.arg_conv 
chaieb@23906
   141
                   (Simplifier.rewrite 
chaieb@23906
   142
                      (HOL_basic_ss addsimps (simp_thms @ ex_simps))))
chaieb@23906
   143
           |> SOME
chaieb@23906
   144
          end
chaieb@23906
   145
    | _ => conj_aci_rule (Thm.mk_binop @{cterm "op == :: prop => _"} Pp 
chaieb@23906
   146
                 (Thm.capply @{cterm Trueprop} (list_conj (eqs@neqs))))
wenzelm@36945
   147
           |> Thm.abstract_rule xn x |> Drule.arg_cong_rule e 
chaieb@23906
   148
           |> Conv.fconv_rule (Conv.arg_conv 
chaieb@23906
   149
                   (Simplifier.rewrite 
chaieb@23906
   150
                (HOL_basic_ss addsimps (simp_thms @ ex_simps)))) 
chaieb@23906
   151
           |> SOME
chaieb@23906
   152
   end
chaieb@23906
   153
 | _ => NONE;
chaieb@23906
   154
in val reduce_ex_simproc = 
chaieb@23906
   155
  Simplifier.make_simproc 
chaieb@23906
   156
  {lhss = [@{cpat "EX x. ?P x"}] , name = "reduce_ex_simproc",
chaieb@23906
   157
   proc = K (K proc) , identifier = []}
chaieb@23906
   158
end;
chaieb@23906
   159
chaieb@23906
   160
fun raw_dlo_conv dlo_ss 
chaieb@23906
   161
          ({qe_bnds, qe_nolb, qe_noub, gst, gs, atoms}:Langford_Data.entry) = 
chaieb@23906
   162
 let 
chaieb@23906
   163
  val ss = dlo_ss addsimps @{thms "dnf_simps"} addsimprocs [reduce_ex_simproc]
chaieb@23906
   164
  val dnfex_conv = Simplifier.rewrite ss
chaieb@23906
   165
   val pcv = Simplifier.rewrite
chaieb@23906
   166
               (dlo_ss addsimps (simp_thms @ ex_simps @ all_simps)
chaieb@23906
   167
                @ [@{thm "all_not_ex"}, not_all, ex_disj_distrib])
chaieb@23906
   168
 in fn p => 
chaieb@23906
   169
   Qelim.gen_qelim_conv pcv pcv dnfex_conv cons 
wenzelm@36945
   170
                  (Thm.add_cterm_frees p [])  (K Thm.reflexive) (K Thm.reflexive)
chaieb@23906
   171
                  (K (basic_dloqe () gst qe_bnds qe_nolb qe_noub gs)) p
chaieb@23906
   172
 end;
chaieb@23906
   173
chaieb@23906
   174
chaieb@23906
   175
val grab_atom_bop =
chaieb@23906
   176
 let
chaieb@23906
   177
  fun h bounds tm =
chaieb@23906
   178
   (case term_of tm of
haftmann@38774
   179
     Const (@{const_name "op ="}, T) $ _ $ _ =>
chaieb@23906
   180
       if domain_type T = HOLogic.boolT then find_args bounds tm
chaieb@23906
   181
       else Thm.dest_fun2 tm
haftmann@38783
   182
   | Const (@{const_name Not}, _) $ _ => h bounds (Thm.dest_arg tm)
haftmann@38783
   183
   | Const (@{const_name All}, _) $ _ => find_body bounds (Thm.dest_arg tm)
chaieb@24083
   184
   | Const ("all", _) $ _ => find_body bounds (Thm.dest_arg tm)
haftmann@38783
   185
   | Const (@{const_name Ex}, _) $ _ => find_body bounds (Thm.dest_arg tm)
haftmann@38774
   186
   | Const (@{const_name "op &"}, _) $ _ $ _ => find_args bounds tm
haftmann@38774
   187
   | Const (@{const_name "op |"}, _) $ _ $ _ => find_args bounds tm
haftmann@39019
   188
   | Const (@{const_name HOL.implies}, _) $ _ $ _ => find_args bounds tm
chaieb@23906
   189
   | Const ("==>", _) $ _ $ _ => find_args bounds tm
chaieb@23906
   190
   | Const ("==", _) $ _ $ _ => find_args bounds tm
haftmann@38783
   191
   | Const (@{const_name Trueprop}, _) $ _ => h bounds (Thm.dest_arg tm)
chaieb@23906
   192
   | _ => Thm.dest_fun2 tm)
chaieb@23906
   193
  and find_args bounds tm =
chaieb@24083
   194
    (h bounds (Thm.dest_arg tm) handle CTERM _ => h bounds (Thm.dest_arg1 tm))
chaieb@23906
   195
 and find_body bounds b =
chaieb@23906
   196
   let val (_, b') = Thm.dest_abs (SOME (Name.bound bounds)) b
chaieb@23906
   197
   in h (bounds + 1) b' end;
chaieb@23906
   198
in h end;
chaieb@23906
   199
chaieb@23906
   200
fun dlo_instance ctxt tm =
chaieb@23906
   201
  (fst (Langford_Data.get ctxt), 
chaieb@23906
   202
   Langford_Data.match ctxt (grab_atom_bop 0 tm));
chaieb@23906
   203
chaieb@23906
   204
fun dlo_conv ctxt tm =
chaieb@23906
   205
  (case dlo_instance ctxt tm of
chaieb@23906
   206
    (_, NONE) => raise CTERM ("dlo_conv (langford): no corresponding instance in context!", [tm])
chaieb@23906
   207
  | (ss, SOME instance) => raw_dlo_conv ss instance tm);
chaieb@23906
   208
chaieb@24083
   209
fun generalize_tac f = CSUBGOAL (fn (p, i) => PRIMITIVE (fn st =>
chaieb@24083
   210
 let 
chaieb@24083
   211
   fun all T = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "all"}
chaieb@24083
   212
   fun gen x t = Thm.capply (all (ctyp_of_term x)) (Thm.cabs x t)
wenzelm@35408
   213
   val ts = sort (fn (a,b) => Term_Ord.fast_term_ord (term_of a, term_of b)) (f p)
chaieb@24083
   214
   val p' = fold_rev gen ts p
wenzelm@36945
   215
 in Thm.implies_intr p' (Thm.implies_elim st (fold Thm.forall_elim ts (Thm.assume p'))) end));
chaieb@24083
   216
chaieb@24083
   217
chaieb@24083
   218
fun cfrees ats ct =
chaieb@24083
   219
 let 
chaieb@24083
   220
  val ins = insert (op aconvc)
chaieb@24083
   221
  fun h acc t = 
chaieb@24083
   222
   case (term_of t) of
chaieb@24083
   223
    b$_$_ => if member (op aconvc) ats (Thm.dest_fun2 t) 
chaieb@24083
   224
                then ins (Thm.dest_arg t) (ins (Thm.dest_arg1 t) acc) 
chaieb@24083
   225
                else h (h acc (Thm.dest_arg t)) (Thm.dest_fun t)
chaieb@24083
   226
  | _$_ => h (h acc (Thm.dest_arg t)) (Thm.dest_fun t)
chaieb@24083
   227
  | Abs(_,_,_) => Thm.dest_abs NONE t ||> h acc |> uncurry (remove (op aconvc))
chaieb@24083
   228
  | Free _ => if member (op aconvc) ats t then acc else ins t acc
chaieb@24083
   229
  | Var _ => if member (op aconvc) ats t then acc else ins t acc
chaieb@24083
   230
  | _ => acc
chaieb@24083
   231
 in h [] ct end
chaieb@24083
   232
chaieb@23906
   233
fun dlo_tac ctxt = CSUBGOAL (fn (p, i) =>
chaieb@23906
   234
  (case dlo_instance ctxt p of
chaieb@23906
   235
    (ss, NONE) => simp_tac ss i
chaieb@23906
   236
  | (ss,  SOME instance) =>
wenzelm@35625
   237
      Object_Logic.full_atomize_tac i THEN
chaieb@24083
   238
      simp_tac ss i
chaieb@24083
   239
      THEN (CONVERSION Thm.eta_long_conversion) i
chaieb@24083
   240
      THEN (TRY o generalize_tac (cfrees (#atoms instance))) i
wenzelm@35625
   241
      THEN Object_Logic.full_atomize_tac i
wenzelm@35625
   242
      THEN CONVERSION (Object_Logic.judgment_conv (raw_dlo_conv ss instance)) i
chaieb@24083
   243
      THEN (simp_tac ss i)));  
chaieb@23906
   244
end;