src/HOL/Prolog/prolog.ML
author wenzelm
Sun, 26 Jan 2014 13:45:40 +0100
changeset 55151 f331472f1027
parent 55143 04448228381d
child 56245 84fc7dfa3cd4
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     1
(*  Title:    HOL/Prolog/prolog.ML
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     2
    Author:   David von Oheimb (based on a lecture on Lambda Prolog by Nadathur)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     3
*)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     4
52043
286629271d65 more system options as context-sensitive config options;
wenzelm
parents: 46473
diff changeset
     5
Options.default_put_bool @{option show_main_goal} true;
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     6
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     7
structure Prolog =
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     8
struct
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
     9
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    10
exception not_HOHH;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    11
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    12
fun isD t = case t of
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    13
    Const(@{const_name Trueprop},_)$t     => isD t
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    14
  | Const(@{const_name HOL.conj}  ,_)$l$r     => isD l andalso isD r
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38557
diff changeset
    15
  | Const(@{const_name HOL.implies},_)$l$r     => isG l andalso isD r
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    16
  | Const(   "==>",_)$l$r     => isG l andalso isD r
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    17
  | Const(@{const_name All},_)$Abs(s,_,t) => isD t
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    18
  | Const("all",_)$Abs(s,_,t) => isD t
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    19
  | Const(@{const_name HOL.disj},_)$_$_       => false
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    20
  | Const(@{const_name Ex} ,_)$_          => false
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    21
  | Const(@{const_name Not},_)$_          => false
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    22
  | Const(@{const_name True},_)           => false
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    23
  | Const(@{const_name False},_)          => false
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    24
  | l $ r                     => isD l
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    25
  | Const _ (* rigid atom *)  => true
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    26
  | Bound _ (* rigid atom *)  => true
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    27
  | Free  _ (* rigid atom *)  => true
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    28
  | _    (* flexible atom,
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    29
            anything else *)  => false
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    30
and
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    31
    isG t = case t of
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    32
    Const(@{const_name Trueprop},_)$t     => isG t
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    33
  | Const(@{const_name HOL.conj}  ,_)$l$r     => isG l andalso isG r
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    34
  | Const(@{const_name HOL.disj}  ,_)$l$r     => isG l andalso isG r
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38557
diff changeset
    35
  | Const(@{const_name HOL.implies},_)$l$r     => isD l andalso isG r
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    36
  | Const(   "==>",_)$l$r     => isD l andalso isG r
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    37
  | Const(@{const_name All},_)$Abs(_,_,t) => isG t
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    38
  | Const("all",_)$Abs(_,_,t) => isG t
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    39
  | Const(@{const_name Ex} ,_)$Abs(_,_,t) => isG t
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    40
  | Const(@{const_name True},_)           => true
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    41
  | Const(@{const_name Not},_)$_          => false
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    42
  | Const(@{const_name False},_)          => false
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    43
  | _ (* atom *)              => true;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    44
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    45
val check_HOHH_tac1 = PRIMITIVE (fn thm =>
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    46
        if isG (concl_of thm) then thm else raise not_HOHH);
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    47
val check_HOHH_tac2 = PRIMITIVE (fn thm =>
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    48
        if forall isG (prems_of thm) then thm else raise not_HOHH);
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    49
fun check_HOHH thm  = (if isD (concl_of thm) andalso forall isG (prems_of thm)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    50
                        then thm else raise not_HOHH);
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    51
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 52233
diff changeset
    52
fun atomizeD ctxt thm =
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 52233
diff changeset
    53
  let
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    54
    fun at  thm = case concl_of thm of
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 52233
diff changeset
    55
      _$(Const(@{const_name All} ,_)$Abs(s,_,_))=>
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 52233
diff changeset
    56
        let val s' = if s="P" then "PP" else s
