src/HOL/Prolog/prolog.ML
author wenzelm
Thu Feb 11 23:00:22 2010 +0100 (2010-02-11)
changeset 35115 446c5063e4fd
parent 32952 aeb1e44fbc19
child 35232 f588e1169c8b
permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
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@32740
     5
Unsynchronized.set Proof.show_main_goal;
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
wenzelm@21425
    13
    Const("Trueprop",_)$t     => isD t
wenzelm@21425
    14
  | Const("op &"  ,_)$l$r     => isD l andalso isD r
wenzelm@21425
    15
  | Const("op -->",_)$l$r     => isG l andalso isD r
wenzelm@21425
    16
  | Const(   "==>",_)$l$r     => isG l andalso isD r
wenzelm@21425
    17
  | Const("All",_)$Abs(s,_,t) => isD t
wenzelm@21425
    18
  | Const("all",_)$Abs(s,_,t) => isD t
wenzelm@21425
    19
  | Const("op |",_)$_$_       => false
wenzelm@21425
    20
  | Const("Ex" ,_)$_          => false
wenzelm@21425
    21
  | Const("not",_)$_          => false
wenzelm@21425
    22
  | Const("True",_)           => false
wenzelm@21425
    23
  | Const("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
wenzelm@21425
    32
    Const("Trueprop",_)$t     => isG t
wenzelm@21425
    33
  | Const("op &"  ,_)$l$r     => isG l andalso isG r
wenzelm@21425
    34
  | Const("op |"  ,_)$l$r     => isG l andalso isG r
wenzelm@21425
    35
  | Const("op -->",_)$l$r     => isD l andalso isG r
wenzelm@21425
    36
  | Const(   "==>",_)$l$r     => isD l andalso isG r
wenzelm@21425
    37
  | Const("All",_)$Abs(_,_,t) => isG t
wenzelm@21425
    38
  | Const("all",_)$Abs(_,_,t) => isG t
wenzelm@21425
    39
  | Const("Ex" ,_)$Abs(_,_,t) => isG t
wenzelm@21425
    40
  | Const("True",_)           => true
wenzelm@21425
    41
  | Const("not",_)$_          => false
wenzelm@21425
    42
  | Const("False",_)          => false
wenzelm@21425
    43
  | _ (* atom *)              => true;
wenzelm@21425
    44
wenzelm@21425
    45
val check_HOHH_tac1 = PRIMITIVE (fn thm =>
wenzelm@21425
    46
        if isG (concl_of thm) then thm else raise not_HOHH);
wenzelm@21425
    47
val check_HOHH_tac2 = PRIMITIVE (fn thm =>
wenzelm@21425
    48
        if forall isG (prems_of thm) then thm else raise not_HOHH);
wenzelm@21425
    49
fun check_HOHH thm  = (if isD (concl_of thm) andalso forall isG (prems_of thm)
wenzelm@21425
    50
                        then thm else raise not_HOHH);
wenzelm@21425
    51
wenzelm@27229
    52
fun atomizeD ctxt thm = let
wenzelm@21425
    53
    fun at  thm = case concl_of thm of
wenzelm@27229
    54
      _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS
wenzelm@27239
    55
        (read_instantiate ctxt [(("x", 0), "?" ^ (if s="P" then "PP" else s))] spec))
wenzelm@21425
    56
    | _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
wenzelm@21425
    57
    | _$(Const("op -->",_)$_$_)     => at(thm RS mp)
wenzelm@21425
    58
    | _                             => [thm]
wenzelm@21425
    59
in map zero_var_indexes (at thm) end;
wenzelm@21425
    60
wenzelm@21425
    61
val atomize_ss =
wenzelm@32010
    62
  Simplifier.theory_context @{theory} empty_ss
wenzelm@21425
    63
  setmksimps (mksimps mksimps_pairs)
wenzelm@21425
    64
  addsimps [
wenzelm@21425
    65
        all_conj_distrib, (* "(! x. P x & Q x) = ((! x. P x) & (! x. Q x))" *)
wenzelm@21425
    66
        imp_conjL RS sym, (* "(D :- G1 :- G2) = (D :- G1 & G2)" *)
wenzelm@21425
    67
        imp_conjR,        (* "(D1 & D2 :- G) = ((D1 :- G) & (D2 :- G))" *)
wenzelm@21425
    68
        imp_all];         (* "((!x. D) :- G) = (!x. D :- G)" *)
wenzelm@21425
    69
wenzelm@32283
    70
(*val hyp_resolve_tac = Subgoal.FOCUS_PREMS (fn {prems, ...} =>
wenzelm@32260
    71
                                  resolve_tac (maps atomizeD prems) 1);
wenzelm@21425
    72
  -- is nice, but cannot instantiate unknowns in the assumptions *)
wenzelm@21425
    73
fun hyp_resolve_tac i st = let
wenzelm@21425
    74
        fun ap (Const("All",_)$Abs(_,_,t))=(case ap t of (k,a,t) => (k+1,a  ,t))
