src/HOL/Tools/Sledgehammer/sledgehammer_tptp_format.ML
changeset 37642 06992bc8bdda
parent 37624 3ee568334813
child 37643 f576af716aa6
equal deleted inserted replaced
37633:ff1137a9c056 37642:06992bc8bdda
   168 
   168 
   169 (*Boolean-valued terms are here converted to literals.*)
   169 (*Boolean-valued terms are here converted to literals.*)
   170 fun boolify params c =
   170 fun boolify params c =
   171   string_of_combterm params c #>> prefix "hBOOL" o paren_pack o single
   171   string_of_combterm params c #>> prefix "hBOOL" o paren_pack o single
   172 
   172 
   173 fun string_of_predicate (params as (_, const_tab)) t =
   173 fun string_for_predicate (params as (_, const_tab)) t =
   174   case #1 (strip_combterm_comb t) of
   174   case #1 (strip_combterm_comb t) of
   175     CombConst ((s, _), _, _) =>
   175     CombConst ((s, _), _, _) =>
   176     (if needs_hBOOL const_tab s then boolify else string_of_combterm) params t
   176     (if needs_hBOOL const_tab s then boolify else string_of_combterm) params t
   177   | _ => boolify params t
   177   | _ => boolify params t
   178 
   178 
   179 fun tptp_of_equality params pos (t1, t2) =
   179 fun string_for_equality params pos (t1, t2) =
   180   pool_map (string_of_combterm params) [t1, t2]
   180   pool_map (string_of_combterm params) [t1, t2]
   181   #>> space_implode (if pos then " = " else " != ")
   181   #>> space_implode (if pos then " = " else " != ")
   182 
   182 
   183 fun tptp_sign true s = s
   183 fun string_for_sign true s = s
   184   | tptp_sign false s = "~ " ^ s
   184   | string_for_sign false s = "~ " ^ s
   185 
   185 
   186 fun tptp_literal params
   186 fun string_for_literal params
   187         (Literal (pos, CombApp (CombApp (CombConst (("equal", _), _, _), t1),
   187         (Literal (pos, CombApp (CombApp (CombConst (("equal", _), _, _), t1),
   188                                          t2))) =
   188                                          t2))) =
   189     tptp_of_equality params pos (t1, t2)
   189     string_for_equality params pos (t1, t2)
   190   | tptp_literal params (Literal (pos, pred)) =
   190   | string_for_literal params (Literal (pos, pred)) =
   191     string_of_predicate params pred #>> tptp_sign pos
   191     string_for_predicate params pred #>> string_for_sign pos
   192  
   192  
   193 fun tptp_of_type_literal pos (TyLitVar (s, name)) =
   193 fun string_for_type_literal pos (TyLitVar (s, name)) =
   194     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
   194     nice_name name #>> (fn s' => string_for_sign pos (s ^ "(" ^ s' ^ ")"))
   195   | tptp_of_type_literal pos (TyLitFree (s, name)) =
   195   | string_for_type_literal pos (TyLitFree (s, name)) =
   196     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
   196     nice_name name #>> (fn s' => string_for_sign pos (s ^ "(" ^ s' ^ ")"))
   197 
   197 
   198 (* Given a clause, returns its literals paired with a list of literals
   198 (* Given a clause, returns its literals paired with a list of literals
   199    concerning TFrees; the latter should only occur in conjecture clauses. *)
   199    concerning TFrees; the latter should only occur in conjecture clauses. *)
   200 fun tptp_type_literals params pos (HOLClause {literals, ctypes_sorts, ...})
   200 fun string_for_type_literals params pos
   201                        pool =
   201                              (HOLClause {literals, ctypes_sorts, ...}) pool =
   202   let
   202   let
   203     val (lits, pool) = pool_map (tptp_literal params) literals pool
   203     (* ### FIXME: use combinator *)
   204     val (tylits, pool) = pool_map (tptp_of_type_literal pos)
   204     val (lits, pool) = pool_map (string_for_literal params) literals pool
       
   205     val (tylits, pool) = pool_map (string_for_type_literal pos)
   205                                   (type_literals_for_types ctypes_sorts) pool
   206                                   (type_literals_for_types ctypes_sorts) pool
   206   in ((lits, tylits), pool) end
   207   in ((lits, tylits), pool) end
   207 
   208 
   208 fun tptp_cnf name kind formula =
   209 fun string_for_cnf_clause name kind formula =
   209   "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
   210   "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
   210 
   211 
   211 fun tptp_raw_clause strings = "(" ^ space_implode " | " strings ^ ")"
   212 fun string_for_disjunction strings = "(" ^ space_implode " | " strings ^ ")"
   212 
   213 
   213 val tptp_tfree_clause =
   214 val string_for_tfree_clause =
   214   tptp_cnf "tfree_tcs" "negated_conjecture" o tptp_raw_clause o single
   215   string_for_cnf_clause "tfree_tcs" "negated_conjecture"
   215 
   216   o string_for_disjunction o single
   216 fun tptp_classrel_literals sub sup =
   217 
       
   218 fun string_for_classrel_literals sub sup =
   217   let val tvar = "(T)" in
   219   let val tvar = "(T)" in
   218     tptp_raw_clause [tptp_sign false (sub ^ tvar), tptp_sign true (sup ^ tvar)]
   220     string_for_disjunction [string_for_sign false (sub ^ tvar),
       
   221                             string_for_sign true (sup ^ tvar)]
   219   end
   222   end
   220 
   223 
   221 fun tptp_clause params (cls as HOLClause {axiom_name, clause_id, kind, ...})
   224 fun string_for_clause params
   222                 pool =
   225         (cls as HOLClause {axiom_name, clause_id, kind, ...}) pool =
   223   let
   226   let
   224     val ((lits, tylits), pool) =
   227     val ((lits, tylits), pool) =
   225       tptp_type_literals params (kind = Conjecture) cls pool
   228       string_for_type_literals params (kind = Conjecture) cls pool
   226     val name = clause_prefix ^ ascii_of axiom_name ^ "_" ^
   229     val name = clause_prefix ^ ascii_of axiom_name ^ "_" ^
   227                Int.toString clause_id
   230                Int.toString clause_id
   228     val cnf =
   231     val cnf =
   229       case kind of
   232       case kind of
   230         Axiom => tptp_cnf name "axiom" (tptp_raw_clause (tylits @ lits))
   233         Axiom => string_for_cnf_clause name "axiom"
   231       | Conjecture => tptp_cnf name "negated_conjecture" (tptp_raw_clause lits)
   234                                        (string_for_disjunction (tylits @ lits))
       
   235       | Conjecture => string_for_cnf_clause name "negated_conjecture"
       
   236                                        (string_for_disjunction lits)
   232   in ((cnf, tylits), pool) end
   237   in ((cnf, tylits), pool) end
   233 
   238 
   234 fun tptp_classrel_clause (ClassrelClause {axiom_name, subclass, superclass,
   239 fun string_for_classrel_clause (ClassrelClause {axiom_name, subclass,
       
   240                                                 superclass, ...}) =
       
   241   string_for_cnf_clause axiom_name "axiom"
       
   242                         (string_for_classrel_literals subclass superclass)
       
   243 
       
   244 fun string_for_arity_literal (TConsLit (c, t, args)) =
       
   245     string_for_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
       
   246   | string_for_arity_literal (TVarLit (c, str)) =
       
   247     string_for_sign false (make_type_class c ^ "(" ^ str ^ ")")
       
   248 
       
   249 fun string_for_arity_clause (ArityClause {axiom_name, conclLit, premLits,
   235                                           ...}) =
   250                                           ...}) =
   236   tptp_cnf axiom_name "axiom" (tptp_classrel_literals subclass superclass)
   251   string_for_cnf_clause (arity_clause_prefix ^ ascii_of axiom_name) "axiom"
   237 
   252                         (string_for_disjunction (map string_for_arity_literal
   238 fun tptp_of_arity_literal (TConsLit (c, t, args)) =
   253                                                 (conclLit :: premLits)))
   239     tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
       
   240   | tptp_of_arity_literal (TVarLit (c, str)) =
       
   241     tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
       
   242 
       
   243 fun tptp_arity_clause (ArityClause {axiom_name, conclLit, premLits, ...}) =
       
   244   tptp_cnf (arity_clause_prefix ^ ascii_of axiom_name) "axiom"
       
   245            (tptp_raw_clause (map tptp_of_arity_literal (conclLit :: premLits)))
       
   246 
   254 
   247 (* Find the minimal arity of each function mentioned in the term. Also, note
   255 (* Find the minimal arity of each function mentioned in the term. Also, note
   248    which uses are not at top level, to see if "hBOOL" is needed. *)
   256    which uses are not at top level, to see if "hBOOL" is needed. *)
   249 fun count_constants_term top_level t =
   257 fun count_constants_term top_level t =
   250   let val (head, args) = strip_combterm_comb t in
   258   let val (head, args) = strip_combterm_comb t in
   283       classrel_clauses, arity_clauses) = clauses
   291       classrel_clauses, arity_clauses) = clauses
   284     val const_tab = if explicit_apply then NONE
   292     val const_tab = if explicit_apply then NONE
   285                     else SOME (Symtab.empty |> count_constants clauses)
   293                     else SOME (Symtab.empty |> count_constants clauses)
   286     val params = (full_types, const_tab)
   294     val params = (full_types, const_tab)
   287     val ((conjecture_clss, tfree_litss), pool) =
   295     val ((conjecture_clss, tfree_litss), pool) =
   288       pool_map (tptp_clause params) conjectures pool |>> ListPair.unzip
   296       pool_map (string_for_clause params) conjectures pool |>> ListPair.unzip
   289     val tfree_clss = map tptp_tfree_clause (fold (union (op =)) tfree_litss [])
   297     val tfree_clss =
       
   298       map string_for_tfree_clause (fold (union (op =)) tfree_litss [])
   290     val (ax_clss, pool) =
   299     val (ax_clss, pool) =
   291       pool_map (apfst fst oo tptp_clause params) axclauses pool
   300       pool_map (apfst fst oo string_for_clause params) axclauses pool
   292     val classrel_clss = map tptp_classrel_clause classrel_clauses
   301     val classrel_clss = map string_for_classrel_clause classrel_clauses
   293     val arity_clss = map tptp_arity_clause arity_clauses
   302     val arity_clss = map string_for_arity_clause arity_clauses
   294     val (helper_clss, pool) = pool_map (apfst fst oo tptp_clause params)
   303     val (helper_clss, pool) = pool_map (apfst fst oo string_for_clause params)
   295                                        helper_clauses pool
   304                                        helper_clauses pool
   296     val conjecture_offset =
   305     val conjecture_offset =
   297       length ax_clss + length classrel_clss + length arity_clss
   306       length ax_clss + length classrel_clss + length arity_clss
   298       + length helper_clss
   307       + length helper_clss
   299     val _ =
   308     val _ =