55151
f331472f1027 tuned signature;
wenzelm
parents: 55143
diff changeset
    57
        in at(thm RS (Rule_Insts.read_instantiate ctxt [(("x", 0), s')] [s'] spec)) end
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
    58
    | _$(Const(@{const_name HOL.conj},_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38557
diff changeset
    59
    | _$(Const(@{const_name HOL.implies},_)$_$_)     => at(thm RS mp)
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    60
    | _                             => [thm]
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 52233
diff changeset
    61
  in map zero_var_indexes (at thm) end;
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    62
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    63
val atomize_ss =
52233
wenzelm
parents: 52088
diff changeset
    64
  (empty_simpset @{context} |> Simplifier.set_mksimps (mksimps mksimps_pairs))
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    65
  addsimps [
45654
cf10bde35973 more antiquotations;
wenzelm
parents: 45625
diff changeset
    66
        @{thm all_conj_distrib}, (* "(! x. P x & Q x) = ((! x. P x) & (! x. Q x))" *)
46161
4ed94d92ae19 prefer antiquotations;
wenzelm
parents: 45654
diff changeset
    67
        @{thm imp_conjL} RS sym, (* "(D :- G1 :- G2) = (D :- G1 & G2)" *)
4ed94d92ae19 prefer antiquotations;
wenzelm
parents: 45654
diff changeset
    68
        @{thm imp_conjR},        (* "(D1 & D2 :- G) = ((D1 :- G) & (D2 :- G))" *)
52088
7d8b53e80ce7 proper run-time context;
wenzelm
parents: 52043
diff changeset
    69
        @{thm imp_all}]          (* "((!x. D) :- G) = (!x. D :- G)" *)
7d8b53e80ce7 proper run-time context;
wenzelm
parents: 52043
diff changeset
    70
  |> simpset_of;
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    71
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 39125
diff changeset
    72
32283
3bebc195c124 qualified Subgoal.FOCUS;
wenzelm
parents: 32282
diff changeset
    73
(*val hyp_resolve_tac = Subgoal.FOCUS_PREMS (fn {prems, ...} =>
32260
eb97888fa422 eliminated METAHYPS;
wenzelm
parents: 32010
diff changeset
    74
                                  resolve_tac (maps atomizeD prems) 1);
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    75
  -- is nice, but cannot instantiate unknowns in the assumptions *)
46473
a687b75f9fa8 more conventional tactic setup;
wenzelm
parents: 46161
diff changeset
    76
val hyp_resolve_tac = SUBGOAL (fn (subgoal, i) =>
a687b75f9fa8 more conventional tactic setup;
wenzelm
parents: 46161
diff changeset
    77
  let
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    78
        fun ap (Const(@{const_name All},_)$Abs(_,_,t))=(case ap t of (k,a,t) => (k+1,a  ,t))
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38557
diff changeset
    79
        |   ap (Const(@{const_name HOL.implies},_)$_$t)    =(case ap t of (k,_,t) => (k,true ,t))
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    80
        |   ap t                          =                         (0,false,t);
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    81
(*
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    82
        fun rep_goal (Const ("all",_)$Abs (_,_,t)) = rep_goal t
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    83
        |   rep_goal (Const ("==>",_)$s$t)         =
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    84
                        (case rep_goal t of (l,t) => (s::l,t))
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    85
        |   rep_goal t                             = ([]  ,t);
38557
9926c47ad1a1 more antiquotations
haftmann
parents: 38549
diff changeset
    86
        val (prems, Const(@{const_name Trueprop}, _)$concl) = rep_goal
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    87
                                                (#3(dest_state (st,i)));
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    88
*)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    89
        val prems = Logic.strip_assums_hyp subgoal;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    90
        val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal);
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    91
        fun drot_tac k i = DETERM (rotate_tac k i);
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    92
        fun spec_tac 0 i = all_tac
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    93
        |   spec_tac k i = EVERY' [dtac spec, drot_tac ~1, spec_tac (k-1)] i;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    94
        fun dup_spec_tac k i = if k = 0 then all_tac else EVERY'
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    95
                      [DETERM o (etac all_dupE), drot_tac ~2, spec_tac (k-1)] i;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    96
        fun same_head _ (Const (x,_)) (Const (y,_)) = x = y
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    97
        |   same_head k (s$_)         (t$_)         = same_head k s t
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    98
        |   same_head k (Bound i)     (Bound j)     = i = j + k
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
    99
        |   same_head _ _             _             = true;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   100
        fun mapn f n []      = []
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   101
        |   mapn f n (x::xs) = f n x::mapn f (n+1) xs;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   102
        fun pres_tac (k,arrow,t) n i = drot_tac n i THEN (
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   103
                if same_head k t concl
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   104
                then dup_spec_tac k i THEN
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   105
                     (if arrow then etac mp i THEN drot_tac (~n) i else atac i)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   106
                else no_tac);
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   107
        val ptacs = mapn (fn n => fn t =>
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   108
                          pres_tac (ap (HOLogic.dest_Trueprop t)) n i) 0 prems;
46473
a687b75f9fa8 more conventional tactic setup;
wenzelm
parents: 46161
diff changeset
   109
  in Library.foldl (op APPEND) (no_tac, ptacs) end);
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   110
27229
f656a12e0f4e ptac/prolog_tac: proper context;
wenzelm
parents: 27153
diff changeset
   111
fun ptac ctxt prog = let
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32740
diff changeset
   112
  val proga = maps (atomizeD ctxt) prog         (* atomize the prog *)
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   113
  in    (REPEAT_DETERM1 o FIRST' [
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   114
                rtac TrueI,                     (* "True" *)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   115
                rtac conjI,                     (* "[| P; Q |] ==> P & Q" *)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   116
                rtac allI,                      (* "(!!x. P x) ==> ! x. P x" *)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   117
                rtac exI,                       (* "P x ==> ? x. P x" *)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   118
                rtac impI THEN'                 (* "(P ==> Q) ==> P --> Q" *)
52088
7d8b53e80ce7 proper run-time context;
wenzelm
parents: 52043
diff changeset
   119
                  asm_full_simp_tac (put_simpset atomize_ss ctxt) THEN'    (* atomize the asms *)
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   120
                  (REPEAT_DETERM o (etac conjE))        (* split the asms *)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   121
                ])
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   122
        ORELSE' resolve_tac [disjI1,disjI2]     (* "P ==> P | Q","Q ==> P | Q"*)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   123
        ORELSE' ((resolve_tac proga APPEND' hyp_resolve_tac)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   124
                 THEN' (fn _ => check_HOHH_tac2))
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   125
end;
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   126
27229
f656a12e0f4e ptac/prolog_tac: proper context;
wenzelm
parents: 27153
diff changeset
   127
fun prolog_tac ctxt prog =
f656a12e0f4e ptac/prolog_tac: proper context;
wenzelm
parents: 27153
diff changeset
   128
  check_HOHH_tac1 THEN
f656a12e0f4e ptac/prolog_tac: proper context;
wenzelm
parents: 27153
diff changeset
   129
  DEPTH_SOLVE (ptac ctxt (map check_HOHH prog) 1);
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   130
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   131
val prog_HOHH = [];
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   132
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents:
diff changeset
   133
end;