src/HOL/Tools/res_hol_clause.ML
changeset 30240 5b25fee0362c
parent 28835 d4d8eba5f781
child 30242 aea5d7fa7ef5
equal deleted inserted replaced
30239:179ff9cb160b 30240:5b25fee0362c
     1 (* ID: $Id$
     1 (*
     2    Author: Jia Meng, NICTA
     2    Author: Jia Meng, NICTA
     3 
     3 
     4 FOL clauses translated from HOL formulae.
     4 FOL clauses translated from HOL formulae.
     5 *)
     5 *)
     6 
     6 
    11   val comb_K: thm
    11   val comb_K: thm
    12   val comb_B: thm
    12   val comb_B: thm
    13   val comb_C: thm
    13   val comb_C: thm
    14   val comb_S: thm
    14   val comb_S: thm
    15   datatype type_level = T_FULL | T_CONST
    15   datatype type_level = T_FULL | T_CONST
    16   val typ_level: type_level ref
    16   val typ_level: type_level
    17   val minimize_applies: bool ref
    17   val minimize_applies: bool
    18   type axiom_name = string
    18   type axiom_name = string
    19   type polarity = bool
    19   type polarity = bool
    20   type clause_id = int
    20   type clause_id = int
    21   datatype combterm =
    21   datatype combterm =
    22       CombConst of string * ResClause.fol_type * ResClause.fol_type list (*Const and Free*)
    22       CombConst of string * ResClause.fol_type * ResClause.fol_type list (*Const and Free*)
    51 
    51 
    52 
    52 
    53 (*The different translations of types*)
    53 (*The different translations of types*)
    54 datatype type_level = T_FULL | T_CONST;
    54 datatype type_level = T_FULL | T_CONST;
    55 
    55 
    56 val typ_level = ref T_CONST;
    56 val typ_level = T_CONST;
    57 
    57 
    58 (*If true, each function will be directly applied to as many arguments as possible, avoiding
    58 (*If true, each function will be directly applied to as many arguments as possible, avoiding
    59   use of the "apply" operator. Use of hBOOL is also minimized.*)
    59   use of the "apply" operator. Use of hBOOL is also minimized.*)
    60 val minimize_applies = ref true;
    60 val minimize_applies = true;
    61 
    61 
    62 val const_min_arity = ref (Symtab.empty : int Symtab.table);
    62 fun min_arity_of const_min_arity c = getOpt (Symtab.lookup const_min_arity c, 0);
    63 
       
    64 val const_needs_hBOOL = ref (Symtab.empty : bool Symtab.table);
       
    65 
       
    66 fun min_arity_of c = getOpt (Symtab.lookup(!const_min_arity) c, 0);
       
    67 
    63 
    68 (*True if the constant ever appears outside of the top-level position in literals.
    64 (*True if the constant ever appears outside of the top-level position in literals.
    69   If false, the constant always receives all of its arguments and is used as a predicate.*)
    65   If false, the constant always receives all of its arguments and is used as a predicate.*)
    70 fun needs_hBOOL c = not (!minimize_applies) orelse
    66 fun needs_hBOOL const_needs_hBOOL c = not minimize_applies orelse
    71                     getOpt (Symtab.lookup(!const_needs_hBOOL) c, false);
    67                     getOpt (Symtab.lookup const_needs_hBOOL c, false);
    72 
    68 
    73 
    69 
    74 (******************************************************)
    70 (******************************************************)
    75 (* data types for typed combinator expressions        *)
    71 (* data types for typed combinator expressions        *)
    76 (******************************************************)
    72 (******************************************************)
   108       (not pol andalso c = "c_False")
   104       (not pol andalso c = "c_False")
   109   | isTrue _ = false;
   105   | isTrue _ = false;
   110 
   106 
   111 fun isTaut (Clause {literals,...}) = exists isTrue literals;
   107 fun isTaut (Clause {literals,...}) = exists isTrue literals;
   112 
   108 
   113 fun type_of (Type (a, Ts)) =
   109 fun type_of dfg (Type (a, Ts)) =
   114       let val (folTypes,ts) = types_of Ts
   110       let val (folTypes,ts) = types_of dfg Ts
   115       in  (RC.Comp(RC.make_fixed_type_const a, folTypes), ts)  end
   111       in  (RC.Comp(RC.make_fixed_type_const dfg a, folTypes), ts)  end
   116   | type_of (tp as (TFree(a,s))) =
   112   | type_of dfg (tp as (TFree(a,s))) =
   117       (RC.AtomF (RC.make_fixed_type_var a), [tp])
   113       (RC.AtomF (RC.make_fixed_type_var a), [tp])
   118   | type_of (tp as (TVar(v,s))) =
   114   | type_of dfg (tp as (TVar(v,s))) =
   119       (RC.AtomV (RC.make_schematic_type_var v), [tp])
   115       (RC.AtomV (RC.make_schematic_type_var v), [tp])
   120 and types_of Ts =
   116 and types_of dfg Ts =
   121       let val (folTyps,ts) = ListPair.unzip (map type_of Ts)
   117       let val (folTyps,ts) = ListPair.unzip (map (type_of dfg) Ts)
   122       in  (folTyps, RC.union_all ts)  end;
   118       in  (folTyps, RC.union_all ts)  end;
   123 
   119 
   124 (* same as above, but no gathering of sort information *)
   120 (* same as above, but no gathering of sort information *)
   125 fun simp_type_of (Type (a, Ts)) =
   121 fun simp_type_of dfg (Type (a, Ts)) =
   126       RC.Comp(RC.make_fixed_type_const a, map simp_type_of Ts)
   122       RC.Comp(RC.make_fixed_type_const dfg a, map (simp_type_of dfg) Ts)
   127   | simp_type_of (TFree (a,s)) = RC.AtomF(RC.make_fixed_type_var a)
   123   | simp_type_of dfg (TFree (a,s)) = RC.AtomF(RC.make_fixed_type_var a)
   128   | simp_type_of (TVar (v,s)) = RC.AtomV(RC.make_schematic_type_var v);
   124   | simp_type_of dfg (TVar (v,s)) = RC.AtomV(RC.make_schematic_type_var v);
   129 
   125 
   130 
   126 
   131 fun const_type_of thy (c,t) =
   127 fun const_type_of dfg thy (c,t) =
   132       let val (tp,ts) = type_of t
   128       let val (tp,ts) = type_of dfg t
   133       in  (tp, ts, map simp_type_of (Sign.const_typargs thy (c,t))) end;
   129       in  (tp, ts, map (simp_type_of dfg) (Sign.const_typargs thy (c,t))) end;
   134 
   130 
   135 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   131 (* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
   136 fun combterm_of thy (Const(c,t)) =
   132 fun combterm_of dfg thy (Const(c,t)) =
   137       let val (tp,ts,tvar_list) = const_type_of thy (c,t)
   133       let val (tp,ts,tvar_list) = const_type_of dfg thy (c,t)
   138           val c' = CombConst(RC.make_fixed_const c, tp, tvar_list)
   134           val c' = CombConst(RC.make_fixed_const dfg c, tp, tvar_list)
   139       in  (c',ts)  end
   135       in  (c',ts)  end
   140   | combterm_of thy (Free(v,t)) =
   136   | combterm_of dfg thy (Free(v,t)) =
   141       let val (tp,ts) = type_of t
   137       let val (tp,ts) = type_of dfg t
   142           val v' = CombConst(RC.make_fixed_var v, tp, [])
   138           val v' = CombConst(RC.make_fixed_var v, tp, [])
   143       in  (v',ts)  end
   139       in  (v',ts)  end
   144   | combterm_of thy (Var(v,t)) =
   140   | combterm_of dfg thy (Var(v,t)) =
   145       let val (tp,ts) = type_of t
   141       let val (tp,ts) = type_of dfg t
   146           val v' = CombVar(RC.make_schematic_var v,tp)
   142           val v' = CombVar(RC.make_schematic_var v,tp)
   147       in  (v',ts)  end
   143       in  (v',ts)  end
   148   | combterm_of thy (P $ Q) =
   144   | combterm_of dfg thy (P $ Q) =
   149       let val (P',tsP) = combterm_of thy P
   145       let val (P',tsP) = combterm_of dfg thy P
   150           val (Q',tsQ) = combterm_of thy Q
   146           val (Q',tsQ) = combterm_of dfg thy Q
   151       in  (CombApp(P',Q'), tsP union tsQ)  end
   147       in  (CombApp(P',Q'), tsP union tsQ)  end
   152   | combterm_of thy (t as Abs _) = raise RC.CLAUSE("HOL CLAUSE",t);
   148   | combterm_of _ thy (t as Abs _) = raise RC.CLAUSE("HOL CLAUSE",t);
   153 
   149 
   154 fun predicate_of thy ((Const("Not",_) $ P), polarity) = predicate_of thy (P, not polarity)
   150 fun predicate_of dfg thy ((Const("Not",_) $ P), polarity) = predicate_of dfg thy (P, not polarity)
   155   | predicate_of thy (t,polarity) = (combterm_of thy (Envir.eta_contract t), polarity);
   151   | predicate_of dfg thy (t,polarity) = (combterm_of dfg thy (Envir.eta_contract t), polarity);
   156 
   152 
   157 fun literals_of_term1 thy args (Const("Trueprop",_) $ P) = literals_of_term1 thy args P
   153 fun literals_of_term1 dfg thy args (Const("Trueprop",_) $ P) = literals_of_term1 dfg thy args P
   158   | literals_of_term1 thy args (Const("op |",_) $ P $ Q) =
   154   | literals_of_term1 dfg thy args (Const("op |",_) $ P $ Q) =
   159       literals_of_term1 thy (literals_of_term1 thy args P) Q
   155       literals_of_term1 dfg thy (literals_of_term1 dfg thy args P) Q
   160   | literals_of_term1 thy (lits,ts) P =
   156   | literals_of_term1 dfg thy (lits,ts) P =
   161       let val ((pred,ts'),pol) = predicate_of thy (P,true)
   157       let val ((pred,ts'),pol) = predicate_of dfg thy (P,true)
   162       in
   158       in
   163           (Literal(pol,pred)::lits, ts union ts')
   159           (Literal(pol,pred)::lits, ts union ts')
   164       end;
   160       end;
   165 
   161 
   166 fun literals_of_term thy P = literals_of_term1 thy ([],[]) P;
   162 fun literals_of_term_dfg dfg thy P = literals_of_term1 dfg thy ([],[]) P;
       
   163 val literals_of_term = literals_of_term_dfg false;
   167 
   164 
   168 (* Problem too trivial for resolution (empty clause) *)
   165 (* Problem too trivial for resolution (empty clause) *)
   169 exception TOO_TRIVIAL;
   166 exception TOO_TRIVIAL;
   170 
   167 
   171 (* making axiom and conjecture clauses *)
   168 (* making axiom and conjecture clauses *)
   172 fun make_clause thy (clause_id,axiom_name,kind,th) =
   169 fun make_clause dfg thy (clause_id,axiom_name,kind,th) =
   173     let val (lits,ctypes_sorts) = literals_of_term thy (prop_of th)
   170     let val (lits,ctypes_sorts) = literals_of_term_dfg dfg thy (prop_of th)
   174     in
   171     in
   175         if forall isFalse lits
   172         if forall isFalse lits
   176         then raise TOO_TRIVIAL
   173         then raise TOO_TRIVIAL
   177         else
   174         else
   178             Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
   175             Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
   179                     literals = lits, ctypes_sorts = ctypes_sorts}
   176                     literals = lits, ctypes_sorts = ctypes_sorts}
   180     end;
   177     end;
   181 
   178 
   182 
   179 
   183 fun add_axiom_clause thy ((th,(name,id)), pairs) =
   180 fun add_axiom_clause dfg thy ((th,(name,id)), pairs) =
   184   let val cls = make_clause thy (id, name, RC.Axiom, th)
   181   let val cls = make_clause dfg thy (id, name, RC.Axiom, th)
   185   in
   182   in
   186       if isTaut cls then pairs else (name,cls)::pairs
   183       if isTaut cls then pairs else (name,cls)::pairs
   187   end;
   184   end;
   188 
   185 
   189 fun make_axiom_clauses thy = foldl (add_axiom_clause thy) [];
   186 fun make_axiom_clauses dfg thy = List.foldl (add_axiom_clause dfg thy) [];
   190 
   187 
   191 fun make_conjecture_clauses_aux _ _ [] = []
   188 fun make_conjecture_clauses_aux dfg _ _ [] = []
   192   | make_conjecture_clauses_aux thy n (th::ths) =
   189   | make_conjecture_clauses_aux dfg thy n (th::ths) =
   193       make_clause thy (n,"conjecture", RC.Conjecture, th) ::
   190       make_clause dfg thy (n,"conjecture", RC.Conjecture, th) ::
   194       make_conjecture_clauses_aux thy (n+1) ths;
   191       make_conjecture_clauses_aux dfg thy (n+1) ths;
   195 
   192 
   196 fun make_conjecture_clauses thy = make_conjecture_clauses_aux thy 0;
   193 fun make_conjecture_clauses dfg thy = make_conjecture_clauses_aux dfg thy 0;
   197 
   194 
   198 
   195 
   199 (**********************************************************************)
   196 (**********************************************************************)
   200 (* convert clause into ATP specific formats:                          *)
   197 (* convert clause into ATP specific formats:                          *)
   201 (* TPTP used by Vampire and E                                         *)
   198 (* TPTP used by Vampire and E                                         *)
   216         |   stripc  x =  x
   213         |   stripc  x =  x
   217     in  stripc(u,[])  end;
   214     in  stripc(u,[])  end;
   218 
   215 
   219 val type_wrapper = "ti";
   216 val type_wrapper = "ti";
   220 
   217 
   221 fun head_needs_hBOOL (CombConst(c,_,_)) = needs_hBOOL c
   218 fun head_needs_hBOOL const_needs_hBOOL (CombConst(c,_,_)) = needs_hBOOL const_needs_hBOOL c
   222   | head_needs_hBOOL _ = true;
   219   | head_needs_hBOOL const_needs_hBOOL _ = true;
   223 
   220 
   224 fun wrap_type (s, tp) =
   221 fun wrap_type (s, tp) =
   225   if !typ_level=T_FULL then
   222   if typ_level=T_FULL then
   226       type_wrapper ^ RC.paren_pack [s, RC.string_of_fol_type tp]
   223       type_wrapper ^ RC.paren_pack [s, RC.string_of_fol_type tp]
   227   else s;
   224   else s;
   228 
   225 
   229 fun apply ss = "hAPP" ^ RC.paren_pack ss;
   226 fun apply ss = "hAPP" ^ RC.paren_pack ss;
   230 
   227 
   233 
   230 
   234 fun string_apply (v, args) = rev_apply (v, rev args);
   231 fun string_apply (v, args) = rev_apply (v, rev args);
   235 
   232 
   236 (*Apply an operator to the argument strings, using either the "apply" operator or
   233 (*Apply an operator to the argument strings, using either the "apply" operator or
   237   direct function application.*)
   234   direct function application.*)
   238 fun string_of_applic (CombConst(c,tp,tvars), args) =
   235 fun string_of_applic cma (CombConst(c,tp,tvars), args) =
   239       let val c = if c = "equal" then "c_fequal" else c
   236       let val c = if c = "equal" then "c_fequal" else c
   240           val nargs = min_arity_of c
   237           val nargs = min_arity_of cma c
   241           val args1 = List.take(args, nargs)
   238           val args1 = List.take(args, nargs)
   242             handle Subscript => error ("string_of_applic: " ^ c ^ " has arity " ^
   239             handle Subscript => error ("string_of_applic: " ^ c ^ " has arity " ^
   243                                          Int.toString nargs ^ " but is applied to " ^
   240                                          Int.toString nargs ^ " but is applied to " ^
   244                                          space_implode ", " args)
   241                                          space_implode ", " args)
   245           val args2 = List.drop(args, nargs)
   242           val args2 = List.drop(args, nargs)
   246           val targs = if !typ_level = T_CONST then map RC.string_of_fol_type tvars
   243           val targs = if typ_level = T_CONST then map RC.string_of_fol_type tvars
   247                       else []
   244                       else []
   248       in
   245       in
   249           string_apply (c ^ RC.paren_pack (args1@targs), args2)
   246           string_apply (c ^ RC.paren_pack (args1@targs), args2)
   250       end
   247       end
   251   | string_of_applic (CombVar(v,tp), args) = string_apply (v, args)
   248   | string_of_applic cma (CombVar(v,tp), args) = string_apply (v, args)
   252   | string_of_applic _ = error "string_of_applic";
   249   | string_of_applic _ _ = error "string_of_applic";
   253 
   250 
   254 fun wrap_type_if (head, s, tp) = if head_needs_hBOOL head then wrap_type (s, tp) else s;
   251 fun wrap_type_if cnh (head, s, tp) = if head_needs_hBOOL cnh head then wrap_type (s, tp) else s;
   255 
   252 
   256 fun string_of_combterm t =
   253 fun string_of_combterm cma cnh t =
   257   let val (head, args) = strip_comb t
   254   let val (head, args) = strip_comb t
   258   in  wrap_type_if (head,
   255   in  wrap_type_if cnh (head,
   259                     string_of_applic (head, map string_of_combterm args),
   256                     string_of_applic cma (head, map (string_of_combterm cma cnh) args),
   260                     type_of_combterm t)
   257                     type_of_combterm t)
   261   end;
   258   end;
   262 
   259 
   263 (*Boolean-valued terms are here converted to literals.*)
   260 (*Boolean-valued terms are here converted to literals.*)
   264 fun boolify t = "hBOOL" ^ RC.paren_pack [string_of_combterm t];
   261 fun boolify cma cnh t = "hBOOL" ^ RC.paren_pack [string_of_combterm cma cnh t];
   265 
   262 
   266 fun string_of_predicate t =
   263 fun string_of_predicate cma cnh t =
   267   case t of
   264   case t of
   268       (CombApp(CombApp(CombConst("equal",_,_), t1), t2)) =>
   265       (CombApp(CombApp(CombConst("equal",_,_), t1), t2)) =>
   269           (*DFG only: new TPTP prefers infix equality*)
   266           (*DFG only: new TPTP prefers infix equality*)
   270           ("equal" ^ RC.paren_pack [string_of_combterm t1, string_of_combterm t2])
   267           ("equal" ^ RC.paren_pack [string_of_combterm cma cnh t1, string_of_combterm cma cnh t2])
   271     | _ =>
   268     | _ =>
   272           case #1 (strip_comb t) of
   269           case #1 (strip_comb t) of
   273               CombConst(c,_,_) => if needs_hBOOL c then boolify t else string_of_combterm t
   270               CombConst(c,_,_) => if needs_hBOOL cnh c then boolify cma cnh t else string_of_combterm cma cnh t
   274             | _ => boolify t;
   271             | _ => boolify cma cnh t;
   275 
   272 
   276 fun string_of_clausename (cls_id,ax_name) =
   273 fun string_of_clausename (cls_id,ax_name) =
   277     RC.clause_prefix ^ RC.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   274     RC.clause_prefix ^ RC.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
   278 
   275 
   279 fun string_of_type_clsname (cls_id,ax_name,idx) =
   276 fun string_of_type_clsname (cls_id,ax_name,idx) =
   280     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   277     string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
   281 
   278 
   282 
   279 
   283 (*** tptp format ***)
   280 (*** tptp format ***)
   284 
   281 
   285 fun tptp_of_equality pol (t1,t2) =
   282 fun tptp_of_equality cma cnh pol (t1,t2) =
   286   let val eqop = if pol then " = " else " != "
   283   let val eqop = if pol then " = " else " != "
   287   in  string_of_combterm t1 ^ eqop ^ string_of_combterm t2  end;
   284   in  string_of_combterm cma cnh t1 ^ eqop ^ string_of_combterm cma cnh t2  end;
   288 
   285 
   289 fun tptp_literal (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) =
   286 fun tptp_literal cma cnh (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) =
   290       tptp_of_equality pol (t1,t2)
   287       tptp_of_equality cma cnh pol (t1,t2)
   291   | tptp_literal (Literal(pol,pred)) =
   288   | tptp_literal cma cnh (Literal(pol,pred)) =
   292       RC.tptp_sign pol (string_of_predicate pred);
   289       RC.tptp_sign pol (string_of_predicate cma cnh pred);
   293 
   290 
   294 (*Given a clause, returns its literals paired with a list of literals concerning TFrees;
   291 (*Given a clause, returns its literals paired with a list of literals concerning TFrees;
   295   the latter should only occur in conjecture clauses.*)
   292   the latter should only occur in conjecture clauses.*)
   296 fun tptp_type_lits pos (Clause{literals, ctypes_sorts, ...}) =
   293 fun tptp_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   297       (map tptp_literal literals, 
   294       (map (tptp_literal cma cnh) literals, 
   298        map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
   295        map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
   299 
   296 
   300 fun clause2tptp (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   297 fun clause2tptp cma cnh (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   301   let val (lits,tylits) = tptp_type_lits (kind = RC.Conjecture) cls
   298   let val (lits,tylits) = tptp_type_lits cma cnh (kind = RC.Conjecture) cls
   302   in
   299   in
   303       (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
   300       (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
   304   end;
   301   end;
   305 
   302 
   306 
   303 
   307 (*** dfg format ***)
   304 (*** dfg format ***)
   308 
   305 
   309 fun dfg_literal (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate pred);
   306 fun dfg_literal cma cnh (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate cma cnh pred);
   310 
   307 
   311 fun dfg_type_lits pos (Clause{literals, ctypes_sorts, ...}) =
   308 fun dfg_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   312       (map dfg_literal literals, 
   309       (map (dfg_literal cma cnh) literals, 
   313        map (RC.dfg_of_typeLit pos) (RC.add_typs ctypes_sorts));
   310        map (RC.dfg_of_typeLit pos) (RC.add_typs ctypes_sorts));
   314 
   311 
   315 fun get_uvars (CombConst _) vars = vars
   312 fun get_uvars (CombConst _) vars = vars
   316   | get_uvars (CombVar(v,_)) vars = (v::vars)
   313   | get_uvars (CombVar(v,_)) vars = (v::vars)
   317   | get_uvars (CombApp(P,Q)) vars = get_uvars P (get_uvars Q vars);
   314   | get_uvars (CombApp(P,Q)) vars = get_uvars P (get_uvars Q vars);
   318 
   315 
   319 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   316 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   320 
   317 
   321 fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   318 fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   322 
   319 
   323 fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   320 fun clause2dfg cma cnh (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   324   let val (lits,tylits) = dfg_type_lits (kind = RC.Conjecture) cls
   321   let val (lits,tylits) = dfg_type_lits cma cnh (kind = RC.Conjecture) cls
   325       val vars = dfg_vars cls
   322       val vars = dfg_vars cls
   326       val tvars = RC.get_tvar_strs ctypes_sorts
   323       val tvars = RC.get_tvar_strs ctypes_sorts
   327   in
   324   in
   328       (RC.gen_dfg_cls(clause_id, axiom_name, kind, lits, tylits, tvars@vars), tylits)
   325       (RC.gen_dfg_cls(clause_id, axiom_name, kind, lits, tylits, tvars@vars), tylits)
   329   end;
   326   end;
   330 
   327 
   331 
   328 
   332 (** For DFG format: accumulate function and predicate declarations **)
   329 (** For DFG format: accumulate function and predicate declarations **)
   333 
   330 
   334 fun addtypes tvars tab = foldl RC.add_foltype_funcs tab tvars;
   331 fun addtypes tvars tab = List.foldl RC.add_foltype_funcs tab tvars;
   335 
   332 
   336 fun add_decls (CombConst(c,tp,tvars), (funcs,preds)) =
   333 fun add_decls cma cnh (CombConst(c,tp,tvars), (funcs,preds)) =
   337       if c = "equal" then (addtypes tvars funcs, preds)
   334       if c = "equal" then (addtypes tvars funcs, preds)
   338       else
   335       else
   339 	let val arity = min_arity_of c
   336 	let val arity = min_arity_of cma c
   340 	    val ntys = if !typ_level = T_CONST then length tvars else 0
   337 	    val ntys = if typ_level = T_CONST then length tvars else 0
   341 	    val addit = Symtab.update(c, arity+ntys)
   338 	    val addit = Symtab.update(c, arity+ntys)
   342 	in
   339 	in
   343 	    if needs_hBOOL c then (addtypes tvars (addit funcs), preds)
   340 	    if needs_hBOOL cnh c then (addtypes tvars (addit funcs), preds)
   344 	    else (addtypes tvars funcs, addit preds)
   341 	    else (addtypes tvars funcs, addit preds)
   345 	end
   342 	end
   346   | add_decls (CombVar(_,ctp), (funcs,preds)) =
   343   | add_decls _ _ (CombVar(_,ctp), (funcs,preds)) =
   347       (RC.add_foltype_funcs (ctp,funcs), preds)
   344       (RC.add_foltype_funcs (ctp,funcs), preds)
   348   | add_decls (CombApp(P,Q),decls) = add_decls(P,add_decls (Q,decls));
   345   | add_decls cma cnh (CombApp(P,Q),decls) = add_decls cma cnh (P,add_decls cma cnh (Q,decls));
   349 
   346 
   350 fun add_literal_decls (Literal(_,c), decls) = add_decls (c,decls);
   347 fun add_literal_decls cma cnh (Literal(_,c), decls) = add_decls cma cnh (c,decls);
   351 
   348 
   352 fun add_clause_decls (Clause {literals, ...}, decls) =
   349 fun add_clause_decls cma cnh (Clause {literals, ...}, decls) =
   353     foldl add_literal_decls decls literals
   350     List.foldl (add_literal_decls cma cnh) decls literals
   354     handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
   351     handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
   355 
   352 
   356 fun decls_of_clauses clauses arity_clauses =
   353 fun decls_of_clauses cma cnh clauses arity_clauses =
   357   let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
   354   let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
   358       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   355       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   359       val (functab,predtab) = (foldl add_clause_decls (init_functab, init_predtab) clauses)
   356       val (functab,predtab) = (List.foldl (add_clause_decls cma cnh) (init_functab, init_predtab) clauses)
   360   in
   357   in
   361       (Symtab.dest (foldl RC.add_arityClause_funcs functab arity_clauses),
   358       (Symtab.dest (List.foldl RC.add_arityClause_funcs functab arity_clauses),
   362        Symtab.dest predtab)
   359        Symtab.dest predtab)
   363   end;
   360   end;
   364 
   361 
   365 fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
   362 fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
   366   foldl RC.add_type_sort_preds preds ctypes_sorts
   363   List.foldl RC.add_type_sort_preds preds ctypes_sorts
   367   handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
   364   handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
   368 
   365 
   369 (*Higher-order clauses have only the predicates hBOOL and type classes.*)
   366 (*Higher-order clauses have only the predicates hBOOL and type classes.*)
   370 fun preds_of_clauses clauses clsrel_clauses arity_clauses =
   367 fun preds_of_clauses clauses clsrel_clauses arity_clauses =
   371     Symtab.dest
   368     Symtab.dest
   372         (foldl RC.add_classrelClause_preds
   369         (List.foldl RC.add_classrelClause_preds
   373                (foldl RC.add_arityClause_preds
   370                (List.foldl RC.add_arityClause_preds
   374                       (foldl add_clause_preds Symtab.empty clauses)
   371                       (List.foldl add_clause_preds Symtab.empty clauses)
   375                       arity_clauses)
   372                       arity_clauses)
   376                clsrel_clauses)
   373                clsrel_clauses)
   377 
   374 
   378 
   375 
   379 (**********************************************************************)
   376 (**********************************************************************)
   391   | count_combterm (CombVar(v,tp), ct) = ct
   388   | count_combterm (CombVar(v,tp), ct) = ct
   392   | count_combterm (CombApp(t1,t2), ct) = count_combterm(t1, count_combterm(t2, ct));
   389   | count_combterm (CombApp(t1,t2), ct) = count_combterm(t1, count_combterm(t2, ct));
   393 
   390 
   394 fun count_literal (Literal(_,t), ct) = count_combterm(t,ct);
   391 fun count_literal (Literal(_,t), ct) = count_combterm(t,ct);
   395 
   392 
   396 fun count_clause (Clause{literals,...}, ct) = foldl count_literal ct literals;
   393 fun count_clause (Clause{literals,...}, ct) = List.foldl count_literal ct literals;
   397 
   394 
   398 fun count_user_clause user_lemmas (Clause{axiom_name,literals,...}, ct) =
   395 fun count_user_clause user_lemmas (Clause{axiom_name,literals,...}, ct) =
   399   if axiom_name mem_string user_lemmas then foldl count_literal ct literals
   396   if axiom_name mem_string user_lemmas then List.foldl count_literal ct literals
   400   else ct;
   397   else ct;
   401 
   398 
   402 fun cnf_helper_thms thy =
   399 fun cnf_helper_thms thy =
   403   ResAxioms.cnf_rules_pairs thy o map ResAxioms.pairname
   400   ResAxioms.cnf_rules_pairs thy o map ResAxioms.pairname
   404 
   401 
   405 fun get_helper_clauses thy isFO (conjectures, axclauses, user_lemmas) =
   402 fun get_helper_clauses dfg thy isFO (conjectures, axclauses, user_lemmas) =
   406   if isFO then []  (*first-order*)
   403   if isFO then []  (*first-order*)
   407   else
   404   else
   408     let val ct0 = foldl count_clause init_counters conjectures
   405     let val ct0 = List.foldl count_clause init_counters conjectures
   409         val ct = foldl (count_user_clause user_lemmas) ct0 axclauses
   406         val ct = List.foldl (count_user_clause user_lemmas) ct0 axclauses
   410         fun needed c = valOf (Symtab.lookup ct c) > 0
   407         fun needed c = valOf (Symtab.lookup ct c) > 0
   411         val IK = if needed "c_COMBI" orelse needed "c_COMBK"
   408         val IK = if needed "c_COMBI" orelse needed "c_COMBK"
   412                  then (Output.debug (fn () => "Include combinator I K"); cnf_helper_thms thy [comb_I,comb_K])
   409                  then (Output.debug (fn () => "Include combinator I K"); cnf_helper_thms thy [comb_I,comb_K])
   413                  else []
   410                  else []
   414         val BC = if needed "c_COMBB" orelse needed "c_COMBC"
   411         val BC = if needed "c_COMBB" orelse needed "c_COMBC"
   417         val S = if needed "c_COMBS"
   414         val S = if needed "c_COMBS"
   418                 then (Output.debug (fn () => "Include combinator S"); cnf_helper_thms thy [comb_S])
   415                 then (Output.debug (fn () => "Include combinator S"); cnf_helper_thms thy [comb_S])
   419                 else []
   416                 else []
   420         val other = cnf_helper_thms thy [ext,fequal_imp_equal,equal_imp_fequal]
   417         val other = cnf_helper_thms thy [ext,fequal_imp_equal,equal_imp_fequal]
   421     in
   418     in
   422         map #2 (make_axiom_clauses thy (other @ IK @ BC @ S))
   419         map #2 (make_axiom_clauses dfg thy (other @ IK @ BC @ S))
   423     end;
   420     end;
   424 
   421 
   425 (*Find the minimal arity of each function mentioned in the term. Also, note which uses
   422 (*Find the minimal arity of each function mentioned in the term. Also, note which uses
   426   are not at top level, to see if hBOOL is needed.*)
   423   are not at top level, to see if hBOOL is needed.*)
   427 fun count_constants_term toplev t =
   424 fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
   428   let val (head, args) = strip_comb t
   425   let val (head, args) = strip_comb t
   429       val n = length args
   426       val n = length args
   430       val _ = List.app (count_constants_term false) args
   427       val (const_min_arity, const_needs_hBOOL) = fold (count_constants_term false) args (const_min_arity, const_needs_hBOOL)
   431   in
   428   in
   432       case head of
   429       case head of
   433           CombConst (a,_,_) => (*predicate or function version of "equal"?*)
   430           CombConst (a,_,_) => (*predicate or function version of "equal"?*)
   434             let val a = if a="equal" andalso not toplev then "c_fequal" else a
   431             let val a = if a="equal" andalso not toplev then "c_fequal" else a
       
   432             val const_min_arity = Symtab.map_default (a,n) (curry Int.min n) const_min_arity
   435             in
   433             in
   436               const_min_arity := Symtab.map_default (a,n) (curry Int.min n) (!const_min_arity);
   434               if toplev then (const_min_arity, const_needs_hBOOL)
   437               if toplev then ()
   435               else (const_min_arity, Symtab.update (a,true) (const_needs_hBOOL))
   438               else const_needs_hBOOL := Symtab.update (a,true) (!const_needs_hBOOL)
       
   439             end
   436             end
   440         | ts => ()
   437         | ts => (const_min_arity, const_needs_hBOOL)
   441   end;
   438   end;
   442 
   439 
   443 (*A literal is a top-level term*)
   440 (*A literal is a top-level term*)
   444 fun count_constants_lit (Literal (_,t)) = count_constants_term true t;
   441 fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
   445 
   442   count_constants_term true t (const_min_arity, const_needs_hBOOL);
   446 fun count_constants_clause (Clause{literals,...}) = List.app count_constants_lit literals;
   443 
   447 
   444 fun count_constants_clause (Clause{literals,...}) (const_min_arity, const_needs_hBOOL) =
   448 fun display_arity (c,n) =
   445   fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
       
   446 
       
   447 fun display_arity const_needs_hBOOL (c,n) =
   449   Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
   448   Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
   450                 (if needs_hBOOL c then " needs hBOOL" else ""));
   449                 (if needs_hBOOL const_needs_hBOOL c then " needs hBOOL" else ""));
   451 
   450 
   452 fun count_constants (conjectures, axclauses, helper_clauses) =
   451 fun count_constants (conjectures, axclauses, helper_clauses) =
   453   if !minimize_applies then
   452   if minimize_applies then
   454     (const_min_arity := Symtab.empty;
   453      let val (const_min_arity, const_needs_hBOOL) =
   455      const_needs_hBOOL := Symtab.empty;
   454           fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
   456      List.app count_constants_clause conjectures;
   455        |> fold count_constants_clause axclauses
   457      List.app count_constants_clause axclauses;
   456        |> fold count_constants_clause helper_clauses
   458      List.app count_constants_clause helper_clauses;
   457      val _ = List.app (display_arity const_needs_hBOOL) (Symtab.dest (const_min_arity))
   459      List.app display_arity (Symtab.dest (!const_min_arity)))
   458      in (const_min_arity, const_needs_hBOOL) end
   460   else ();
   459   else (Symtab.empty, Symtab.empty);
   461 
   460 
   462 (* tptp format *)
   461 (* tptp format *)
   463 
   462 
   464 (* write TPTP format to a single file *)
   463 (* write TPTP format to a single file *)
   465 fun tptp_write_file thy isFO thms filename (ax_tuples,classrel_clauses,arity_clauses) user_lemmas =
   464 fun tptp_write_file thy isFO thms filename (ax_tuples,classrel_clauses,arity_clauses) user_lemmas =
   466     let val _ = Output.debug (fn () => ("Preparing to write the TPTP file " ^ filename))
   465     let val _ = Output.debug (fn () => ("Preparing to write the TPTP file " ^ filename))
   467         val _ = RC.dfg_format := false
   466         val conjectures = make_conjecture_clauses false thy thms
   468         val conjectures = make_conjecture_clauses thy thms
   467         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses false thy ax_tuples)
   469         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses thy ax_tuples)
   468         val helper_clauses = get_helper_clauses false thy isFO (conjectures, axclauses, user_lemmas)
   470         val helper_clauses = get_helper_clauses thy isFO (conjectures, axclauses, user_lemmas)
   469         val (const_min_arity, const_needs_hBOOL) = count_constants (conjectures, axclauses, helper_clauses);
   471         val _ = count_constants (conjectures, axclauses, helper_clauses);
   470         val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_min_arity const_needs_hBOOL) conjectures)
   472         val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp conjectures)
   471         val tfree_clss = map RC.tptp_tfree_clause (List.foldl (op union_string) [] tfree_litss)
   473         val tfree_clss = map RC.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
       
   474         val out = TextIO.openOut filename
   472         val out = TextIO.openOut filename
   475     in
   473     in
   476         List.app (curry TextIO.output out o #1 o clause2tptp) axclauses;
   474         List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity const_needs_hBOOL)) axclauses;
   477         RC.writeln_strs out tfree_clss;
   475         RC.writeln_strs out tfree_clss;
   478         RC.writeln_strs out tptp_clss;
   476         RC.writeln_strs out tptp_clss;
   479         List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   477         List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   480         List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
   478         List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
   481         List.app (curry TextIO.output out o #1 o clause2tptp) helper_clauses;
   479         List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity const_needs_hBOOL)) helper_clauses;
   482         TextIO.closeOut out;
   480         TextIO.closeOut out;
   483         clnames
   481         clnames
   484     end;
   482     end;
   485 
   483 
   486 
   484 
   487 (* dfg format *)
   485 (* dfg format *)
   488 
   486 
   489 fun dfg_write_file thy isFO thms filename (ax_tuples,classrel_clauses,arity_clauses) user_lemmas =
   487 fun dfg_write_file thy isFO thms filename (ax_tuples,classrel_clauses,arity_clauses) user_lemmas =
   490     let val _ = Output.debug (fn () => ("Preparing to write the DFG file " ^ filename))
   488     let val _ = Output.debug (fn () => ("Preparing to write the DFG file " ^ filename))
   491         val _ = RC.dfg_format := true
   489         val conjectures = make_conjecture_clauses true thy thms
   492         val conjectures = make_conjecture_clauses thy thms
   490         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses true thy ax_tuples)
   493         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses thy ax_tuples)
   491         val helper_clauses = get_helper_clauses true thy isFO (conjectures, axclauses, user_lemmas)
   494         val helper_clauses = get_helper_clauses thy isFO (conjectures, axclauses, user_lemmas)
   492         val (const_min_arity, const_needs_hBOOL) = count_constants (conjectures, axclauses, helper_clauses);
   495         val _ = count_constants (conjectures, axclauses, helper_clauses);
   493         val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_min_arity const_needs_hBOOL) conjectures)
   496         val (dfg_clss, tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
       
   497         and probname = Path.implode (Path.base (Path.explode filename))
   494         and probname = Path.implode (Path.base (Path.explode filename))
   498         val axstrs = map (#1 o clause2dfg) axclauses
   495         val axstrs = map (#1 o (clause2dfg const_min_arity const_needs_hBOOL)) axclauses
   499         val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   496         val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   500         val out = TextIO.openOut filename
   497         val out = TextIO.openOut filename
   501         val helper_clauses_strs = map (#1 o clause2dfg) helper_clauses
   498         val helper_clauses_strs = map (#1 o (clause2dfg const_min_arity const_needs_hBOOL)) helper_clauses
   502         val (funcs,cl_preds) = decls_of_clauses (helper_clauses @ conjectures @ axclauses) arity_clauses
   499         val (funcs,cl_preds) = decls_of_clauses const_min_arity const_needs_hBOOL (helper_clauses @ conjectures @ axclauses) arity_clauses
   503         and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   500         and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   504     in
   501     in
   505         TextIO.output (out, RC.string_of_start probname);
   502         TextIO.output (out, RC.string_of_start probname);
   506         TextIO.output (out, RC.string_of_descrip probname);
   503         TextIO.output (out, RC.string_of_descrip probname);
   507         TextIO.output (out, RC.string_of_symbols
   504         TextIO.output (out, RC.string_of_symbols