src/HOL/Tools/res_hol_clause.ML
author haftmann
Fri Dec 16 09:00:11 2005 +0100 (2005-12-16)
changeset 18418 bf448d999b7e
parent 18356 443717b3a9ad
child 18440 72ee07f4b56b
permissions -rw-r--r--
re-arranged tuples (theory * 'a) to ('a * theory) in Pure
     1 (* ID: $Id$ 
     2    Author: Jia Meng, NICTA
     3 
     4 FOL clauses translated from HOL formulae.  Combinators are used to represent lambda terms.
     5 
     6 *)
     7 
     8 structure ResHolClause =
     9 
    10 struct
    11 
    12 
    13 val include_combS = ref false;
    14 val include_min_comb = ref false;
    15 
    16 val const_typargs = ref (Library.K [] : (string*typ -> typ list));
    17 
    18 fun init thy = (include_combS:=false;include_min_comb:=false;const_typargs := Sign.const_typargs thy);
    19 
    20 (**********************************************************************)
    21 (* convert a Term.term with lambdas into a Term.term with combinators *) 
    22 (**********************************************************************)
    23 
    24 fun is_free (Bound(a)) n = (a = n)
    25   | is_free (Abs(x,_,b)) n = (is_free b (n+1))
    26   | is_free (P $ Q) n = ((is_free P n) orelse (is_free Q n))
    27   | is_free _ _ = false;
    28 
    29 
    30 exception LAM2COMB of term;
    31 
    32 exception BND of term;
    33 
    34 fun decre_bndVar (Bound n) = Bound (n-1)
    35   | decre_bndVar (P $ Q) = (decre_bndVar P) $ (decre_bndVar Q)
    36   | decre_bndVar t =
    37     case t of Const(_,_) => t
    38 	    | Free(_,_) => t
    39 	    | Var(_,_) => t
    40 	    | Abs(_,_,_) => raise BND(t); (*should not occur*)
    41 
    42 
    43 (*******************************************)
    44 fun lam2comb (Abs(x,tp,Bound 0)) _ = 
    45     let val tpI = Type("fun",[tp,tp])
    46     in 
    47 	include_min_comb:=true;
    48 	Const("COMBI",tpI) 
    49     end
    50   | lam2comb (Abs(x,tp,Bound n)) Bnds = 
    51     let val (Bound n') = decre_bndVar (Bound n)
    52 	val tb = List.nth(Bnds,n')
    53 	val tK = Type("fun",[tb,Type("fun",[tp,tb])])
    54     in
    55 	include_min_comb:=true;
    56 	Const("COMBK",tK) $ (Bound n')
    57     end
    58   | lam2comb (Abs(x,t1,Const(c,t2))) _ = 
    59     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
    60     in 
    61 	include_min_comb:=true;
    62 	Const("COMBK",tK) $ Const(c,t2) 
    63     end
    64   | lam2comb (Abs(x,t1,Free(v,t2))) _ =
    65     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
    66     in
    67 	include_min_comb:=true;
    68 	Const("COMBK",tK) $ Free(v,t2)
    69     end
    70   | lam2comb (Abs(x,t1,Var(ind,t2))) _=
    71     let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
    72     in
    73 	include_min_comb:=true;
    74 	Const("COMBK",tK) $ Var(ind,t2)
    75     end
    76   | lam2comb (t as (Abs(x,t1,P$(Bound 0)))) Bnds =
    77     let val nfreeP = not(is_free P 0)
    78 	val tr = Term.type_of1(t1::Bnds,P)
    79     in
    80 	if nfreeP then (decre_bndVar P)
    81 	else (
    82 	      let val tI = Type("fun",[t1,t1])
    83 		  val P' = lam2comb (Abs(x,t1,P)) Bnds
    84 		  val tp' = Term.type_of1(Bnds,P')
    85 		  val tS = Type("fun",[tp',Type("fun",[tI,tr])])
    86 	      in
    87 		  include_min_comb:=true;
    88 		  include_combS:=true;
    89 		  Const("COMBS",tS) $ P' $ Const("COMBI",tI)
    90 	      end)
    91     end
    92 	    
    93   | lam2comb (t as (Abs(x,t1,P$Q))) Bnds =
    94     let val (nfreeP,nfreeQ) = (not(is_free P 0),not(is_free Q 0))
    95 	val tpq = Term.type_of1(t1::Bnds, P$Q) 
    96     in
    97 	if(nfreeP andalso nfreeQ) then (
    98 	    let val tK = Type("fun",[tpq,Type("fun",[t1,tpq])])
    99 		val PQ' = decre_bndVar(P $ Q)
   100 	    in 
   101 		include_min_comb:=true;
   102 		Const("COMBK",tK) $ PQ'
   103 	    end)
   104 	else (
   105 	      if nfreeP then (
   106 			       let val Q' = lam2comb (Abs(x,t1,Q)) Bnds
   107 				   val P' = decre_bndVar P
   108 				   val tp = Term.type_of1(Bnds,P')
   109 				   val tq' = Term.type_of1(Bnds, Q')
   110 				   val tB = Type("fun",[tp,Type("fun",[tq',Type("fun",[t1,tpq])])])
   111 			       in
   112 				   include_min_comb:=true;
   113 				   Const("COMBB",tB) $ P' $ Q' 
   114 			       end)
   115 	      else (
   116 		    if nfreeQ then (
   117 				    let val P' = lam2comb (Abs(x,t1,P)) Bnds
   118 					val Q' = decre_bndVar Q
   119 					val tq = Term.type_of1(Bnds,Q')
   120 					val tp' = Term.type_of1(Bnds, P')
   121 					val tC = Type("fun",[tp',Type("fun",[tq,Type("fun",[t1,tpq])])])
   122 				    in
   123 					include_min_comb:=true;
   124 					Const("COMBC",tC) $ P' $ Q'
   125 				    end)
   126 		    else(
   127 			 let val P' = lam2comb (Abs(x,t1,P)) Bnds
   128 			     val Q' = lam2comb (Abs(x,t1,Q)) Bnds
   129 			     val tp' = Term.type_of1(Bnds,P')
   130 			     val tq' = Term.type_of1(Bnds,Q')
   131 			     val tS = Type("fun",[tp',Type("fun",[tq',Type("fun",[t1,tpq])])])
   132 			 in
   133 			     include_min_comb:=true;
   134 			     include_combS:=true;
   135 			     Const("COMBS",tS) $ P' $ Q'
   136 			 end)))
   137     end
   138   | lam2comb (t as (Abs(x,t1,_))) _ = raise LAM2COMB (t);
   139 
   140 	     
   141 
   142 (*********************)
   143 
   144 fun to_comb (Abs(x,tp,b)) Bnds =
   145     let val b' = to_comb b (tp::Bnds)
   146     in lam2comb (Abs(x,tp,b')) Bnds end
   147   | to_comb (P $ Q) Bnds = ((to_comb P Bnds) $ (to_comb Q Bnds))
   148   | to_comb t _ = t;
   149  
   150     
   151 fun comb_of t = to_comb t [];
   152 
   153 
   154 (* print a term containing combinators, used for debugging *)
   155 exception TERM_COMB of term;
   156 
   157 fun string_of_term (Const(c,t)) = c
   158   | string_of_term (Free(v,t)) = v
   159   | string_of_term (Var((x,n),t)) =
   160     let val xn = x ^ "_" ^ (string_of_int n)
   161     in xn end
   162   | string_of_term (P $ Q) =
   163     let val P' = string_of_term P
   164 	val Q' = string_of_term Q
   165     in
   166 	"(" ^ P' ^ " " ^ Q' ^ ")" end
   167   | string_of_term t =  raise TERM_COMB (t);
   168 
   169 
   170 
   171 (******************************************************)
   172 (* data types for typed combinator expressions        *)
   173 (******************************************************)
   174 
   175 type axiom_name = string;
   176 datatype kind = Axiom | Conjecture;
   177 fun name_of_kind Axiom = "axiom"
   178   | name_of_kind Conjecture = "conjecture";
   179 
   180 type polarity = bool;
   181 type indexname = Term.indexname;
   182 type clause_id = int;
   183 type csort = Term.sort;
   184 type ctyp = string;
   185 
   186 type ctyp_var = ResClause.typ_var;
   187 
   188 type ctype_literal = ResClause.type_literal;
   189 
   190 
   191 datatype combterm = CombConst of string * ctyp * ctyp list
   192 		  | CombFree of string * ctyp
   193 		  | CombVar of string * ctyp
   194 		  | CombApp of combterm * combterm * ctyp
   195 		  | Bool of combterm
   196 		  | Equal of combterm * combterm;
   197 datatype literal = Literal of polarity * combterm;
   198 
   199 
   200 
   201 datatype clause = 
   202 	 Clause of {clause_id: clause_id,
   203 		    axiom_name: axiom_name,
   204 		    kind: kind,
   205 		    literals: literal list,
   206 		    ctypes_sorts: (ctyp_var * csort) list, 
   207                     ctvar_type_literals: ctype_literal list, 
   208                     ctfree_type_literals: ctype_literal list};
   209 
   210 
   211 
   212 fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
   213 fun get_axiomName (Clause cls) = #axiom_name cls;
   214 fun get_clause_id (Clause cls) = #clause_id cls;
   215 
   216 
   217 
   218 
   219 (*********************************************************************)
   220 (* convert a clause with type Term.term to a clause with type clause *)
   221 (*********************************************************************)
   222 
   223 fun isFalse (Literal(pol,Bool(CombConst(c,_,_)))) =
   224     (pol andalso c = "c_False") orelse
   225     (not pol andalso c = "c_True")
   226   | isFalse _ = false;
   227 
   228 
   229 fun isTrue (Literal (pol,Bool(CombConst(c,_,_)))) =
   230       (pol andalso c = "c_True") orelse
   231       (not pol andalso c = "c_False")
   232   | isTrue _ = false;
   233   
   234 fun isTaut (Clause {literals,...}) = exists isTrue literals;  
   235 
   236 
   237 
   238 fun make_clause(clause_id,axiom_name,kind,literals,ctypes_sorts,ctvar_type_literals,ctfree_type_literals) =
   239     if forall isFalse literals
   240     then error "Problem too trivial for resolution (empty clause)"
   241     else
   242 	Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind,
   243 		literals = literals, ctypes_sorts = ctypes_sorts, 
   244 		ctvar_type_literals = ctvar_type_literals,
   245 		ctfree_type_literals = ctfree_type_literals};
   246 
   247 (* convert a Term.type to a string; gather sort information of type variables *)
   248 fun type_of (Type (a, [])) = (ResClause.make_fixed_type_const a,[])
   249   | type_of (Type (a, Ts)) = 
   250     let val typ_ts = map type_of Ts
   251 	val (typs,tsorts) = ListPair.unzip typ_ts
   252 	val ts = ResClause.union_all tsorts
   253 	val t = ResClause.make_fixed_type_const a
   254     in
   255 	(t ^ (ResClause.paren_pack typs),ts)
   256     end
   257   | type_of (tp as (TFree (a,s))) = (ResClause.make_fixed_type_var a,[ResClause.mk_typ_var_sort tp])
   258   | type_of (tp as (TVar (v,s))) = (ResClause.make_schematic_type_var v,[ResClause.mk_typ_var_sort tp]);
   259 
   260 
   261 
   262 (* same as above, but no gathering of sort information *)
   263 fun simp_type_of (Type (a, [])) = ResClause.make_fixed_type_const a
   264   | simp_type_of (Type (a, Ts)) = 
   265     let val typs = map simp_type_of Ts
   266 	val t = ResClause.make_fixed_type_const a
   267     in
   268 	t ^ ResClause.paren_pack typs
   269     end
   270   | simp_type_of (TFree (a,s)) = ResClause.make_fixed_type_var a
   271   | simp_type_of (TVar (v,s)) = ResClause.make_schematic_type_var v;
   272 
   273 
   274 fun comb_typ ("COMBI",t) = 
   275     let val t' = domain_type t
   276     in
   277 	[simp_type_of t']
   278     end
   279   | comb_typ ("COMBK",t) = 
   280     let val (ab,_) = strip_type t
   281     in
   282 	map simp_type_of ab
   283     end
   284   | comb_typ ("COMBS",t) = 
   285     let val t' = domain_type t
   286 	val ([a,b],c) = strip_type t' 
   287     in 
   288 	map simp_type_of [a,b,c]
   289     end
   290   | comb_typ ("COMBB",t) = 
   291     let val ([ab,ca,c],b) = strip_type t
   292 	val a = domain_type ab
   293     in
   294 	map simp_type_of [a,b,c]
   295     end
   296   | comb_typ ("COMBC",t) =
   297     let val t1 = domain_type t
   298 	val ([a,b],c) = strip_type t1
   299     in
   300 	map simp_type_of [a,b,c]
   301     end;
   302 
   303 fun const_type_of ("COMBI",t) = 
   304     let val (tp,ts) = type_of t
   305 	val I_var = comb_typ ("COMBI",t)
   306     in
   307 	(tp,ts,I_var)
   308     end
   309   | const_type_of ("COMBK",t) =
   310     let val (tp,ts) = type_of t
   311 	val K_var = comb_typ ("COMBK",t)
   312     in
   313 	(tp,ts,K_var)
   314     end
   315   | const_type_of ("COMBS",t) =
   316     let val (tp,ts) = type_of t
   317 	val S_var = comb_typ ("COMBS",t)
   318     in
   319 	(tp,ts,S_var)
   320     end
   321   | const_type_of ("COMBB",t) =
   322     let val (tp,ts) = type_of t
   323 	val B_var = comb_typ ("COMBB",t)
   324     in
   325 	(tp,ts,B_var)
   326     end
   327   | const_type_of ("COMBC",t) =
   328     let val (tp,ts) = type_of t
   329 	val C_var = comb_typ ("COMBC",t)
   330     in
   331 	(tp,ts,C_var)
   332     end
   333   | const_type_of (c,t) =
   334     let val (tp,ts) = type_of t
   335 	val tvars = !const_typargs(c,t)
   336 	val tvars' = map simp_type_of tvars
   337     in
   338 	(tp,ts,tvars')
   339     end;
   340 
   341 fun is_bool_type (Type("bool",[])) = true
   342   | is_bool_type _ = false;
   343 
   344 
   345 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   346 fun combterm_of (Const(c,t)) =
   347     let val (tp,ts,tvar_list) = const_type_of (c,t)
   348 	val is_bool = is_bool_type t
   349 	val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
   350 	val c'' = if is_bool then Bool(c') else c'
   351     in
   352 	(c'',ts)
   353     end
   354   | combterm_of (Free(v,t)) =
   355     let val (tp,ts) = type_of t
   356 	val is_bool = is_bool_type t
   357 	val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
   358 		 else CombFree(ResClause.make_fixed_var v,tp)
   359 	val v'' = if is_bool then Bool(v') else v'
   360     in
   361 	(v'',ts)
   362     end
   363   | combterm_of (Var(v,t)) =
   364     let val (tp,ts) = type_of t
   365 	val is_bool = is_bool_type t
   366 	val v' = CombVar(ResClause.make_schematic_var v,tp)
   367 	val v'' = if is_bool then Bool(v') else v'
   368     in
   369 	(v'',ts)
   370     end
   371   | combterm_of (Const("op =",T) $ P $ Q) = (*FIXME: allow equal between bools?*)
   372     let val (P',tsP) = combterm_of P        
   373 	val (Q',tsQ) = combterm_of Q
   374     in
   375 	(Equal(P',Q'),tsP union tsQ)
   376     end
   377   | combterm_of (t as (P $ Q)) =
   378     let val (P',tsP) = combterm_of P
   379 	val (Q',tsQ) = combterm_of Q
   380 	val tp = Term.type_of t
   381 	val tp' = simp_type_of tp
   382 	val is_bool = is_bool_type tp
   383 	val t' = CombApp(P',Q',tp')
   384 	val t'' = if is_bool then Bool(t') else t'
   385     in
   386 	(t'',tsP union tsQ)
   387     end;
   388 
   389 fun predicate_of ((Const("Not",_) $ P), polarity) =
   390     predicate_of (P, not polarity)
   391   | predicate_of (term,polarity) = (combterm_of term,polarity);
   392 
   393 
   394 fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
   395   | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
   396     let val args' = literals_of_term1 args P
   397     in
   398 	literals_of_term1 args' Q
   399     end
   400   | literals_of_term1 (lits,ts) P =
   401     let val ((pred,ts'),pol) = predicate_of (P,true)
   402 	val lits' = Literal(pol,pred)::lits
   403     in
   404 	(lits',ts union ts')
   405     end;
   406 
   407 
   408 fun literals_of_term P = literals_of_term1 ([],[]) P;
   409 
   410 
   411 (* making axiom and conjecture clauses *)
   412 fun make_axiom_clause term (ax_name,cls_id) =
   413     let val term' = comb_of term
   414 	val (lits,ctypes_sorts) = literals_of_term term'
   415 	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux2 ctypes_sorts
   416     in
   417 	make_clause(cls_id,ax_name,Axiom,
   418 		    lits,ctypes_sorts,ctvar_lits,ctfree_lits)
   419     end;
   420 
   421 
   422 fun make_conjecture_clause n t =
   423     let val t' = comb_of t
   424 	val (lits,ctypes_sorts) = literals_of_term t'
   425 	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux2 ctypes_sorts
   426     in
   427 	make_clause(n,"conjecture",Conjecture,lits,ctypes_sorts,ctvar_lits,ctfree_lits)
   428     end;
   429 
   430 
   431 
   432 fun make_conjecture_clauses_aux _ [] = []
   433   | make_conjecture_clauses_aux n (t::ts) =
   434     make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts;
   435 
   436 val make_conjecture_clauses = make_conjecture_clauses_aux 0;
   437 
   438 
   439 (**********************************************************************)
   440 (* convert clause into ATP specific formats:                          *)
   441 (* TPTP used by Vampire and E                                         *)
   442 (**********************************************************************)
   443 
   444 val type_wrapper = "typeinfo";
   445 
   446 datatype type_level = T_FULL | T_PARTIAL | T_CONST | T_NONE;
   447 
   448 val typ_level = ref T_PARTIAL;
   449 
   450 fun full_types () = (typ_level:=T_FULL);
   451 fun partial_types () = (typ_level:=T_PARTIAL);
   452 fun const_types_only () = (typ_level:=T_CONST);
   453 fun no_types () = (typ_level:=T_NONE);
   454 
   455 
   456 fun find_typ_level () = !typ_level;
   457 
   458 fun wrap_type (c,t) = 
   459     case !typ_level of T_FULL => type_wrapper ^ (ResClause.paren_pack [c,t])
   460 		     | _ => c;
   461     
   462 
   463 val bool_tp = ResClause.make_fixed_type_const "bool";
   464 
   465 val app_str = "hAPP";
   466 
   467 val bool_str = "hBOOL";
   468 
   469 exception STRING_OF_COMBTERM of int;
   470 
   471 (* convert literals of clauses into strings *)
   472 fun string_of_combterm1_aux _ (CombConst(c,tp,_)) = (wrap_type (c,tp),tp)
   473   | string_of_combterm1_aux _ (CombFree(v,tp)) = (wrap_type (v,tp),tp)
   474   | string_of_combterm1_aux _ (CombVar(v,tp)) = (wrap_type (v,tp),tp)
   475   | string_of_combterm1_aux is_pred (CombApp(t1,t2,tp)) =
   476     let val (s1,tp1) = string_of_combterm1_aux is_pred t1
   477 	val (s2,tp2) = string_of_combterm1_aux is_pred t2
   478 	val r =	case !typ_level of T_FULL => type_wrapper ^  (ResClause.paren_pack [(app_str ^ (ResClause.paren_pack [s1,s2])),tp])
   479 				 | T_PARTIAL => app_str ^ (ResClause.paren_pack [s1,s2,tp1])
   480 				 | T_NONE => app_str ^ (ResClause.paren_pack [s1,s2])
   481 				 | T_CONST => raise STRING_OF_COMBTERM (1) (*should not happen, if happened may be a bug*)
   482     in
   483 	(r,tp)
   484 
   485     end
   486   | string_of_combterm1_aux is_pred (Bool(t)) = 
   487     let val (t',_) = string_of_combterm1_aux false t
   488 	val r = if is_pred then bool_str ^ (ResClause.paren_pack [t'])
   489 		else t'
   490     in
   491 	(r,bool_tp)
   492     end
   493   | string_of_combterm1_aux _ (Equal(t1,t2)) =
   494     let val (s1,_) = string_of_combterm1_aux false t1
   495 	val (s2,_) = string_of_combterm1_aux false t2
   496     in
   497 	("equal" ^ (ResClause.paren_pack [s1,s2]),bool_tp) 
   498     end;
   499 
   500 fun string_of_combterm1 is_pred term = fst (string_of_combterm1_aux is_pred term);
   501 
   502 fun string_of_combterm2 _ (CombConst(c,tp,tvars)) = c ^ (ResClause.paren_pack tvars)
   503   | string_of_combterm2 _ (CombFree(v,tp)) = v
   504   | string_of_combterm2 _ (CombVar(v,tp)) = v
   505   | string_of_combterm2 is_pred (CombApp(t1,t2,tp)) =
   506     let val s1 = string_of_combterm2 is_pred t1
   507 	val s2 = string_of_combterm2 is_pred t2
   508     in
   509 	app_str ^ (ResClause.paren_pack [s1,s2])
   510     end
   511   | string_of_combterm2 is_pred (Bool(t)) = 
   512     let val t' = string_of_combterm2 false t
   513     in
   514 	if is_pred then bool_str ^ (ResClause.paren_pack [t'])
   515 	else t'
   516     end
   517   | string_of_combterm2 _ (Equal(t1,t2)) =
   518     let val s1 = string_of_combterm2 false t1
   519 	val s2 = string_of_combterm2 false t2
   520     in
   521 	("equal" ^ (ResClause.paren_pack [s1,s2])) 
   522     end;
   523 
   524 
   525 
   526 fun string_of_combterm is_pred term = 
   527     case !typ_level of T_CONST => string_of_combterm2 is_pred term
   528 		     | _ => string_of_combterm1 is_pred term;
   529 
   530 
   531 fun string_of_clausename (cls_id,ax_name) = 
   532     ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   533 
   534 fun string_of_type_clsname (cls_id,ax_name,idx) = 
   535     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   536 
   537 
   538 fun tptp_literal (Literal(pol,pred)) =
   539     let val pred_string = string_of_combterm true pred
   540 	val pol_str = if pol then "++" else "--"
   541     in
   542 	pol_str ^ pred_string
   543     end;
   544 
   545  
   546 fun tptp_type_lits (Clause cls) = 
   547     let val lits = map tptp_literal (#literals cls)
   548 	val ctvar_lits_strs =
   549 	    case !typ_level of T_NONE => []
   550 			     | _ => (map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)) 
   551 	val ctfree_lits = 
   552 	    case !typ_level of T_NONE => []
   553 			     | _ => (map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)) 
   554     in
   555 	(ctvar_lits_strs @ lits, ctfree_lits)
   556     end; 
   557     
   558     
   559 fun clause2tptp cls =
   560     let val (lits,ctfree_lits) = tptp_type_lits cls
   561 	val cls_id = get_clause_id cls
   562 	val ax_name = get_axiomName cls
   563 	val knd = string_of_kind cls
   564 	val lits_str = ResClause.bracket_pack lits
   565 	val cls_str = ResClause.gen_tptp_cls(cls_id,ax_name,knd,lits_str)
   566     in
   567 	(cls_str,ctfree_lits)
   568     end;
   569 
   570 
   571 end