src/HOL/Prolog/HOHH.ML
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 15570 8d8c70b41bab
child 17311 5b1d47d920ce
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
oheimb@13208
     1
(*  Title:    HOL/Prolog/HOHH.ML
oheimb@13208
     2
    ID:       $Id$
oheimb@13208
     3
    Author:   David von Oheimb (based on a lecture on Lambda Prolog by Nadathur)
oheimb@13208
     4
*)
oheimb@13208
     5
oheimb@9015
     6
open HOHH;
oheimb@9015
     7
oheimb@9015
     8
exception not_HOHH;
oheimb@9015
     9
oheimb@9015
    10
fun isD t = case t of 
oheimb@9015
    11
    Const("Trueprop",_)$t     => isD t
oheimb@9015
    12
  | Const("op &"  ,_)$l$r     => isD l andalso isD r
oheimb@9015
    13
  | Const("op -->",_)$l$r     => isG l andalso isD r
oheimb@9015
    14
  | Const(   "==>",_)$l$r     => isG l andalso isD r
oheimb@9015
    15
  | Const("All",_)$Abs(s,_,t) => isD t
oheimb@9015
    16
  | Const("all",_)$Abs(s,_,t) => isD t
oheimb@9015
    17
  | Const("op |",_)$_$_       => false
oheimb@9015
    18
  | Const("Ex" ,_)$_          => false
oheimb@9015
    19
  | Const("not",_)$_          => false
oheimb@9015
    20
  | Const("True",_)           => false
oheimb@9015
    21
  | Const("False",_)          => false
oheimb@9015
    22
  | l $ r                     => isD l
oheimb@9015
    23
  | Const _ (* rigid atom *)  => true
oheimb@9015
    24
  | Bound _ (* rigid atom *)  => true
oheimb@9015
    25
  | Free  _ (* rigid atom *)  => true
oheimb@9015
    26
  | _    (* flexible atom,
oheimb@9015
    27
	    anything else *)  => false
oheimb@9015
    28
and
oheimb@9015
    29
    isG t = case t of
oheimb@9015
    30
    Const("Trueprop",_)$t     => isG t
oheimb@9015
    31
  | Const("op &"  ,_)$l$r     => isG l andalso isG r
oheimb@9015
    32
  | Const("op |"  ,_)$l$r     => isG l andalso isG r
oheimb@9015
    33
  | Const("op -->",_)$l$r     => isD l andalso isG r
oheimb@9015
    34
  | Const(   "==>",_)$l$r     => isD l andalso isG r
oheimb@9015
    35
  | Const("All",_)$Abs(_,_,t) => isG t
oheimb@9015
    36
  | Const("all",_)$Abs(_,_,t) => isG t
oheimb@9015
    37
  | Const("Ex" ,_)$Abs(_,_,t) => isG t
oheimb@9015
    38
  | Const("True",_)           => true
oheimb@9015
    39
  | Const("not",_)$_          => false
oheimb@9015
    40
  | Const("False",_)          => false
oheimb@9015
    41
  | _ (* atom *)	      => true;
oheimb@9015
    42
oheimb@9015
    43
val check_HOHH_tac1 = PRIMITIVE (fn thm => 
oheimb@9015
    44
	if isG (concl_of thm) then thm else raise not_HOHH);
oheimb@9015
    45
val check_HOHH_tac2 = PRIMITIVE (fn thm => 
oheimb@9015
    46
	if forall isG (prems_of thm) then thm else raise not_HOHH);
oheimb@9015
    47
fun check_HOHH thm  = (if isD (concl_of thm) andalso forall isG (prems_of thm) 
oheimb@9015
    48
			then thm else raise not_HOHH);
oheimb@9015
    49
oheimb@9015
    50
fun atomizeD thm = let 
oheimb@9015
    51
    fun at  thm = case concl_of thm of
oheimb@9015
    52
      _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (read_instantiate [("x",
oheimb@9015
    53
					"?"^(if s="P" then "PP" else s))] spec))
oheimb@9015
    54
    | _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
oheimb@9015
    55
    | _$(Const("op -->",_)$_$_)     => at(thm RS mp)
oheimb@9015
    56
    | _				    => [thm]
oheimb@9015
    57
in map zero_var_indexes (at thm) end;
oheimb@9015
    58
oheimb@9015
    59
val atomize_ss = empty_ss setmksimps (mksimps mksimps_pairs) addsimps [
oheimb@9015
    60
	all_conj_distrib, (* "(! x. P x & Q x) = ((! x. P x) & (! x. Q x))" *)
oheimb@9015
    61
	imp_conjL RS sym, (* "(D :- G1 :- G2) = (D :- G1 & G2)" *)
oheimb@9015
    62
	imp_conjR,	  (* "(D1 & D2 :- G) = ((D1 :- G) & (D2 :- G))" *)
oheimb@9015
    63
	imp_all];	  (* "((!x. D) :- G) = (!x. D :- G)" *)
oheimb@9015
    64
oheimb@9015
    65
