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