wenzelm@21425
    75
        |   ap (Const("op -->",_)$_$t)    =(case ap t of (k,_,t) => (k,true ,t))
wenzelm@21425
    76
        |   ap t                          =                         (0,false,t);
wenzelm@21425
    77
(*
wenzelm@21425
    78
        fun rep_goal (Const ("all",_)$Abs (_,_,t)) = rep_goal t
wenzelm@21425
    79
        |   rep_goal (Const ("==>",_)$s$t)         =
wenzelm@21425
    80
                        (case rep_goal t of (l,t) => (s::l,t))
wenzelm@21425
    81
        |   rep_goal t                             = ([]  ,t);
wenzelm@21425
    82
        val (prems, Const("Trueprop", _)$concl) = rep_goal
wenzelm@21425
    83
                                                (#3(dest_state (st,i)));
wenzelm@21425
    84
*)
wenzelm@21425
    85
        val subgoal = #3(dest_state (st,i));
wenzelm@21425
    86
        val prems = Logic.strip_assums_hyp subgoal;
wenzelm@21425
    87
        val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal);
wenzelm@21425
    88
        fun drot_tac k i = DETERM (rotate_tac k i);
wenzelm@21425
    89
        fun spec_tac 0 i = all_tac
wenzelm@21425
    90
        |   spec_tac k i = EVERY' [dtac spec, drot_tac ~1, spec_tac (k-1)] i;
wenzelm@21425
    91
        fun dup_spec_tac k i = if k = 0 then all_tac else EVERY'
wenzelm@21425
    92
                      [DETERM o (etac all_dupE), drot_tac ~2, spec_tac (k-1)] i;
wenzelm@21425
    93
        fun same_head _ (Const (x,_)) (Const (y,_)) = x = y
wenzelm@21425
    94
        |   same_head k (s$_)         (t$_)         = same_head k s t
wenzelm@21425
    95
        |   same_head k (Bound i)     (Bound j)     = i = j + k
wenzelm@21425
    96
        |   same_head _ _             _             = true;
wenzelm@21425
    97
        fun mapn f n []      = []
wenzelm@21425
    98
        |   mapn f n (x::xs) = f n x::mapn f (n+1) xs;
wenzelm@21425
    99
        fun pres_tac (k,arrow,t) n i = drot_tac n i THEN (
wenzelm@21425
   100
                if same_head k t concl
wenzelm@21425
   101
                then dup_spec_tac k i THEN
wenzelm@21425
   102
                     (if arrow then etac mp i THEN drot_tac (~n) i else atac i)
wenzelm@21425
   103
                else no_tac);
wenzelm@21425
   104
        val ptacs = mapn (fn n => fn t =>
wenzelm@21425
   105
                          pres_tac (ap (HOLogic.dest_Trueprop t)) n i) 0 prems;
wenzelm@21425
   106
        in Library.foldl (op APPEND) (no_tac, ptacs) st end;
wenzelm@21425
   107
wenzelm@27229
   108
fun ptac ctxt prog = let
wenzelm@32952
   109
  val proga = maps (atomizeD ctxt) prog         (* atomize the prog *)
wenzelm@21425
   110
  in    (REPEAT_DETERM1 o FIRST' [
wenzelm@21425
   111
                rtac TrueI,                     (* "True" *)
wenzelm@21425
   112
                rtac conjI,                     (* "[| P; Q |] ==> P & Q" *)
wenzelm@21425
   113
                rtac allI,                      (* "(!!x. P x) ==> ! x. P x" *)
wenzelm@21425
   114
                rtac exI,                       (* "P x ==> ? x. P x" *)
wenzelm@21425
   115
                rtac impI THEN'                 (* "(P ==> Q) ==> P --> Q" *)
wenzelm@21425
   116
                  asm_full_simp_tac atomize_ss THEN'    (* atomize the asms *)
wenzelm@21425
   117
                  (REPEAT_DETERM o (etac conjE))        (* split the asms *)
wenzelm@21425
   118
                ])
wenzelm@21425
   119
        ORELSE' resolve_tac [disjI1,disjI2]     (* "P ==> P | Q","Q ==> P | Q"*)
wenzelm@21425
   120
        ORELSE' ((resolve_tac proga APPEND' hyp_resolve_tac)
wenzelm@21425
   121
                 THEN' (fn _ => check_HOHH_tac2))
wenzelm@21425
   122
end;
wenzelm@21425
   123
wenzelm@27229
   124
fun prolog_tac ctxt prog =
wenzelm@27229
   125
  check_HOHH_tac1 THEN
wenzelm@27229
   126
  DEPTH_SOLVE (ptac ctxt (map check_HOHH prog) 1);
wenzelm@21425
   127
wenzelm@21425
   128
val prog_HOHH = [];
wenzelm@21425
   129
wenzelm@21425
   130
end;