src/HOL/Prolog/prolog.ML
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (20 months ago)
changeset 67022 49309fe530fd
parent 60754 02924903a6fd
child 69597 ff784d5a5bfb
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
wenzelm@21425
     1
(*  Title:    HOL/Prolog/prolog.ML
wenzelm@21425
     2
    Author:   David von Oheimb (based on a lecture on Lambda Prolog by Nadathur)
wenzelm@21425
     3
*)
wenzelm@21425
     4
wenzelm@56467
     5
Options.default_put_bool @{system_option show_main_goal} true;
wenzelm@21425
     6
wenzelm@21425
     7
structure Prolog =
wenzelm@21425
     8
struct
wenzelm@21425
     9
wenzelm@21425
    10
exception not_HOHH;
wenzelm@21425
    11
wenzelm@21425
    12
fun isD t = case t of
haftmann@38557
    13
    Const(@{const_name Trueprop},_)$t     => isD t
haftmann@38795
    14
  | Const(@{const_name HOL.conj}  ,_)$l$r     => isD l andalso isD r
haftmann@38786
    15
  | Const(@{const_name HOL.implies},_)$l$r     => isG l andalso isD r
wenzelm@56245
    16
  | Const(@{const_name Pure.imp},_)$l$r     => isG l andalso isD r
haftmann@38557
    17
  | Const(@{const_name All},_)$Abs(s,_,t) => isD t
wenzelm@56245
    18
  | Const(@{const_name Pure.all},_)$Abs(s,_,t) => isD t
haftmann@38795
    19
  | Const(@{const_name HOL.disj},_)$_$_       => false
haftmann@38557
    20
  | Const(@{const_name Ex} ,_)$_          => false
haftmann@38557
    21
  | Const(@{const_name Not},_)$_          => false
haftmann@38557
    22
  | Const(@{const_name True},_)           => false
haftmann@38557
    23
  | Const(@{const_name False},_)          => false
wenzelm@21425
    24
  | l $ r                     => isD l
wenzelm@21425
    25
  | Const _ (* rigid atom *)  => true
wenzelm@21425
    26
  | Bound _ (* rigid atom *)  => true
wenzelm@21425
    27
  | Free  _ (* rigid atom *)  => true
wenzelm@21425
    28
  | _    (* flexible atom,
wenzelm@21425
    29
            anything else *)  => false
wenzelm@21425
    30
and
wenzelm@21425
    31
    isG t = case t of
haftmann@38557
    32
    Const(@{const_name Trueprop},_)$t     => isG t
haftmann@38795
    33
  | Const(@{const_name HOL.conj}  ,_)$l$r     => isG l andalso isG r
haftmann@38795
    34
  | Const(@{const_name HOL.disj}  ,_)$l$r     => isG l andalso isG r
haftmann@38786
    35
  | Const(@{const_name HOL.implies},_)$l$r     => isD l andalso isG r
wenzelm@56245
    36
  | Const(@{const_name Pure.imp},_)$l$r     => isD l andalso isG r
haftmann@38557
    37
  | Const(@{const_name All},_)$Abs(_,_,t) => isG t
wenzelm@56245
    38
  | Const(@{const_name Pure.all},_)$Abs(_,_,t) => isG t
haftmann@38557
    39
  | Const(@{const_name Ex} ,_)$Abs(_,_,t) => isG t
haftmann@38557
    40
  | Const(@{const_name True},_)           => true
haftmann@38557
    41
  | Const(@{const_name Not},_)$_          => false
haftmann@38557
    42
  | Const(@{const_name False},_)          => false
wenzelm@21425
    43
  | _ (* atom *)              => true;
wenzelm@21425
    44
wenzelm@21425
    45
val check_HOHH_tac1 = PRIMITIVE (fn thm =>
wenzelm@59582
    46
        if isG (Thm.concl_of thm) then thm else raise not_HOHH);
wenzelm@21425
    47
val check_HOHH_tac2 = PRIMITIVE (fn thm =>
wenzelm@59582
    48
        if forall isG (Thm.prems_of thm) then thm else raise not_HOHH);
