src/HOL/Tools/Sledgehammer/sledgehammer.ML
changeset 38085 cc44e887246c
parent 38084 e2aac207d13b
child 38086 c802b76d542f
equal deleted inserted replaced
38084:e2aac207d13b 38085:cc44e887246c
   169 fun mk_ahorn [] phi = phi
   169 fun mk_ahorn [] phi = phi
   170   | mk_ahorn (phi :: phis) psi =
   170   | mk_ahorn (phi :: phis) psi =
   171     AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
   171     AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
   172 
   172 
   173 (* ### FIXME: reintroduce
   173 (* ### FIXME: reintroduce
   174 fun make_clause_table xs =
   174 fun make_formula_table xs =
   175   fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty
   175   fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty
   176 (* Remove existing axiom clauses from the conjecture clauses, as this can
   176 (* Remove existing axioms from the conjecture, as this can
   177    dramatically boost an ATP's performance (for some reason). *)
   177    dramatically boost an ATP's performance (for some reason). *)
   178 fun subtract_cls ax_clauses =
   178 fun subtract_cls axioms =
   179   filter_out (Termtab.defined (make_clause_table ax_clauses) o prop_of)
   179   filter_out (Termtab.defined (make_formula_table axioms) o prop_of)
   180 *)
   180 *)
   181 
   181 
   182 fun combformula_for_prop thy =
   182 fun combformula_for_prop thy =
   183   let
   183   let
   184     val do_term = combterm_from_term thy
   184     val do_term = combterm_from_term thy
   275          (* A type variable of sort "{}" will make abstraction fail. *)
   275          (* A type variable of sort "{}" will make abstraction fail. *)
   276          case kind of
   276          case kind of
   277            Axiom => HOLogic.true_const
   277            Axiom => HOLogic.true_const
   278          | Conjecture => HOLogic.false_const
   278          | Conjecture => HOLogic.false_const
   279 
   279 
   280 (* making axiom and conjecture clauses *)
   280 (* making axiom and conjecture formulas *)
   281 fun make_clause ctxt (formula_name, kind, t) =
   281 fun make_formula ctxt (formula_name, kind, t) =
   282   let
   282   let
   283     val thy = ProofContext.theory_of ctxt
   283     val thy = ProofContext.theory_of ctxt
   284     (* ### FIXME: perform other transformations previously done by
   284     (* ### FIXME: perform other transformations previously done by
   285        "Clausifier.to_nnf", e.g. "HOL.If" *)
   285        "Clausifier.to_nnf", e.g. "HOL.If" *)
   286     val t = t |> transform_elim_term
   286     val t = t |> transform_elim_term
   291   in
   291   in
   292     FOLFormula {formula_name = formula_name, combformula = combformula,
   292     FOLFormula {formula_name = formula_name, combformula = combformula,
   293                 kind = kind, ctypes_sorts = ctypes_sorts}
   293                 kind = kind, ctypes_sorts = ctypes_sorts}
   294   end
   294   end
   295 
   295 
   296 fun make_axiom_clause ctxt (name, th) =
   296 fun make_axiom ctxt (name, th) =
   297   (name, make_clause ctxt (name, Axiom, prop_of th))
   297   (name, make_formula ctxt (name, Axiom, prop_of th))
   298 fun make_conjecture_clauses ctxt ts =
   298 fun make_conjectures ctxt ts =
   299   map2 (fn j => fn t => make_clause ctxt (Int.toString j, Conjecture, t))
   299   map2 (fn j => fn t => make_formula ctxt (Int.toString j, Conjecture, t))
   300        (0 upto length ts - 1) ts
   300        (0 upto length ts - 1) ts
   301 
   301 
   302 (** Helper clauses **)
   302 (** Helper facts **)
   303 
   303 
   304 fun count_combterm (CombConst ((s, _), _, _)) =
   304 fun count_combterm (CombConst ((s, _), _, _)) =
   305     Symtab.map_entry s (Integer.add 1)
   305     Symtab.map_entry s (Integer.add 1)
   306   | count_combterm (CombVar _) = I
   306   | count_combterm (CombVar _) = I
   307   | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
   307   | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
   322 
   322 
   323 val init_counters =
   323 val init_counters =
   324   Symtab.make (maps (maps (map (rpair 0) o fst))
   324   Symtab.make (maps (maps (map (rpair 0) o fst))
   325                     [optional_helpers, optional_typed_helpers])
   325                     [optional_helpers, optional_typed_helpers])
   326 
   326 
   327 fun get_helper_clauses ctxt is_FO full_types conjectures axclauses =
   327 fun get_helper_facts ctxt is_FO full_types conjectures axioms =
   328   let
   328   let
   329     val ct = fold (fold count_fol_formula) [conjectures, axclauses]
   329     val ct = fold (fold count_fol_formula) [conjectures, axioms] init_counters
   330                   init_counters
       
   331     fun is_needed c = the (Symtab.lookup ct c) > 0
   330     fun is_needed c = the (Symtab.lookup ct c) > 0
   332     val cnfs =
   331   in
   333       (optional_helpers
   332     (optional_helpers
   334        |> full_types ? append optional_typed_helpers
   333      |> full_types ? append optional_typed_helpers
   335        |> maps (fn (ss, ths) =>
   334      |> maps (fn (ss, ths) =>
   336                    if exists is_needed ss then map (`Thm.get_name_hint) ths
   335                  if exists is_needed ss then map (`Thm.get_name_hint) ths
   337                    else [])) @
   336                  else [])) @
   338       (if is_FO then [] else map (`Thm.get_name_hint) mandatory_helpers)
   337     (if is_FO then [] else map (`Thm.get_name_hint) mandatory_helpers)
   339   in map (snd o make_axiom_clause ctxt) cnfs end
   338     |> map (snd o make_axiom ctxt)
       
   339   end
   340 
   340 
   341 fun s_not (@{const Not} $ t) = t
   341 fun s_not (@{const Not} $ t) = t
   342   | s_not t = @{const Not} $ t
   342   | s_not t = @{const Not} $ t
   343 
   343 
   344 fun prepare_clauses ctxt full_types hyp_ts concl_t axcls =
   344 fun prepare_formulas ctxt full_types hyp_ts concl_t axcls =
   345   let
   345   let
   346     val thy = ProofContext.theory_of ctxt
   346     val thy = ProofContext.theory_of ctxt
   347     val goal_t = Logic.list_implies (hyp_ts, concl_t)
   347     val goal_t = Logic.list_implies (hyp_ts, concl_t)
   348     val is_FO = Meson.is_fol_term thy goal_t
   348     val is_FO = Meson.is_fol_term thy goal_t
   349     val axtms = map (prop_of o snd) axcls
   349     val axtms = map (prop_of o snd) axcls
   350     val subs = tfree_classes_of_terms [goal_t]
   350     val subs = tfree_classes_of_terms [goal_t]
   351     val supers = tvar_classes_of_terms axtms
   351     val supers = tvar_classes_of_terms axtms
   352     val tycons = type_consts_of_terms thy (goal_t :: axtms)
   352     val tycons = type_consts_of_terms thy (goal_t :: axtms)
   353     (* TFrees in conjecture clauses; TVars in axiom clauses *)
   353     (* TFrees in the conjecture; TVars in the axioms *)
   354     val conjectures =
   354     val conjectures =
   355       map (s_not o HOLogic.dest_Trueprop) hyp_ts @
   355       map (s_not o HOLogic.dest_Trueprop) hyp_ts @
   356         [HOLogic.dest_Trueprop concl_t]
   356       [HOLogic.dest_Trueprop concl_t]
   357       |> make_conjecture_clauses ctxt
   357       |> make_conjectures ctxt
   358     val (clnames, axioms) = ListPair.unzip (map (make_axiom_clause ctxt) axcls)
   358     val (clnames, axioms) = ListPair.unzip (map (make_axiom ctxt) axcls)
   359     val helper_clauses =
   359     val helper_facts = get_helper_facts ctxt is_FO full_types conjectures axioms
   360       get_helper_clauses ctxt is_FO full_types conjectures axioms
       
   361     val (supers', arity_clauses) = make_arity_clauses thy tycons supers
   360     val (supers', arity_clauses) = make_arity_clauses thy tycons supers
   362     val class_rel_clauses = make_class_rel_clauses thy subs supers'
   361     val class_rel_clauses = make_class_rel_clauses thy subs supers'
   363   in
   362   in
   364     (Vector.fromList clnames,
   363     (Vector.fromList clnames,
   365       (conjectures, axioms, helper_clauses, class_rel_clauses, arity_clauses))
   364       (conjectures, axioms, helper_facts, class_rel_clauses, arity_clauses))
   366   end
   365   end
   367 
   366 
   368 fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t])
   367 fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t])
   369 
   368 
   370 fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
   369 fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
   583 fun repair_problem thy explicit_forall full_types explicit_apply problem =
   582 fun repair_problem thy explicit_forall full_types explicit_apply problem =
   584   repair_problem_with_const_table thy explicit_forall full_types
   583   repair_problem_with_const_table thy explicit_forall full_types
   585       (const_table_for_problem explicit_apply problem) problem
   584       (const_table_for_problem explicit_apply problem) problem
   586 
   585 
   587 fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply
   586 fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply
   588                     file (conjectures, axioms, helper_clauses,
   587                     file (conjectures, axioms, helper_facts, class_rel_clauses,
   589                           class_rel_clauses, arity_clauses) =
   588                           arity_clauses) =
   590   let
   589   let
   591     val axiom_lines = map (problem_line_for_axiom full_types) axioms
   590     val axiom_lines = map (problem_line_for_axiom full_types) axioms
   592     val class_rel_lines =
   591     val class_rel_lines =
   593       map problem_line_for_class_rel_clause class_rel_clauses
   592       map problem_line_for_class_rel_clause class_rel_clauses
   594     val arity_lines = map problem_line_for_arity_clause arity_clauses
   593     val arity_lines = map problem_line_for_arity_clause arity_clauses
   595     val helper_lines = map (problem_line_for_axiom full_types) helper_clauses
   594     val helper_lines = map (problem_line_for_axiom full_types) helper_facts
   596     val conjecture_lines =
   595     val conjecture_lines =
   597       map (problem_line_for_conjecture full_types) conjectures
   596       map (problem_line_for_conjecture full_types) conjectures
   598     val tfree_lines = problem_lines_for_free_types conjectures
   597     val tfree_lines = problem_lines_for_free_types conjectures
   599     (* Reordering these might or might not confuse the proof reconstruction
   598     (* Reordering these might or might not confuse the proof reconstruction
   600        code or the SPASS Flotter hack. *)
   599        code or the SPASS Flotter hack. *)
   673           relevance_convergence, theory_relevant, defs_relevant, isar_proof,
   672           relevance_convergence, theory_relevant, defs_relevant, isar_proof,
   674           isar_shrink_factor, ...} : params)
   673           isar_shrink_factor, ...} : params)
   675         minimize_command timeout
   674         minimize_command timeout
   676         ({subgoal, goal, relevance_override, axioms} : problem) =
   675         ({subgoal, goal, relevance_override, axioms} : problem) =
   677   let
   676   let
   678     (* get clauses and prepare them for writing *)
       
   679     val (ctxt, (_, th)) = goal;
   677     val (ctxt, (_, th)) = goal;
   680     val thy = ProofContext.theory_of ctxt
   678     val thy = ProofContext.theory_of ctxt
   681     (* ### FIXME: (1) preprocessing for "if" etc. *)
   679     (* ### FIXME: (1) preprocessing for "if" etc. *)
   682     val (params, hyp_ts, concl_t) = strip_subgoal th subgoal
   680     val (params, hyp_ts, concl_t) = strip_subgoal th subgoal
   683     val the_axioms =
   681     val the_axioms =
   686       | NONE => relevant_facts full_types relevance_threshold
   684       | NONE => relevant_facts full_types relevance_threshold
   687                     relevance_convergence defs_relevant
   685                     relevance_convergence defs_relevant
   688                     max_new_relevant_facts_per_iter
   686                     max_new_relevant_facts_per_iter
   689                     (the_default prefers_theory_relevant theory_relevant)
   687                     (the_default prefers_theory_relevant theory_relevant)
   690                     relevance_override goal hyp_ts concl_t
   688                     relevance_override goal hyp_ts concl_t
   691     val (internal_thm_names, clauses) =
   689     val (internal_thm_names, formulas) =
   692       prepare_clauses ctxt full_types hyp_ts concl_t the_axioms
   690       prepare_formulas ctxt full_types hyp_ts concl_t the_axioms
   693 
   691 
   694     (* path to unique problem file *)
   692     (* path to unique problem file *)
   695     val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
   693     val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
   696                        else Config.get ctxt dest_dir;
   694                        else Config.get ctxt dest_dir;
   697     val the_problem_prefix = Config.get ctxt problem_prefix;
   695     val the_problem_prefix = Config.get ctxt problem_prefix;
   755                                             known_failures output
   753                                             known_failures output
   756               in (output, msecs, proof, outcome) end
   754               in (output, msecs, proof, outcome) end
   757             val readable_names = debug andalso overlord
   755             val readable_names = debug andalso overlord
   758             val (pool, conjecture_offset) =
   756             val (pool, conjecture_offset) =
   759               write_tptp_file thy readable_names explicit_forall full_types
   757               write_tptp_file thy readable_names explicit_forall full_types
   760                               explicit_apply probfile clauses
   758                               explicit_apply probfile formulas
   761             val conjecture_shape =
   759             val conjecture_shape =
   762               conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
   760               conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
   763               |> map single
   761               |> map single
   764             val result =
   762             val result =
   765               do_run false
   763               do_run false