src/HOL/Tools/Sledgehammer/sledgehammer_translate.ML
changeset 38752 6628adcae4a7
parent 38748 69fea359d3f8
child 38786 e46e7a9cb622
child 38818 61cf050f8b2e
equal deleted inserted replaced
38751:01c4d14b2a61 38752:6628adcae4a7
    16   val class_rel_clause_prefix : string
    16   val class_rel_clause_prefix : string
    17   val arity_clause_prefix : string
    17   val arity_clause_prefix : string
    18   val tfrees_name : string
    18   val tfrees_name : string
    19   val prepare_problem :
    19   val prepare_problem :
    20     Proof.context -> bool -> bool -> bool -> bool -> term list -> term
    20     Proof.context -> bool -> bool -> bool -> bool -> term list -> term
    21     -> ((string * bool) * thm) list
    21     -> ((string * 'a) * thm) list
    22     -> string problem * string Symtab.table * int * (string * bool) vector
    22     -> string problem * string Symtab.table * int * (string * 'a) vector
    23 end;
    23 end;
    24 
    24 
    25 structure Sledgehammer_Translate : SLEDGEHAMMER_TRANSLATE =
    25 structure Sledgehammer_Translate : SLEDGEHAMMER_TRANSLATE =
    26 struct
    26 struct
    27 
    27 
    37 val tfrees_name = "tfrees"
    37 val tfrees_name = "tfrees"
    38 
    38 
    39 (* Freshness almost guaranteed! *)
    39 (* Freshness almost guaranteed! *)
    40 val sledgehammer_weak_prefix = "Sledgehammer:"
    40 val sledgehammer_weak_prefix = "Sledgehammer:"
    41 
    41 
    42 datatype fol_formula =
    42 type fol_formula =
    43   FOLFormula of {name: string,
    43   {name: string,
    44                  kind: kind,
    44    kind: kind,
    45                  combformula: (name, combterm) formula,
    45    combformula: (name, combterm) formula,
    46                  ctypes_sorts: typ list}
    46    ctypes_sorts: typ list}
    47 
    47 
    48 fun mk_anot phi = AConn (ANot, [phi])
    48 fun mk_anot phi = AConn (ANot, [phi])
    49 fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
    49 fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
    50 fun mk_ahorn [] phi = phi
    50 fun mk_ahorn [] phi = phi
    51   | mk_ahorn (phi :: phis) psi =
    51   | mk_ahorn (phi :: phis) psi =
   188               |> perhaps (try (HOLogic.dest_Trueprop))
   188               |> perhaps (try (HOLogic.dest_Trueprop))
   189               |> introduce_combinators_in_term ctxt kind
   189               |> introduce_combinators_in_term ctxt kind
   190               |> kind <> Axiom ? freeze_term
   190               |> kind <> Axiom ? freeze_term
   191     val (combformula, ctypes_sorts) = combformula_for_prop thy t []
   191     val (combformula, ctypes_sorts) = combformula_for_prop thy t []
   192   in
   192   in
   193     FOLFormula {name = name, combformula = combformula, kind = kind,
   193     {name = name, combformula = combformula, kind = kind,
   194                 ctypes_sorts = ctypes_sorts}
   194      ctypes_sorts = ctypes_sorts}
   195   end
   195   end
   196 
   196 
   197 fun make_axiom ctxt presimp ((name, chained), th) =
   197 fun make_axiom ctxt presimp ((name, loc), th) =
   198   case make_formula ctxt presimp name Axiom (prop_of th) of
   198   case make_formula ctxt presimp name Axiom (prop_of th) of
   199     FOLFormula {combformula = AAtom (CombConst (("c_True", _), _, _)), ...} =>
   199     {combformula = AAtom (CombConst (("c_True", _), _, _)), ...} => NONE
   200     NONE
   200   | formula => SOME ((name, loc), formula)
   201   | formula => SOME ((name, chained), formula)
       
   202 fun make_conjecture ctxt ts =
   201 fun make_conjecture ctxt ts =
   203   let val last = length ts - 1 in
   202   let val last = length ts - 1 in
   204     map2 (fn j => make_formula ctxt true (Int.toString j)
   203     map2 (fn j => make_formula ctxt true (Int.toString j)
   205                                (if j = last then Conjecture else Hypothesis))
   204                                (if j = last then Conjecture else Hypothesis))
   206          (0 upto last) ts
   205          (0 upto last) ts
   213   | count_combterm (CombVar _) = I
   212   | count_combterm (CombVar _) = I
   214   | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
   213   | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
   215 fun count_combformula (AQuant (_, _, phi)) = count_combformula phi
   214 fun count_combformula (AQuant (_, _, phi)) = count_combformula phi
   216   | count_combformula (AConn (_, phis)) = fold count_combformula phis
   215   | count_combformula (AConn (_, phis)) = fold count_combformula phis
   217   | count_combformula (AAtom tm) = count_combterm tm
   216   | count_combformula (AAtom tm) = count_combterm tm
   218 fun count_fol_formula (FOLFormula {combformula, ...}) =
   217 fun count_fol_formula ({combformula, ...} : fol_formula) =
   219   count_combformula combformula
   218   count_combformula combformula
   220 
   219 
   221 val optional_helpers =
   220 val optional_helpers =
   222   [(["c_COMBI", "c_COMBK"], @{thms COMBI_def COMBK_def}),
   221   [(["c_COMBI", "c_COMBK"], @{thms COMBI_def COMBK_def}),
   223    (["c_COMBB", "c_COMBC"], @{thms COMBB_def COMBC_def}),
   222    (["c_COMBB", "c_COMBC"], @{thms COMBB_def COMBC_def}),
   324     fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
   323     fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
   325       | aux (AConn (c, phis)) = AConn (c, map aux phis)
   324       | aux (AConn (c, phis)) = AConn (c, map aux phis)
   326       | aux (AAtom tm) = AAtom (fo_term_for_combterm full_types tm)
   325       | aux (AAtom tm) = AAtom (fo_term_for_combterm full_types tm)
   327   in aux end
   326   in aux end
   328 
   327 
   329 fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) =
   328 fun formula_for_axiom full_types
       
   329                       ({combformula, ctypes_sorts, ...} : fol_formula) =
   330   mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
   330   mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
   331                 (type_literals_for_types ctypes_sorts))
   331                 (type_literals_for_types ctypes_sorts))
   332            (formula_for_combformula full_types combformula)
   332            (formula_for_combformula full_types combformula)
   333 
   333 
   334 fun problem_line_for_fact prefix full_types
   334 fun problem_line_for_fact prefix full_types (formula as {name, kind, ...}) =
   335                           (formula as FOLFormula {name, kind, ...}) =
       
   336   Fof (prefix ^ ascii_of name, kind, formula_for_axiom full_types formula)
   335   Fof (prefix ^ ascii_of name, kind, formula_for_axiom full_types formula)
   337 
   336 
   338 fun problem_line_for_class_rel_clause (ClassRelClause {name, subclass,
   337 fun problem_line_for_class_rel_clause (ClassRelClause {name, subclass,
   339                                                        superclass, ...}) =
   338                                                        superclass, ...}) =
   340   let val ty_arg = ATerm (("T", "T"), []) in
   339   let val ty_arg = ATerm (("T", "T"), []) in
   355                       o fo_literal_for_arity_literal) premLits)
   354                       o fo_literal_for_arity_literal) premLits)
   356                 (formula_for_fo_literal
   355                 (formula_for_fo_literal
   357                      (fo_literal_for_arity_literal conclLit)))
   356                      (fo_literal_for_arity_literal conclLit)))
   358 
   357 
   359 fun problem_line_for_conjecture full_types
   358 fun problem_line_for_conjecture full_types
   360                                 (FOLFormula {name, kind, combformula, ...}) =
   359                                 ({name, kind, combformula, ...} : fol_formula) =
   361   Fof (conjecture_prefix ^ name, kind,
   360   Fof (conjecture_prefix ^ name, kind,
   362        formula_for_combformula full_types combformula)
   361        formula_for_combformula full_types combformula)
   363 
   362 
   364 fun free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) =
   363 fun free_type_literals_for_conjecture ({ctypes_sorts, ...} : fol_formula) =
   365   map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
   364   map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
   366 
   365 
   367 fun problem_line_for_free_type lit =
   366 fun problem_line_for_free_type lit =
   368   Fof (tfrees_name, Hypothesis, formula_for_fo_literal lit)
   367   Fof (tfrees_name, Hypothesis, formula_for_fo_literal lit)
   369 fun problem_lines_for_free_types conjectures =
   368 fun problem_lines_for_free_types conjectures =