src/HOL/Prolog/HOHH.ML
changeset 9015 8006e9009621
child 13208 965f95a3abd9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Prolog/HOHH.ML	Fri Jun 02 12:44:04 2000 +0200
     1.3 @@ -0,0 +1,117 @@
     1.4 +open HOHH;
     1.5 +
     1.6 +exception not_HOHH;
     1.7 +
     1.8 +fun isD t = case t of 
     1.9 +    Const("Trueprop",_)$t     => isD t
    1.10 +  | Const("op &"  ,_)$l$r     => isD l andalso isD r
    1.11 +  | Const("op -->",_)$l$r     => isG l andalso isD r
    1.12 +  | Const(   "==>",_)$l$r     => isG l andalso isD r
    1.13 +  | Const("All",_)$Abs(s,_,t) => isD t
    1.14 +  | Const("all",_)$Abs(s,_,t) => isD t
    1.15 +  | Const("op |",_)$_$_       => false
    1.16 +  | Const("Ex" ,_)$_          => false
    1.17 +  | Const("not",_)$_          => false
    1.18 +  | Const("True",_)           => false
    1.19 +  | Const("False",_)          => false
    1.20 +  | l $ r                     => isD l
    1.21 +  | Const _ (* rigid atom *)  => true
    1.22 +  | Bound _ (* rigid atom *)  => true
    1.23 +  | Free  _ (* rigid atom *)  => true
    1.24 +  | _    (* flexible atom,
    1.25 +	    anything else *)  => false
    1.26 +and
    1.27 +    isG t = case t of
    1.28 +    Const("Trueprop",_)$t     => isG t
    1.29 +  | Const("op &"  ,_)$l$r     => isG l andalso isG r
    1.30 +  | Const("op |"  ,_)$l$r     => isG l andalso isG r
    1.31 +  | Const("op -->",_)$l$r     => isD l andalso isG r
    1.32 +  | Const(   "==>",_)$l$r     => isD l andalso isG r
    1.33 +  | Const("All",_)$Abs(_,_,t) => isG t
    1.34 +  | Const("all",_)$Abs(_,_,t) => isG t
    1.35 +  | Const("Ex" ,_)$Abs(_,_,t) => isG t
    1.36 +  | Const("True",_)           => true
    1.37 +  | Const("not",_)$_          => false
    1.38 +  | Const("False",_)          => false
    1.39 +  | _ (* atom *)	      => true;
    1.40 +
    1.41 +val check_HOHH_tac1 = PRIMITIVE (fn thm => 
    1.42 +	if isG (concl_of thm) then thm else raise not_HOHH);
    1.43 +val check_HOHH_tac2 = PRIMITIVE (fn thm => 
    1.44 +	if forall isG (prems_of thm) then thm else raise not_HOHH);
    1.45 +fun check_HOHH thm  = (if isD (concl_of thm) andalso forall isG (prems_of thm) 
    1.46 +			then thm else raise not_HOHH);
    1.47 +
    1.48 +fun atomizeD thm = let 
    1.49 +    fun at  thm = case concl_of thm of
    1.50 +      _$(Const("All" ,_)$Abs(s,_,_))=> at(thm RS (read_instantiate [("x",
    1.51 +					"?"^(if s="P" then "PP" else s))] spec))
    1.52 +    | _$(Const("op &",_)$_$_)       => at(thm RS conjunct1)@at(thm RS conjunct2)
    1.53 +    | _$(Const("op -->",_)$_$_)     => at(thm RS mp)
    1.54 +    | _				    => [thm]
    1.55 +in map zero_var_indexes (at thm) end;
    1.56 +
    1.57 +val atomize_ss = empty_ss setmksimps (mksimps mksimps_pairs) addsimps [
    1.58 +	all_conj_distrib, (* "(! x. P x & Q x) = ((! x. P x) & (! x. Q x))" *)
    1.59 +	imp_conjL RS sym, (* "(D :- G1 :- G2) = (D :- G1 & G2)" *)
    1.60 +	imp_conjR,	  (* "(D1 & D2 :- G) = ((D1 :- G) & (D2 :- G))" *)
    1.61 +	imp_all];	  (* "((!x. D) :- G) = (!x. D :- G)" *)
    1.62 +
    1.63 +(*val hyp_resolve_tac = METAHYPS (fn prems => 
    1.64 +				  resolve_tac (flat (map atomizeD prems)) 1);
    1.65 +  -- is nice, but cannot instantiate unknowns in the assumptions *)
    1.66 +fun hyp_resolve_tac i st = let
    1.67 +	fun ap (Const("All",_)$Abs(_,_,t))=(case ap t of (k,a,t) => (k+1,a  ,t))
    1.68 +	|   ap (Const("op -->",_)$_$t)    =(case ap t of (k,_,t) => (k,true ,t))
    1.69 +	|   ap t			  = 			    (0,false,t);
    1.70 +(*
    1.71 +	fun rep_goal (Const ("all",_)$Abs (_,_,t)) = rep_goal t
    1.72 +	|   rep_goal (Const ("==>",_)$s$t)	   = 
    1.73 +			(case rep_goal t of (l,t) => (s::l,t))
    1.74 +	|   rep_goal t				   = ([]  ,t);
    1.75 +	val (prems, Const("Trueprop", _)$concl) = rep_goal 
    1.76 +						(#3(dest_state (st,i)));
    1.77 +*)
    1.78 +	val subgoal = #3(dest_state (st,i));
    1.79 +	val prems = Logic.strip_assums_hyp subgoal;
    1.80 +	val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal);
    1.81 +	fun drot_tac k i = DETERM (rotate_tac k i);
    1.82 +	fun spec_tac 0 i = all_tac
    1.83 +	|   spec_tac k i = EVERY' [dtac spec, drot_tac ~1, spec_tac (k-1)] i;
    1.84 +	fun dup_spec_tac k i = if k = 0 then all_tac else EVERY'
    1.85 +		      [DETERM o (etac all_dupE), drot_tac ~2, spec_tac (k-1)] i;
    1.86 +	fun same_head _ (Const (x,_)) (Const (y,_)) = x = y
    1.87 +	|   same_head k (s$_)         (t$_)	    = same_head k s t
    1.88 +	|   same_head k (Bound i)     (Bound j)	    = i = j + k
    1.89 +	|   same_head _ _             _             = true;
    1.90 +	fun mapn f n []      = []
    1.91 +	|   mapn f n (x::xs) = f n x::mapn f (n+1) xs;
    1.92 +	fun pres_tac (k,arrow,t) n i = drot_tac n i THEN (
    1.93 +		if same_head k t concl
    1.94 +		then dup_spec_tac k i THEN 
    1.95 +		     (if arrow then etac mp i THEN drot_tac (~n) i else atac i)
    1.96 +		else no_tac);
    1.97 +	val ptacs = mapn (fn n => fn t => 
    1.98 +			  pres_tac (ap (HOLogic.dest_Trueprop t)) n i) 0 prems;
    1.99 +	in foldl (op APPEND) (no_tac, ptacs) st end;
   1.100 +
   1.101 +fun ptac prog = let
   1.102 +  val proga = flat (map atomizeD prog)			(* atomize the prog *)
   1.103 +  in	(REPEAT_DETERM1 o FIRST' [
   1.104 +		rtac TrueI,			(* "True" *)
   1.105 +		rtac conjI,			(* "[| P; Q |] ==> P & Q" *)
   1.106 +		rtac allI,			(* "(!!x. P x) ==> ! x. P x" *)
   1.107 +		rtac exI,			(* "P x ==> ? x. P x" *)
   1.108 +		rtac impI THEN'			(* "(P ==> Q) ==> P --> Q" *)
   1.109 +		  asm_full_simp_tac atomize_ss THEN'	(* atomize the asms *)
   1.110 +		  (REPEAT_DETERM o (etac conjE))	(* split the asms *)
   1.111 +		]) 
   1.112 +	ORELSE' resolve_tac [disjI1,disjI2]	(* "P ==> P | Q","Q ==> P | Q"*)
   1.113 +	ORELSE' ((resolve_tac proga APPEND' hyp_resolve_tac)
   1.114 +		 THEN' (fn _ => check_HOHH_tac2))
   1.115 +end;
   1.116 +
   1.117 +fun prolog_tac prog = check_HOHH_tac1 THEN 
   1.118 +		      DEPTH_SOLVE (ptac (map check_HOHH prog) 1);
   1.119 +
   1.120 +val prog_HOHH = [];