src/HOL/Tools/Sledgehammer/sledgehammer.ML
changeset 38105 373351f5f834
parent 38102 019a49759829
child 38200 2f531f620cb8
equal deleted inserted replaced
38104:8fbf60c33794 38105:373351f5f834
   160 
   160 
   161 
   161 
   162 (* Clause preparation *)
   162 (* Clause preparation *)
   163 
   163 
   164 datatype fol_formula =
   164 datatype fol_formula =
   165   FOLFormula of {formula_name: string,
   165   FOLFormula of {name: string,
   166                  kind: kind,
   166                  kind: kind,
   167                  combformula: (name, combterm) formula,
   167                  combformula: (name, combterm) formula,
   168                  ctypes_sorts: typ list}
   168                  ctypes_sorts: typ list}
   169 
   169 
   170 fun mk_anot phi = AConn (ANot, [phi])
   170 fun mk_anot phi = AConn (ANot, [phi])
   294         Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
   294         Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
   295       | aux t = t
   295       | aux t = t
   296   in t |> exists_subterm is_Var t ? aux end
   296   in t |> exists_subterm is_Var t ? aux end
   297 
   297 
   298 (* making axiom and conjecture formulas *)
   298 (* making axiom and conjecture formulas *)
   299 fun make_formula ctxt presimp (formula_name, kind, t) =
   299 fun make_formula ctxt presimp (name, kind, t) =
   300   let
   300   let
   301     val thy = ProofContext.theory_of ctxt
   301     val thy = ProofContext.theory_of ctxt
   302     val t = t |> transform_elim_term
   302     val t = t |> transform_elim_term
   303               |> Object_Logic.atomize_term thy
   303               |> Object_Logic.atomize_term thy
   304     val t = t |> fastype_of t = HOLogic.boolT ? HOLogic.mk_Trueprop
   304     val t = t |> fastype_of t = HOLogic.boolT ? HOLogic.mk_Trueprop
   307               |> perhaps (try (HOLogic.dest_Trueprop))
   307               |> perhaps (try (HOLogic.dest_Trueprop))
   308               |> introduce_combinators_in_term ctxt kind
   308               |> introduce_combinators_in_term ctxt kind
   309               |> kind = Conjecture ? freeze_term
   309               |> kind = Conjecture ? freeze_term
   310     val (combformula, ctypes_sorts) = combformula_for_prop thy t []
   310     val (combformula, ctypes_sorts) = combformula_for_prop thy t []
   311   in
   311   in
   312     FOLFormula {formula_name = formula_name, combformula = combformula,
   312     FOLFormula {name = name, combformula = combformula, kind = kind,
   313                 kind = kind, ctypes_sorts = ctypes_sorts}
   313                 ctypes_sorts = ctypes_sorts}
   314   end
   314   end
   315 
   315 
   316 fun make_axiom ctxt presimp (name, th) =
   316 fun make_axiom ctxt presimp (name, th) =
   317   (name, make_formula ctxt presimp (name, Axiom, prop_of th))
   317   (name, make_formula ctxt presimp (name, Axiom, prop_of th))
   318 fun make_conjectures ctxt ts =
   318 fun make_conjectures ctxt ts =
   431 fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) =
   431 fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) =
   432   mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
   432   mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
   433                 (type_literals_for_types ctypes_sorts))
   433                 (type_literals_for_types ctypes_sorts))
   434            (formula_for_combformula full_types combformula)
   434            (formula_for_combformula full_types combformula)
   435 
   435 
   436 fun problem_line_for_axiom full_types
   436 fun problem_line_for_fact prefix full_types
   437         (formula as FOLFormula {formula_name, kind, ...}) =
   437                           (formula as FOLFormula {name, kind, ...}) =
   438   Fof (axiom_prefix ^ ascii_of formula_name, kind,
   438   Fof (prefix ^ ascii_of name, kind, formula_for_axiom full_types formula)
   439        formula_for_axiom full_types formula)
   439 
   440 
   440 fun problem_line_for_class_rel_clause (ClassRelClause {name, subclass,
   441 fun problem_line_for_class_rel_clause
   441                                                        superclass, ...}) =
   442         (ClassRelClause {axiom_name, subclass, superclass, ...}) =
       
   443   let val ty_arg = ATerm (("T", "T"), []) in
   442   let val ty_arg = ATerm (("T", "T"), []) in
   444     Fof (ascii_of axiom_name, Axiom,
   443     Fof (class_rel_clause_prefix ^ ascii_of name, Axiom,
   445          AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
   444          AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
   446                            AAtom (ATerm (superclass, [ty_arg]))]))
   445                            AAtom (ATerm (superclass, [ty_arg]))]))
   447   end
   446   end
   448 
   447 
   449 fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
   448 fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
   450     (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
   449     (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
   451   | fo_literal_for_arity_literal (TVarLit (c, sort)) =
   450   | fo_literal_for_arity_literal (TVarLit (c, sort)) =
   452     (false, ATerm (c, [ATerm (sort, [])]))
   451     (false, ATerm (c, [ATerm (sort, [])]))
   453 
   452 
   454 fun problem_line_for_arity_clause
   453 fun problem_line_for_arity_clause (ArityClause {name, conclLit, premLits,
   455         (ArityClause {axiom_name, conclLit, premLits, ...}) =
   454                                                 ...}) =
   456   Fof (arity_clause_prefix ^ ascii_of axiom_name, Axiom,
   455   Fof (arity_clause_prefix ^ ascii_of name, Axiom,
   457        mk_ahorn (map (formula_for_fo_literal o apfst not
   456        mk_ahorn (map (formula_for_fo_literal o apfst not
   458                       o fo_literal_for_arity_literal) premLits)
   457                       o fo_literal_for_arity_literal) premLits)
   459                 (formula_for_fo_literal
   458                 (formula_for_fo_literal
   460                      (fo_literal_for_arity_literal conclLit)))
   459                      (fo_literal_for_arity_literal conclLit)))
   461 
   460 
   462 fun problem_line_for_conjecture full_types
   461 fun problem_line_for_conjecture full_types
   463         (FOLFormula {formula_name, kind, combformula, ...}) =
   462                                 (FOLFormula {name, kind, combformula, ...}) =
   464   Fof (conjecture_prefix ^ formula_name, kind,
   463   Fof (conjecture_prefix ^ name, kind,
   465        formula_for_combformula full_types combformula)
   464        formula_for_combformula full_types combformula)
   466 
   465 
   467 fun free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) =
   466 fun free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) =
   468   map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
   467   map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
   469 
   468 
   606 
   605 
   607 fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply
   606 fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply
   608                     file (conjectures, axioms, helper_facts, class_rel_clauses,
   607                     file (conjectures, axioms, helper_facts, class_rel_clauses,
   609                           arity_clauses) =
   608                           arity_clauses) =
   610   let
   609   let
   611     val axiom_lines = map (problem_line_for_axiom full_types) axioms
   610     val axiom_lines = map (problem_line_for_fact axiom_prefix full_types) axioms
       
   611     val helper_lines =
       
   612       map (problem_line_for_fact helper_prefix full_types) helper_facts
       
   613     val conjecture_lines =
       
   614       map (problem_line_for_conjecture full_types) conjectures
       
   615     val tfree_lines = problem_lines_for_free_types conjectures
   612     val class_rel_lines =
   616     val class_rel_lines =
   613       map problem_line_for_class_rel_clause class_rel_clauses
   617       map problem_line_for_class_rel_clause class_rel_clauses
   614     val arity_lines = map problem_line_for_arity_clause arity_clauses
   618     val arity_lines = map problem_line_for_arity_clause arity_clauses
   615     val helper_lines = map (problem_line_for_axiom full_types) helper_facts
       
   616     val conjecture_lines =
       
   617       map (problem_line_for_conjecture full_types) conjectures
       
   618     val tfree_lines = problem_lines_for_free_types conjectures
       
   619     (* Reordering these might or might not confuse the proof reconstruction
   619     (* Reordering these might or might not confuse the proof reconstruction
   620        code or the SPASS Flotter hack. *)
   620        code or the SPASS Flotter hack. *)
   621     val problem =
   621     val problem =
   622       [("Relevant facts", axiom_lines),
   622       [("Relevant facts", axiom_lines),
   623        ("Class relationships", class_rel_lines),
   623        ("Class relationships", class_rel_lines),
   645   in output |> split_lines |> map_filter (extract_num o tokens_of) end
   645   in output |> split_lines |> map_filter (extract_num o tokens_of) end
   646 
   646 
   647 val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
   647 val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
   648 
   648 
   649 val parse_clause_formula_pair =
   649 val parse_clause_formula_pair =
   650   $$ "(" |-- scan_integer --| $$ "," -- Symbol.scan_id --| $$ ")"
   650   $$ "(" |-- scan_integer --| $$ "," -- Symbol.scan_id
       
   651   --| Scan.repeat ($$ "," |-- Symbol.scan_id) --| $$ ")"
   651   --| Scan.option ($$ ",")
   652   --| Scan.option ($$ ",")
   652 val parse_clause_formula_relation =
   653 val parse_clause_formula_relation =
   653   Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
   654   Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
   654   |-- Scan.repeat parse_clause_formula_pair
   655   |-- Scan.repeat parse_clause_formula_pair
   655 val extract_clause_formula_relation =
   656 val extract_clause_formula_relation =
   671       fun renumber_conjecture j =
   672       fun renumber_conjecture j =
   672         AList.find (op =) name_map (conjecture_prefix ^ Int.toString (j - j0))
   673         AList.find (op =) name_map (conjecture_prefix ^ Int.toString (j - j0))
   673         |> map (fn s => find_index (curry (op =) s) seq + 1)
   674         |> map (fn s => find_index (curry (op =) s) seq + 1)
   674     in
   675     in
   675       (conjecture_shape |> map (maps renumber_conjecture),
   676       (conjecture_shape |> map (maps renumber_conjecture),
   676        seq |> map (the o AList.lookup (op =) name_map)
   677        seq |> map (fn j => case j |> AList.lookup (op =) name_map |> the
   677            |> map (fn s => case try (unprefix axiom_prefix) s of
   678                                   |> try (unprefix axiom_prefix) of
   678                              SOME s' => undo_ascii_of s'
   679                              SOME s' => undo_ascii_of s'
   679                            | NONE => "")
   680                            | NONE => "")
   680            |> Vector.fromList)
   681            |> Vector.fromList)
   681     end
   682     end
   682   else
   683   else