src/HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
changeset 42998 1c80902d0456
parent 42994 fe291ab75eb5
child 43000 bd424c3dde46
equal deleted inserted replaced
42997:038bb26d74b0 42998:1c80902d0456
    79    implemented for readable names. Finally, readable names are, well, more
    79    implemented for readable names. Finally, readable names are, well, more
    80    readable. For these reason, they are enabled by default. *)
    80    readable. For these reason, they are enabled by default. *)
    81 val readable_names =
    81 val readable_names =
    82   Attrib.setup_config_bool @{binding sledgehammer_atp_readable_names} (K true)
    82   Attrib.setup_config_bool @{binding sledgehammer_atp_readable_names} (K true)
    83 
    83 
    84 val type_decl_prefix = "type_"
    84 val type_decl_prefix = "ty_"
    85 val sym_decl_prefix = "sym_"
    85 val sym_decl_prefix = "sy_"
       
    86 val sym_formula_prefix = "sym_"
    86 val fact_prefix = "fact_"
    87 val fact_prefix = "fact_"
    87 val conjecture_prefix = "conj_"
    88 val conjecture_prefix = "conj_"
    88 val helper_prefix = "help_"
    89 val helper_prefix = "help_"
    89 val class_rel_clause_prefix = "crel_";
    90 val class_rel_clause_prefix = "crel_";
    90 val arity_clause_prefix = "arity_"
    91 val arity_clause_prefix = "arity_"
   182 val is_type_sys_fairly_sound = is_type_level_fairly_sound o level_of_type_sys
   183 val is_type_sys_fairly_sound = is_type_level_fairly_sound o level_of_type_sys
   183 
   184 
   184 fun is_setting_higher_order THF (Simple_Types _) = true
   185 fun is_setting_higher_order THF (Simple_Types _) = true
   185   | is_setting_higher_order _ _ = false
   186   | is_setting_higher_order _ _ = false
   186 
   187 
   187 fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
       
   188   | aconn_fold pos f (AImplies, [phi1, phi2]) =
       
   189     f (Option.map not pos) phi1 #> f pos phi2
       
   190   | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
       
   191   | aconn_fold pos f (AOr, phis) = fold (f pos) phis
       
   192   | aconn_fold _ f (_, phis) = fold (f NONE) phis
       
   193 
       
   194 fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
       
   195   | aconn_map pos f (AImplies, [phi1, phi2]) =
       
   196     AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
       
   197   | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
       
   198   | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
       
   199   | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
       
   200 
       
   201 fun formula_fold pos f =
       
   202   let
       
   203     fun aux pos (AQuant (_, _, phi)) = aux pos phi
       
   204       | aux pos (AConn conn) = aconn_fold pos aux conn
       
   205       | aux pos (AAtom tm) = f pos tm
       
   206   in aux pos end
       
   207 
       
   208 type translated_formula =
   188 type translated_formula =
   209   {name: string,
   189   {name: string,
   210    locality: locality,
   190    locality: locality,
   211    kind: formula_kind,
   191    kind: formula_kind,
   212    combformula: (name, typ, combterm) formula,
   192    combformula: (name, typ, combterm) formula,
   280   | combterm_vars (CombConst _) = I
   260   | combterm_vars (CombConst _) = I
   281   | combterm_vars (CombVar (name, T)) = insert (op =) (name, SOME T)
   261   | combterm_vars (CombVar (name, T)) = insert (op =) (name, SOME T)
   282 fun close_combformula_universally phi = close_universally combterm_vars phi
   262 fun close_combformula_universally phi = close_universally combterm_vars phi
   283 
   263 
   284 fun term_vars (ATerm (name as (s, _), tms)) =
   264 fun term_vars (ATerm (name as (s, _), tms)) =
   285   is_atp_variable s ? insert (op =) (name, NONE)
   265   is_tptp_variable s ? insert (op =) (name, NONE) #> fold term_vars tms
   286   #> fold term_vars tms
       
   287 fun close_formula_universally phi = close_universally term_vars phi
   266 fun close_formula_universally phi = close_universally term_vars phi
   288 
   267 
   289 val homo_infinite_type_name = @{type_name ind} (* any infinite type *)
   268 val homo_infinite_type_name = @{type_name ind} (* any infinite type *)
   290 val homo_infinite_type = Type (homo_infinite_type_name, [])
   269 val homo_infinite_type = Type (homo_infinite_type_name, [])
   291 
   270 
   309 fun generic_mangled_type_name f (ATerm (name, [])) = f name
   288 fun generic_mangled_type_name f (ATerm (name, [])) = f name
   310   | generic_mangled_type_name f (ATerm (name, tys)) =
   289   | generic_mangled_type_name f (ATerm (name, tys)) =
   311     f name ^ "(" ^ space_implode "," (map (generic_mangled_type_name f) tys)
   290     f name ^ "(" ^ space_implode "," (map (generic_mangled_type_name f) tys)
   312     ^ ")"
   291     ^ ")"
   313 
   292 
       
   293 val bool_atype = AType (`I tptp_bool_type)
       
   294 
   314 fun ho_type_from_fo_term higher_order pred_sym ary =
   295 fun ho_type_from_fo_term higher_order pred_sym ary =
   315   let
   296   let
   316     fun to_atype ty =
   297     fun to_atype ty =
   317       AType ((make_simple_type (generic_mangled_type_name fst ty),
   298       AType ((make_simple_type (generic_mangled_type_name fst ty),
   318               generic_mangled_type_name snd ty))
   299               generic_mangled_type_name snd ty))
   319     fun to_afun f1 f2 tys = AFun (f1 (hd tys), f2 (nth tys 1))
   300     fun to_afun f1 f2 tys = AFun (f1 (hd tys), f2 (nth tys 1))
   320     fun to_fo 0 ty =
   301     fun to_fo 0 ty = if pred_sym then bool_atype else to_atype ty
   321         if pred_sym then AType (`I tptp_bool_type) else to_atype ty
       
   322       | to_fo ary (ATerm (_, tys)) = to_afun to_atype (to_fo (ary - 1)) tys
   302       | to_fo ary (ATerm (_, tys)) = to_afun to_atype (to_fo (ary - 1)) tys
   323     fun to_ho (ty as ATerm ((s, _), tys)) =
   303     fun to_ho (ty as ATerm ((s, _), tys)) =
   324       if s = tptp_fun_type then to_afun to_ho to_ho tys else to_atype ty
   304       if s = tptp_fun_type then to_afun to_ho to_ho tys else to_atype ty
   325   in if higher_order then to_ho else to_fo ary end
   305   in if higher_order then to_ho else to_fo ary end
   326 
   306 
  1011     if exists (curry (type_instance ctxt) T o get_T) xs then xs
   991     if exists (curry (type_instance ctxt) T o get_T) xs then xs
  1012     else x :: filter_out (curry (type_instance ctxt o swap) T o get_T) xs
   992     else x :: filter_out (curry (type_instance ctxt o swap) T o get_T) xs
  1013   end
   993   end
  1014 
   994 
  1015 fun should_declare_sym type_sys pred_sym s =
   995 fun should_declare_sym type_sys pred_sym s =
  1016   not (String.isPrefix bound_var_prefix s) andalso s <> "equal" andalso
   996   is_tptp_user_symbol s andalso not (String.isPrefix bound_var_prefix s) andalso
  1017   not (String.isPrefix tptp_special_prefix s) andalso
       
  1018   (case type_sys of
   997   (case type_sys of
  1019      Simple_Types _ => true
   998      Simple_Types _ => true
  1020    | Tags (_, _, Light) => true
   999    | Tags (_, _, Light) => true
  1021    | _ => not pred_sym)
  1000    | _ => not pred_sym)
  1022 
  1001 
  1084     val (higher_order, T_arg_Ts, level) =
  1063     val (higher_order, T_arg_Ts, level) =
  1085       case type_sys of
  1064       case type_sys of
  1086         Simple_Types level => (format = THF, [], level)
  1065         Simple_Types level => (format = THF, [], level)
  1087       | _ => (false, replicate (length T_args) homo_infinite_type, No_Types)
  1066       | _ => (false, replicate (length T_args) homo_infinite_type, No_Types)
  1088   in
  1067   in
  1089     Decl (type_decl_prefix ^ s, (s, s'),
  1068     Decl (sym_decl_prefix ^ s, (s, s'),
  1090           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1069           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1091           |> mangled_type higher_order pred_sym (length T_arg_Ts + ary))
  1070           |> mangled_type higher_order pred_sym (length T_arg_Ts + ary))
  1092   end
  1071   end
  1093 
  1072 
  1094 fun is_polymorphic_type T = fold_atyps (fn TVar _ => K true | _ => I) T false
  1073 fun is_polymorphic_type T = fold_atyps (fn TVar _ => K true | _ => I) T false
  1106       bound_names ~~ arg_Ts |> map (fn (name, T) => CombConst (name, T, []))
  1085       bound_names ~~ arg_Ts |> map (fn (name, T) => CombConst (name, T, []))
  1107     val bound_Ts =
  1086     val bound_Ts =
  1108       arg_Ts |> map (fn T => if n > 1 orelse is_polymorphic_type T then SOME T
  1087       arg_Ts |> map (fn T => if n > 1 orelse is_polymorphic_type T then SOME T
  1109                              else NONE)
  1088                              else NONE)
  1110   in
  1089   in
  1111     Formula (sym_decl_prefix ^ s ^
  1090     Formula (sym_formula_prefix ^ s ^
  1112              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  1091              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  1113              CombConst ((s, s'), T, T_args)
  1092              CombConst ((s, s'), T, T_args)
  1114              |> fold (curry (CombApp o swap)) bounds
  1093              |> fold (curry (CombApp o swap)) bounds
  1115              |> type_pred_combterm ctxt nonmono_Ts type_sys res_T
  1094              |> type_pred_combterm ctxt nonmono_Ts type_sys res_T
  1116              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  1095              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  1124 
  1103 
  1125 fun formula_lines_for_tag_sym_decl ctxt format conj_sym_kind nonmono_Ts type_sys
  1104 fun formula_lines_for_tag_sym_decl ctxt format conj_sym_kind nonmono_Ts type_sys
  1126         n s (j, (s', T_args, T, pred_sym, ary, in_conj)) =
  1105         n s (j, (s', T_args, T, pred_sym, ary, in_conj)) =
  1127   let
  1106   let
  1128     val ident_base =
  1107     val ident_base =
  1129       sym_decl_prefix ^ s ^ (if n > 1 then "_" ^ string_of_int j else "")
  1108       sym_formula_prefix ^ s ^ (if n > 1 then "_" ^ string_of_int j else "")
  1130     val (kind, maybe_negate) =
  1109     val (kind, maybe_negate) =
  1131       if in_conj then (conj_sym_kind, conj_sym_kind = Conjecture ? mk_anot)
  1110       if in_conj then (conj_sym_kind, conj_sym_kind = Conjecture ? mk_anot)
  1132       else (Axiom, I)
  1111       else (Axiom, I)
  1133     val (arg_Ts, res_T) = chop_fun ary T
  1112     val (arg_Ts, res_T) = chop_fun ary T
  1134     val bound_names =
  1113     val bound_names =
  1171 
  1150 
  1172 fun result_type_of_decl (_, _, T, _, ary, _) = chop_fun ary T |> snd
  1151 fun result_type_of_decl (_, _, T, _, ary, _) = chop_fun ary T |> snd
  1173 
  1152 
  1174 fun problem_lines_for_sym_decls ctxt format conj_sym_kind nonmono_Ts type_sys
  1153 fun problem_lines_for_sym_decls ctxt format conj_sym_kind nonmono_Ts type_sys
  1175                                 (s, decls) =
  1154                                 (s, decls) =
  1176   (if member (op =) [TFF, THF] format then
  1155   case type_sys of
  1177      decls |> map (decl_line_for_sym ctxt format nonmono_Ts type_sys s)
  1156     Simple_Types _ =>
  1178    else
  1157     decls |> map (decl_line_for_sym ctxt format nonmono_Ts type_sys s)
  1179      []) @
  1158   | Preds _ =>
  1180   (case type_sys of
  1159     let
  1181      Simple_Types _ => []
  1160       val decls =
  1182    | Preds _ =>
  1161         case decls of
  1183      let
  1162           decl :: (decls' as _ :: _) =>
  1184        val decls =
  1163           let val T = result_type_of_decl decl in
  1185          case decls of
  1164             if forall (curry (type_instance ctxt o swap) T
  1186            decl :: (decls' as _ :: _) =>
  1165                        o result_type_of_decl) decls' then
  1187            let val T = result_type_of_decl decl in
  1166               [decl]
  1188              if forall (curry (type_instance ctxt o swap) T
  1167             else
  1189                         o result_type_of_decl) decls' then
  1168               decls
  1190                [decl]
  1169           end
  1191              else
  1170         | _ => decls
  1192                decls
  1171       val n = length decls
  1193            end
  1172       val decls =
  1194          | _ => decls
  1173         decls
  1195        val n = length decls
  1174         |> filter (should_predicate_on_type ctxt nonmono_Ts type_sys (K true)
  1196        val decls =
  1175                    o result_type_of_decl)
  1197          decls
  1176     in
  1198          |> filter (should_predicate_on_type ctxt nonmono_Ts type_sys (K true)
  1177       (0 upto length decls - 1, decls)
  1199                     o result_type_of_decl)
  1178       |-> map2 (formula_line_for_pred_sym_decl ctxt format conj_sym_kind
  1200      in
  1179                                                nonmono_Ts type_sys n s)
  1201        (0 upto length decls - 1, decls)
  1180     end
  1202        |-> map2 (formula_line_for_pred_sym_decl ctxt format conj_sym_kind
  1181   | Tags (_, _, heaviness) =>
  1203                                                 nonmono_Ts type_sys n s)
  1182     (case heaviness of
  1204      end
  1183        Heavy => []
  1205    | Tags (_, _, heaviness) =>
  1184      | Light =>
  1206      (case heaviness of
  1185        let val n = length decls in
  1207         Heavy => []
  1186          (0 upto n - 1 ~~ decls)
  1208       | Light =>
  1187          |> maps (formula_lines_for_tag_sym_decl ctxt format conj_sym_kind
  1209         let val n = length decls in
  1188                                                  nonmono_Ts type_sys n s)
  1210           (0 upto n - 1 ~~ decls)
  1189        end)
  1211           |> maps (formula_lines_for_tag_sym_decl ctxt format conj_sym_kind
       
  1212                                                   nonmono_Ts type_sys n s)
       
  1213         end))
       
  1214 
  1190 
  1215 fun problem_lines_for_sym_decl_table ctxt format conj_sym_kind nonmono_Ts
  1191 fun problem_lines_for_sym_decl_table ctxt format conj_sym_kind nonmono_Ts
  1216                                      type_sys sym_decl_tab =
  1192                                      type_sys sym_decl_tab =
  1217   Symtab.fold_rev
  1193   sym_decl_tab
  1218       (append o problem_lines_for_sym_decls ctxt format conj_sym_kind nonmono_Ts
  1194   |> Symtab.dest
  1219                                             type_sys)
  1195   |> sort_wrt fst
  1220       sym_decl_tab []
  1196   |> rpair []
  1221 
  1197   |-> fold_rev (append o problem_lines_for_sym_decls ctxt format conj_sym_kind
  1222 fun add_simple_types_in_type (AFun (ty1, ty2)) =
  1198                                                      nonmono_Ts type_sys)
  1223     fold add_simple_types_in_type [ty1, ty2]
       
  1224   | add_simple_types_in_type (AType name) = insert (op =) name
       
  1225 
       
  1226 fun add_simple_types_in_formula (AQuant (_, xs, phi)) =
       
  1227     fold add_simple_types_in_type (map_filter snd xs)
       
  1228     #> add_simple_types_in_formula phi
       
  1229   | add_simple_types_in_formula (AConn (_, phis)) =
       
  1230     fold add_simple_types_in_formula phis
       
  1231   | add_simple_types_in_formula (AAtom _) = I
       
  1232 
       
  1233 fun add_simple_types_in_problem_line (Decl (_, _, ty)) =
       
  1234     add_simple_types_in_type ty
       
  1235   | add_simple_types_in_problem_line (Formula (_, _, phi, _, _)) =
       
  1236     add_simple_types_in_formula phi
       
  1237 
       
  1238 fun simple_types_in_problem problem =
       
  1239   fold (fold add_simple_types_in_problem_line o snd) problem []
       
  1240   |> filter_out (String.isPrefix tptp_special_prefix o fst)
       
  1241 
       
  1242 fun decl_line_for_simple_type (s, s') =
       
  1243   Decl (type_decl_prefix ^ s, (s, s'), AType (`I tptp_type_of_types))
       
  1244 
  1199 
  1245 fun should_add_ti_ti_helper (Tags (Polymorphic, level, Heavy)) =
  1200 fun should_add_ti_ti_helper (Tags (Polymorphic, level, Heavy)) =
  1246     level = Nonmonotonic_Types orelse level = Finite_Types
  1201     level = Nonmonotonic_Types orelse level = Finite_Types
  1247   | should_add_ti_ti_helper _ = false
  1202   | should_add_ti_ti_helper _ = false
  1248 
  1203 
  1249 fun offset_of_heading_in_problem _ [] j = j
  1204 fun offset_of_heading_in_problem _ [] j = j
  1250   | offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
  1205   | offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
  1251     if heading = needle then j
  1206     if heading = needle then j
  1252     else offset_of_heading_in_problem needle problem (j + length lines)
  1207     else offset_of_heading_in_problem needle problem (j + length lines)
  1253 
  1208 
  1254 val type_declsN = "Types"
  1209 val implicit_declsN = "Should-be-implicit typings"
  1255 val sym_declsN = "Symbol types"
  1210 val explicit_declsN = "Explicit typings"
  1256 val factsN = "Relevant facts"
  1211 val factsN = "Relevant facts"
  1257 val class_relsN = "Class relationships"
  1212 val class_relsN = "Class relationships"
  1258 val aritiesN = "Arities"
  1213 val aritiesN = "Arities"
  1259 val helpersN = "Helper facts"
  1214 val helpersN = "Helper facts"
  1260 val conjsN = "Conjectures"
  1215 val conjsN = "Conjectures"
  1291       |> (if should_add_ti_ti_helper type_sys then cons (ti_ti_helper_fact ())
  1246       |> (if should_add_ti_ti_helper type_sys then cons (ti_ti_helper_fact ())
  1292           else I)
  1247           else I)
  1293     (* Reordering these might confuse the proof reconstruction code or the SPASS
  1248     (* Reordering these might confuse the proof reconstruction code or the SPASS
  1294        Flotter hack. *)
  1249        Flotter hack. *)
  1295     val problem =
  1250     val problem =
  1296       [(sym_declsN, sym_decl_lines),
  1251       [(explicit_declsN, sym_decl_lines),
  1297        (factsN,
  1252        (factsN,
  1298         map (formula_line_for_fact ctxt format fact_prefix nonmono_Ts type_sys)
  1253         map (formula_line_for_fact ctxt format fact_prefix nonmono_Ts type_sys)
  1299             (0 upto length facts - 1 ~~ facts)),
  1254             (0 upto length facts - 1 ~~ facts)),
  1300        (class_relsN, map formula_line_for_class_rel_clause class_rel_clauses),
  1255        (class_relsN, map formula_line_for_class_rel_clause class_rel_clauses),
  1301        (aritiesN, map formula_line_for_arity_clause arity_clauses),
  1256        (aritiesN, map formula_line_for_arity_clause arity_clauses),
  1305             conjs),
  1260             conjs),
  1306        (free_typesN,
  1261        (free_typesN,
  1307         formula_lines_for_free_types format type_sys (facts @ conjs))]
  1262         formula_lines_for_free_types format type_sys (facts @ conjs))]
  1308     val problem =
  1263     val problem =
  1309       problem
  1264       problem
  1310       |> (case type_sys of
  1265       |> (if format = CNF_UEQ then filter_cnf_ueq_problem else I)
  1311             Simple_Types _ =>
  1266       |> (if is_format_typed format then
  1312             cons (type_declsN, problem |> simple_types_in_problem
  1267             declare_undeclared_syms_in_atp_problem type_decl_prefix
  1313                                        |> map decl_line_for_simple_type)
  1268                                                    implicit_declsN
  1314           | _ => I)
  1269           else
  1315     val problem =
  1270             I)
  1316       problem |> (if format = CNF_UEQ then filter_cnf_ueq_problem else I)
       
  1317     val (problem, pool) =
  1271     val (problem, pool) =
  1318       problem |> nice_atp_problem (Config.get ctxt readable_names)
  1272       problem |> nice_atp_problem (Config.get ctxt readable_names)
  1319     val helpers_offset = offset_of_heading_in_problem helpersN problem 0
  1273     val helpers_offset = offset_of_heading_in_problem helpersN problem 0
  1320     val typed_helpers =
  1274     val typed_helpers =
  1321       map_filter (fn (j, {name, ...}) =>
  1275       map_filter (fn (j, {name, ...}) =>
  1346 val fact_min_weight = 0.2
  1300 val fact_min_weight = 0.2
  1347 val fact_max_weight = 1.0
  1301 val fact_max_weight = 1.0
  1348 val type_info_default_weight = 0.8
  1302 val type_info_default_weight = 0.8
  1349 
  1303 
  1350 fun add_term_weights weight (ATerm (s, tms)) =
  1304 fun add_term_weights weight (ATerm (s, tms)) =
  1351   (not (is_atp_variable s) andalso s <> "equal" andalso
  1305   is_tptp_user_symbol s ? Symtab.default (s, weight)
  1352    not (String.isPrefix tptp_special_prefix s)) ? Symtab.default (s, weight)
       
  1353   #> fold (add_term_weights weight) tms
  1306   #> fold (add_term_weights weight) tms
  1354 fun add_problem_line_weights weight (Formula (_, _, phi, _, _)) =
  1307 fun add_problem_line_weights weight (Formula (_, _, phi, _, _)) =
  1355     formula_fold NONE (K (add_term_weights weight)) phi
  1308     formula_fold NONE (K (add_term_weights weight)) phi
  1356   | add_problem_line_weights _ _ = I
  1309   | add_problem_line_weights _ _ = I
  1357 
  1310 
  1378   let val get = these o AList.lookup (op =) problem in
  1331   let val get = these o AList.lookup (op =) problem in
  1379     Symtab.empty
  1332     Symtab.empty
  1380     |> add_conjectures_weights (get free_typesN @ get conjsN)
  1333     |> add_conjectures_weights (get free_typesN @ get conjsN)
  1381     |> add_facts_weights (get factsN)
  1334     |> add_facts_weights (get factsN)
  1382     |> fold (fold (add_problem_line_weights type_info_default_weight) o get)
  1335     |> fold (fold (add_problem_line_weights type_info_default_weight) o get)
  1383             [sym_declsN, class_relsN, aritiesN]
  1336             [explicit_declsN, class_relsN, aritiesN]
  1384     |> Symtab.dest
  1337     |> Symtab.dest
  1385     |> sort (prod_ord Real.compare string_ord o pairself swap)
  1338     |> sort (prod_ord Real.compare string_ord o pairself swap)
  1386   end
  1339   end
  1387 
  1340 
  1388 end;
  1341 end;