src/HOL/Tools/res_hol_clause.ML
changeset 31409 d8537ba165b5
parent 30242 aea5d7fa7ef5
child 31749 8ee34e3ceb5a
equal deleted inserted replaced
31408:9f2ca03ae7b7 31409:d8537ba165b5
    21   datatype combterm =
    21   datatype combterm =
    22       CombConst of string * ResClause.fol_type * ResClause.fol_type list (*Const and Free*)
    22       CombConst of string * ResClause.fol_type * ResClause.fol_type list (*Const and Free*)
    23     | CombVar of string * ResClause.fol_type
    23     | CombVar of string * ResClause.fol_type
    24     | CombApp of combterm * combterm
    24     | CombApp of combterm * combterm
    25   datatype literal = Literal of polarity * combterm
    25   datatype literal = Literal of polarity * combterm
       
    26   datatype clause = Clause of {clause_id: clause_id, axiom_name: axiom_name, th: thm,
       
    27                     kind: ResClause.kind,literals: literal list, ctypes_sorts: typ list}
    26   val strip_comb: combterm -> combterm * combterm list
    28   val strip_comb: combterm -> combterm * combterm list
    27   val literals_of_term: theory -> term -> literal list * typ list
    29   val literals_of_term: theory -> term -> literal list * typ list
    28   exception TOO_TRIVIAL
    30   exception TOO_TRIVIAL
    29   val tptp_write_file: theory -> bool -> thm list -> string ->
    31   val count_constants: clause list * clause list * clause list * 'a * 'b ->
    30     (thm * (axiom_name * clause_id)) list * ResClause.classrelClause list *
    32     int Symtab.table * bool Symtab.table
    31       ResClause.arityClause list -> string list -> axiom_name list
    33   val prepare_clauses: bool -> theory -> bool -> thm list -> (thm * (axiom_name * clause_id)) list ->
    32   val dfg_write_file: theory -> bool -> thm list -> string ->
    34     string list -> axiom_name list * (clause list * clause list * clause list)
    33     (thm * (axiom_name * clause_id)) list * ResClause.classrelClause list *
    35   val tptp_write_file: string -> int Symtab.table * bool Symtab.table ->
    34       ResClause.arityClause list -> string list -> axiom_name list
    36     clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
       
    37   val dfg_write_file: string -> int Symtab.table * bool Symtab.table ->
       
    38     clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
    35 end
    39 end
    36 
    40 
    37 structure ResHolClause: RES_HOL_CLAUSE =
    41 structure ResHolClause: RES_HOL_CLAUSE =
    38 struct
    42 struct
    39 
    43 
   292   the latter should only occur in conjecture clauses.*)
   296   the latter should only occur in conjecture clauses.*)
   293 fun tptp_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   297 fun tptp_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
   294       (map (tptp_literal cma cnh) literals, 
   298       (map (tptp_literal cma cnh) literals, 
   295        map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
   299        map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
   296 
   300 
   297 fun clause2tptp cma cnh (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   301 fun clause2tptp (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   298   let val (lits,tylits) = tptp_type_lits cma cnh (kind = RC.Conjecture) cls
   302   let val (lits,tylits) = tptp_type_lits cma cnh (kind = RC.Conjecture) cls
   299   in
   303   in
   300       (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
   304       (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
   301   end;
   305   end;
   302 
   306 
   315 
   319 
   316 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   320 fun get_uvars_l (Literal(_,c)) = get_uvars c [];
   317 
   321 
   318 fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   322 fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
   319 
   323 
   320 fun clause2dfg cma cnh (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   324 fun clause2dfg (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
   321   let val (lits,tylits) = dfg_type_lits cma cnh (kind = RC.Conjecture) cls
   325   let val (lits,tylits) = dfg_type_lits cma cnh (kind = RC.Conjecture) cls
   322       val vars = dfg_vars cls
   326       val vars = dfg_vars cls
   323       val tvars = RC.get_tvar_strs ctypes_sorts
   327       val tvars = RC.get_tvar_strs ctypes_sorts
   324   in
   328   in
   325       (RC.gen_dfg_cls(clause_id, axiom_name, kind, lits, tylits, tvars@vars), tylits)
   329       (RC.gen_dfg_cls(clause_id, axiom_name, kind, lits, tylits, tvars@vars), tylits)
   348 
   352 
   349 fun add_clause_decls cma cnh (Clause {literals, ...}, decls) =
   353 fun add_clause_decls cma cnh (Clause {literals, ...}, decls) =
   350     List.foldl (add_literal_decls cma cnh) decls literals
   354     List.foldl (add_literal_decls cma cnh) decls literals
   351     handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
   355     handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
   352 
   356 
   353 fun decls_of_clauses cma cnh clauses arity_clauses =
   357 fun decls_of_clauses (cma, cnh) clauses arity_clauses =
   354   let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
   358   let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
   355       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   359       val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
   356       val (functab,predtab) = (List.foldl (add_clause_decls cma cnh) (init_functab, init_predtab) clauses)
   360       val (functab,predtab) = (List.foldl (add_clause_decls cma cnh) (init_functab, init_predtab) clauses)
   357   in
   361   in
   358       (Symtab.dest (List.foldl RC.add_arityClause_funcs functab arity_clauses),
   362       (Symtab.dest (List.foldl RC.add_arityClause_funcs functab arity_clauses),
   446 
   450 
   447 fun display_arity const_needs_hBOOL (c,n) =
   451 fun display_arity const_needs_hBOOL (c,n) =
   448   Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
   452   Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
   449                 (if needs_hBOOL const_needs_hBOOL c then " needs hBOOL" else ""));
   453                 (if needs_hBOOL const_needs_hBOOL c then " needs hBOOL" else ""));
   450 
   454 
   451 fun count_constants (conjectures, axclauses, helper_clauses) =
   455 fun count_constants (conjectures, axclauses, helper_clauses, _, _) =
   452   if minimize_applies then
   456   if minimize_applies then
   453      let val (const_min_arity, const_needs_hBOOL) =
   457      let val (const_min_arity, const_needs_hBOOL) =
   454           fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
   458           fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
   455        |> fold count_constants_clause axclauses
   459        |> fold count_constants_clause axclauses
   456        |> fold count_constants_clause helper_clauses
   460        |> fold count_constants_clause helper_clauses
   458      in (const_min_arity, const_needs_hBOOL) end
   462      in (const_min_arity, const_needs_hBOOL) end
   459   else (Symtab.empty, Symtab.empty);
   463   else (Symtab.empty, Symtab.empty);
   460 
   464 
   461 (* tptp format *)
   465 (* tptp format *)
   462 
   466 
       
   467 fun prepare_clauses dfg thy isFO thms ax_tuples user_lemmas =
       
   468   let
       
   469     val conjectures = make_conjecture_clauses dfg thy thms
       
   470     val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses dfg thy ax_tuples)
       
   471     val helper_clauses = get_helper_clauses dfg thy isFO (conjectures, axclauses, user_lemmas)
       
   472   in
       
   473     (clnames, (conjectures, axclauses, helper_clauses))
       
   474   end
       
   475 
   463 (* write TPTP format to a single file *)
   476 (* write TPTP format to a single file *)
   464 fun tptp_write_file thy isFO thms filename (ax_tuples,classrel_clauses,arity_clauses) user_lemmas =
   477 fun tptp_write_file filename const_counts (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) =
   465     let val _ = Output.debug (fn () => ("Preparing to write the TPTP file " ^ filename))
   478   let
   466         val conjectures = make_conjecture_clauses false thy thms
   479     val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_counts) conjectures)
   467         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses false thy ax_tuples)
   480     val tfree_clss = map RC.tptp_tfree_clause (List.foldl (op union_string) [] tfree_litss)
   468         val helper_clauses = get_helper_clauses false thy isFO (conjectures, axclauses, user_lemmas)
   481     val out = TextIO.openOut filename
   469         val (const_min_arity, const_needs_hBOOL) = count_constants (conjectures, axclauses, helper_clauses);
   482   in
   470         val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_min_arity const_needs_hBOOL) conjectures)
   483     List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) axclauses;
   471         val tfree_clss = map RC.tptp_tfree_clause (List.foldl (op union_string) [] tfree_litss)
   484     RC.writeln_strs out tfree_clss;
   472         val out = TextIO.openOut filename
   485     RC.writeln_strs out tptp_clss;
   473     in
   486     List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   474         List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity const_needs_hBOOL)) axclauses;
   487     List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
   475         RC.writeln_strs out tfree_clss;
   488     List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) helper_clauses;
   476         RC.writeln_strs out tptp_clss;
   489     TextIO.closeOut out
   477         List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
   490   end;
   478         List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
       
   479         List.app (curry TextIO.output out o #1 o (clause2tptp const_min_arity const_needs_hBOOL)) helper_clauses;
       
   480         TextIO.closeOut out;
       
   481         clnames
       
   482     end;
       
   483 
   491 
   484 
   492 
   485 (* dfg format *)
   493 (* dfg format *)
   486 
   494 
   487 fun dfg_write_file thy isFO thms filename (ax_tuples,classrel_clauses,arity_clauses) user_lemmas =
   495 fun dfg_write_file filename const_counts (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) =
   488     let val _ = Output.debug (fn () => ("Preparing to write the DFG file " ^ filename))
   496   let
   489         val conjectures = make_conjecture_clauses true thy thms
   497     val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_counts) conjectures)
   490         val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses true thy ax_tuples)
   498     and probname = Path.implode (Path.base (Path.explode filename))
   491         val helper_clauses = get_helper_clauses true thy isFO (conjectures, axclauses, user_lemmas)
   499     val axstrs = map (#1 o (clause2dfg const_counts)) axclauses
   492         val (const_min_arity, const_needs_hBOOL) = count_constants (conjectures, axclauses, helper_clauses);
   500     val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   493         val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_min_arity const_needs_hBOOL) conjectures)
   501     val out = TextIO.openOut filename
   494         and probname = Path.implode (Path.base (Path.explode filename))
   502     val helper_clauses_strs = map (#1 o (clause2dfg const_counts)) helper_clauses
   495         val axstrs = map (#1 o (clause2dfg const_min_arity const_needs_hBOOL)) axclauses
   503     val (funcs,cl_preds) = decls_of_clauses const_counts (helper_clauses @ conjectures @ axclauses) arity_clauses
   496         val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
   504     and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   497         val out = TextIO.openOut filename
   505   in
   498         val helper_clauses_strs = map (#1 o (clause2dfg const_min_arity const_needs_hBOOL)) helper_clauses
   506     TextIO.output (out, RC.string_of_start probname);
   499         val (funcs,cl_preds) = decls_of_clauses const_min_arity const_needs_hBOOL (helper_clauses @ conjectures @ axclauses) arity_clauses
   507     TextIO.output (out, RC.string_of_descrip probname);
   500         and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
   508     TextIO.output (out, RC.string_of_symbols
   501     in
   509                           (RC.string_of_funcs funcs)
   502         TextIO.output (out, RC.string_of_start probname);
   510                           (RC.string_of_preds (cl_preds @ ty_preds)));
   503         TextIO.output (out, RC.string_of_descrip probname);
   511     TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   504         TextIO.output (out, RC.string_of_symbols
   512     RC.writeln_strs out axstrs;
   505                               (RC.string_of_funcs funcs)
   513     List.app (curry TextIO.output out o RC.dfg_classrelClause) classrel_clauses;
   506                               (RC.string_of_preds (cl_preds @ ty_preds)));
   514     List.app (curry TextIO.output out o RC.dfg_arity_clause) arity_clauses;
   507         TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
   515     RC.writeln_strs out helper_clauses_strs;
   508         RC.writeln_strs out axstrs;
   516     TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   509         List.app (curry TextIO.output out o RC.dfg_classrelClause) classrel_clauses;
   517     RC.writeln_strs out tfree_clss;
   510         List.app (curry TextIO.output out o RC.dfg_arity_clause) arity_clauses;
   518     RC.writeln_strs out dfg_clss;
   511         RC.writeln_strs out helper_clauses_strs;
   519     TextIO.output (out, "end_of_list.\n\n");
   512         TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
   520     (*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   513         RC.writeln_strs out tfree_clss;
   521     TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
   514         RC.writeln_strs out dfg_clss;
   522     TextIO.output (out, "end_problem.\n");
   515         TextIO.output (out, "end_of_list.\n\n");
   523     TextIO.closeOut out
   516         (*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
   524   end;
   517         TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
       
   518         TextIO.output (out, "end_problem.\n");
       
   519         TextIO.closeOut out;
       
   520         clnames
       
   521     end;
       
   522 
   525 
   523 end
   526 end