(*val hyp_resolve_tac = METAHYPS (fn prems => 
skalberg@15570
    66
				  resolve_tac (List.concat (map atomizeD prems)) 1);
oheimb@9015
    67
  -- is nice, but cannot instantiate unknowns in the assumptions *)
oheimb@9015
    68
fun hyp_resolve_tac i st = let
oheimb@9015
    69
	fun ap (Const("All",_)$Abs(_,_,t))=(case ap t of (k,a,t) => (k+1,a  ,t))
oheimb@9015
    70
	|   ap (Const("op -->",_)$_$t)    =(case ap t of (k,_,t) => (k,true ,t))
oheimb@9015
    71
	|   ap t			  = 			    (0,false,t);
oheimb@9015
    72
(*
oheimb@9015
    73
	fun rep_goal (Const ("all",_)$Abs (_,_,t)) = rep_goal t
oheimb@9015
    74
	|   rep_goal (Const ("==>",_)$s$t)	   = 
oheimb@9015
    75
			(case rep_goal t of (l,t) => (s::l,t))
oheimb@9015
    76
	|   rep_goal t				   = ([]  ,t);
oheimb@9015
    77
	val (prems, Const("Trueprop", _)$concl) = rep_goal 
oheimb@9015
    78
						(#3(dest_state (st,i)));
oheimb@9015
    79
*)
oheimb@9015
    80
	val subgoal = #3(dest_state (st,i));
oheimb@9015
    81
	val prems = Logic.strip_assums_hyp subgoal;
oheimb@9015
    82
	val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal);
oheimb@9015
    83
	fun drot_tac k i = DETERM (rotate_tac k i);
oheimb@9015
    84
	fun spec_tac 0 i = all_tac
oheimb@9015
    85
	|   spec_tac k i = EVERY' [dtac spec, drot_tac ~1, spec_tac (k-1)] i;
oheimb@9015
    86
	fun dup_spec_tac k i = if k = 0 then all_tac else EVERY'
oheimb@9015
    87
		      [DETERM o (etac all_dupE), drot_tac ~2, spec_tac (k-1)] i;
oheimb@9015
    88
	fun same_head _ (Const (x,_)) (Const (y,_)) = x = y
oheimb@9015
    89
	|   same_head k (s$_)         (t$_)	    = same_head k s t
oheimb@9015
    90
	|   same_head k (Bound i)     (Bound j)	    = i = j + k
oheimb@9015
    91
	|   same_head _ _             _             = true;
oheimb@9015
    92
	fun mapn f n []      = []
oheimb@9015
    93
	|   mapn f n (x::xs) = f n x::mapn f (n+1) xs;
oheimb@9015
    94
	fun pres_tac (k,arrow,t) n i = drot_tac n i THEN (
oheimb@9015
    95
		if same_head k t concl
oheimb@9015
    96
		then dup_spec_tac k i THEN 
oheimb@9015
    97
		     (if arrow then etac mp i THEN drot_tac (~n) i else atac i)
oheimb@9015
    98
		else no_tac);
oheimb@9015
    99
	val ptacs = mapn (fn n => fn t => 
oheimb@9015
   100
			  pres_tac (ap (HOLogic.dest_Trueprop t)) n i) 0 prems;
skalberg@15570
   101
	in Library.foldl (op APPEND) (no_tac, ptacs) st end;
oheimb@9015
   102
oheimb@9015
   103
fun ptac prog = let
skalberg@15570
   104
  val proga = List.concat (map atomizeD prog)			(* atomize the prog *)
oheimb@9015
   105
  in	(REPEAT_DETERM1 o FIRST' [
oheimb@9015
   106
		rtac TrueI,			(* "True" *)
oheimb@9015
   107
		rtac conjI,			(* "[| P; Q |] ==> P & Q" *)
oheimb@9015
   108
		rtac allI,			(* "(!!x. P x) ==> ! x. P x" *)
oheimb@9015
   109
		rtac exI,			(* "P x ==> ? x. P x" *)
oheimb@9015
   110
		rtac impI THEN'			(* "(P ==> Q) ==> P --> Q" *)
oheimb@9015
   111
		  asm_full_simp_tac atomize_ss THEN'	(* atomize the asms *)
oheimb@9015
   112
		  (REPEAT_DETERM o (etac conjE))	(* split the asms *)
oheimb@9015
   113
		]) 
oheimb@9015
   114
	ORELSE' resolve_tac [disjI1,disjI2]	(* "P ==> P | Q","Q ==> P | Q"*)
oheimb@9015
   115
	ORELSE' ((resolve_tac proga APPEND' hyp_resolve_tac)
oheimb@9015
   116
		 THEN' (fn _ => check_HOHH_tac2))
oheimb@9015
   117
end;
oheimb@9015
   118
oheimb@9015
   119
fun prolog_tac prog = check_HOHH_tac1 THEN 
oheimb@9015
   120
		      DEPTH_SOLVE (ptac (map check_HOHH prog) 1);
oheimb@9015
   121
oheimb@9015
   122
val prog_HOHH = [];