wenzelm@59582
    49
fun check_HOHH thm  = (if isD (Thm.concl_of thm) andalso forall isG (Thm.prems_of thm)
wenzelm@21425
    50
                        then thm else raise not_HOHH);
wenzelm@21425
    51
wenzelm@55143
    52
fun atomizeD ctxt thm =
wenzelm@55143
    53
  let
wenzelm@59582
    54
    fun at  thm = case Thm.concl_of thm of
wenzelm@55143
    55
      _$(Const(@{const_name All} ,_)$Abs(s,_,_))=>
wenzelm@59755
    56
        let val s' = if s="P" then "PP" else s in
wenzelm@59755
    57
          at(thm RS (Rule_Insts.read_instantiate ctxt [((("x", 0), Position.none), s')] [s'] spec))
wenzelm@59755
    58
        end
haftmann@38795
    59
    | _$(Const(@{const_name HOL.conj},_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
haftmann@38786
    60
    | _$(Const(@{const_name HOL.implies},_)$_$_)     => at(thm RS mp)
wenzelm@21425
    61
    | _                             => [thm]
wenzelm@55143
    62
  in map zero_var_indexes (at thm) end;
wenzelm@21425
    63
wenzelm@21425
    64
val atomize_ss =
wenzelm@52233
    65
  (empty_simpset @{context} |> Simplifier.set_mksimps (mksimps mksimps_pairs))
wenzelm@21425
    66
  addsimps [
wenzelm@45654
    67
        @{thm all_conj_distrib}, (* "(! x. P x & Q x) = ((! x. P x) & (! x. Q x))" *)
wenzelm@46161
    68
        @{thm imp_conjL} RS sym, (* "(D :- G1 :- G2) = (D :- G1 & G2)" *)
wenzelm@46161
    69
        @{thm imp_conjR},        (* "(D1 & D2 :- G) = ((D1 :- G) & (D2 :- G))" *)
wenzelm@52088
    70
        @{thm imp_all}]          (* "((!x. D) :- G) = (!x. D :- G)" *)
wenzelm@52088
    71
  |> simpset_of;
wenzelm@21425
    72
wenzelm@45625
    73
wenzelm@32283
    74
(*val hyp_resolve_tac = Subgoal.FOCUS_PREMS (fn {prems, ...} =>
wenzelm@32260
    75
                                  resolve_tac (maps atomizeD prems) 1);
wenzelm@21425
    76
  -- is nice, but cannot instantiate unknowns in the assumptions *)
wenzelm@60754
    77
fun hyp_resolve_tac ctxt = SUBGOAL (fn (subgoal, i) =>
wenzelm@46473
    78
  let
haftmann@38557
    79
        fun ap (Const(@{const_name All},_)$Abs(_,_,t))=(case ap t of (k,a,t) => (k+1,a  ,t))
haftmann@38786
    80
        |   ap (Const(@{const_name HOL.implies},_)$_$t)    =(case ap t of (k,_,t) => (k,true ,t))
wenzelm@21425
    81
        |   ap t                          =                         (0,false,t);
wenzelm@21425
    82
(*
wenzelm@56245
    83
        fun rep_goal (Const (@{const_name Pure.all},_)$Abs (_,_,t)) = rep_goal t
wenzelm@56245
    84
        |   rep_goal (Const (@{const_name Pure.imp},_)$s$t)         =
wenzelm@21425
    85
                        (case rep_goal t of (l,t) => (s::l,t))
wenzelm@21425
    86
        |   rep_goal t                             = ([]  ,t);
haftmann@38557
    87
        val (prems, Const(@{const_name Trueprop}, _)$concl) = rep_goal
wenzelm@21425
    88
                                                (#3(dest_state (st,i)));
wenzelm@21425
    89
*)
wenzelm@21425
    90
        val prems = Logic.strip_assums_hyp subgoal;
wenzelm@21425
    91
        val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal);
wenzelm@21425
    92
        fun drot_tac k i = DETERM (rotate_tac k i);
wenzelm@21425
    93
        fun spec_tac 0 i = all_tac
wenzelm@60754
    94
        |   spec_tac k i = EVERY' [dresolve_tac ctxt [spec], drot_tac ~1, spec_tac (k-1)] i;
wenzelm@21425
    95
        fun dup_spec_tac k i = if k = 0 then all_tac else EVERY'
wenzelm@60754
    96
                      [DETERM o (eresolve_tac ctxt [all_dupE]), drot_tac ~2, spec_tac (k-1)] i;
wenzelm@21425
    97
        fun same_head _ (Const (x,_)) (Const (y,_)) = x = y
wenzelm@21425
    98
        |   same_head k (s$_)         (t$_)         = same_head k s t
wenzelm@21425
    99
        |   same_head k (Bound i)     (Bound j)     = i = j + k
wenzelm@21425
   100
        |   same_head _ _             _             = true;
wenzelm@21425
   101
        fun mapn f n []      = []
wenzelm@21425
   102
        |   mapn f n (x::xs) = f n x::mapn f (n+1) xs;
wenzelm@21425
   103
        fun pres_tac (k,arrow,t) n i = drot_tac n i THEN (
wenzelm@60754
   104
          if same_head k t concl
wenzelm@60754
   105
          then dup_spec_tac k i THEN
wenzelm@60754
   106
               (if arrow then eresolve_tac ctxt [mp] i THEN drot_tac (~n) i else assume_tac ctxt i)
wenzelm@60754
   107
          else no_tac);
wenzelm@21425
   108
        val ptacs = mapn (fn n => fn t =>
wenzelm@21425
   109
                          pres_tac (ap (HOLogic.dest_Trueprop t)) n i) 0 prems;
wenzelm@46473
   110
  in Library.foldl (op APPEND) (no_tac, ptacs) end);
wenzelm@21425
   111
wenzelm@27229
   112
fun ptac ctxt prog = let
wenzelm@32952
   113
  val proga = maps (atomizeD ctxt) prog         (* atomize the prog *)
wenzelm@21425
   114
  in    (REPEAT_DETERM1 o FIRST' [
wenzelm@60754
   115
                resolve_tac ctxt [TrueI],                     (* "True" *)
wenzelm@60754
   116
                resolve_tac ctxt [conjI],                     (* "[| P; Q |] ==> P & Q" *)
wenzelm@60754
   117
                resolve_tac ctxt [allI],                      (* "(!!x. P x) ==> ! x. P x" *)
wenzelm@60754
   118
                resolve_tac ctxt [exI],                       (* "P x ==> ? x. P x" *)
wenzelm@60754
   119
                resolve_tac ctxt [impI] THEN'                 (* "(P ==> Q) ==> P --> Q" *)
wenzelm@52088
   120
                  asm_full_simp_tac (put_simpset atomize_ss ctxt) THEN'    (* atomize the asms *)
wenzelm@60754
   121
                  (REPEAT_DETERM o (eresolve_tac ctxt [conjE]))        (* split the asms *)
wenzelm@21425
   122
                ])
wenzelm@59498
   123
        ORELSE' resolve_tac ctxt [disjI1,disjI2]     (* "P ==> P | Q","Q ==> P | Q"*)
wenzelm@60754
   124
        ORELSE' ((resolve_tac ctxt proga APPEND' hyp_resolve_tac ctxt)
wenzelm@21425
   125
                 THEN' (fn _ => check_HOHH_tac2))
wenzelm@21425
   126
end;
wenzelm@21425
   127
wenzelm@27229
   128
fun prolog_tac ctxt prog =
wenzelm@27229
   129
  check_HOHH_tac1 THEN
wenzelm@27229
   130
  DEPTH_SOLVE (ptac ctxt (map check_HOHH prog) 1);
wenzelm@21425
   131
wenzelm@21425
   132
val prog_HOHH = [];
wenzelm@21425
   133
wenzelm@21425
   134
end;