src/HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
changeset 37509 f39464d971c4
parent 37500 7587b6e63454
child 37570 de5feddaa95c
equal deleted inserted replaced
37508:f9af8a863bd3 37509:f39464d971c4
     8 FIXME: combine with sledgehammer_hol_clause!
     8 FIXME: combine with sledgehammer_hol_clause!
     9 *)
     9 *)
    10 
    10 
    11 signature SLEDGEHAMMER_FOL_CLAUSE =
    11 signature SLEDGEHAMMER_FOL_CLAUSE =
    12 sig
    12 sig
       
    13   val type_wrapper_name : string
    13   val schematic_var_prefix: string
    14   val schematic_var_prefix: string
    14   val fixed_var_prefix: string
    15   val fixed_var_prefix: string
    15   val tvar_prefix: string
    16   val tvar_prefix: string
    16   val tfree_prefix: string
    17   val tfree_prefix: string
    17   val clause_prefix: string
       
    18   val const_prefix: string
    18   val const_prefix: string
    19   val tconst_prefix: string
    19   val tconst_prefix: string
    20   val class_prefix: string
    20   val class_prefix: string
    21   val union_all: ''a list list -> ''a list
    21   val union_all: ''a list list -> ''a list
    22   val const_trans_table: string Symtab.table
    22   val const_trans_table: string Symtab.table
    23   val type_const_trans_table: string Symtab.table
    23   val type_const_trans_table: string Symtab.table
    24   val ascii_of: string -> string
    24   val ascii_of: string -> string
    25   val undo_ascii_of: string -> string
    25   val undo_ascii_of: string -> string
    26   val paren_pack : string list -> string
       
    27   val make_schematic_var : string * int -> string
    26   val make_schematic_var : string * int -> string
    28   val make_fixed_var : string -> string
    27   val make_fixed_var : string -> string
    29   val make_schematic_type_var : string * int -> string
    28   val make_schematic_type_var : string * int -> string
    30   val make_fixed_type_var : string -> string
    29   val make_fixed_type_var : string -> string
    31   val make_fixed_const : string -> string
    30   val make_fixed_const : string -> string
    35   type name_pool = string Symtab.table * string Symtab.table
    34   type name_pool = string Symtab.table * string Symtab.table
    36   val empty_name_pool : bool -> name_pool option
    35   val empty_name_pool : bool -> name_pool option
    37   val pool_map : ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
    36   val pool_map : ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
    38   val nice_name : name -> name_pool option -> string * name_pool option
    37   val nice_name : name -> name_pool option -> string * name_pool option
    39   datatype kind = Axiom | Conjecture
    38   datatype kind = Axiom | Conjecture
    40   datatype fol_type =
       
    41     TyVar of name |
       
    42     TyFree of name |
       
    43     TyConstr of name * fol_type list
       
    44   val string_of_fol_type :
       
    45     fol_type -> name_pool option -> string * name_pool option
       
    46   datatype type_literal =
    39   datatype type_literal =
    47     TyLitVar of string * name |
    40     TyLitVar of string * name |
    48     TyLitFree of string * name
    41     TyLitFree of string * name
    49   exception CLAUSE of string * term
    42   exception CLAUSE of string * term
    50   val type_literals_for_types : typ list -> type_literal list
    43   val type_literals_for_types : typ list -> type_literal list
    55    {axiom_name: string, conclLit: arLit, premLits: arLit list}
    48    {axiom_name: string, conclLit: arLit, premLits: arLit list}
    56   datatype classrel_clause = ClassrelClause of
    49   datatype classrel_clause = ClassrelClause of
    57    {axiom_name: string, subclass: class, superclass: class}
    50    {axiom_name: string, subclass: class, superclass: class}
    58   val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
    51   val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
    59   val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
    52   val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
    60   val tptp_sign: bool -> string -> string
       
    61   val tptp_of_type_literal :
       
    62     bool -> type_literal -> name_pool option -> string * name_pool option
       
    63   val gen_tptp_cls : int * string * kind * string list * string list -> string
       
    64   val tptp_tfree_clause : string -> string
       
    65   val tptp_arity_clause : arity_clause -> string
       
    66   val tptp_classrel_clause : classrel_clause -> string
       
    67 end
    53 end
    68 
    54 
    69 structure Sledgehammer_FOL_Clause : SLEDGEHAMMER_FOL_CLAUSE =
    55 structure Sledgehammer_FOL_Clause : SLEDGEHAMMER_FOL_CLAUSE =
    70 struct
    56 struct
    71 
    57 
    72 open Sledgehammer_Util
    58 open Sledgehammer_Util
    73 
    59 
       
    60 val type_wrapper_name = "ti"
       
    61 
    74 val schematic_var_prefix = "V_";
    62 val schematic_var_prefix = "V_";
    75 val fixed_var_prefix = "v_";
    63 val fixed_var_prefix = "v_";
    76 
    64 
    77 val tvar_prefix = "T_";
    65 val tvar_prefix = "T_";
    78 val tfree_prefix = "t_";
    66 val tfree_prefix = "t_";
    79 
    67 
    80 val clause_prefix = "cls_";
    68 val classrel_clause_prefix = "clsrel_";
    81 val arclause_prefix = "clsarity_"
       
    82 val clrelclause_prefix = "clsrel_";
       
    83 
    69 
    84 val const_prefix = "c_";
    70 val const_prefix = "c_";
    85 val tconst_prefix = "tc_";
    71 val tconst_prefix = "tc_";
    86 val class_prefix = "class_";
    72 val class_prefix = "class_";
    87 
    73 
   149               | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
   135               | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
   150         end
   136         end
   151   | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
   137   | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
   152 
   138 
   153 val undo_ascii_of = undo_ascii_aux [] o String.explode;
   139 val undo_ascii_of = undo_ascii_aux [] o String.explode;
   154 
       
   155 (* convert a list of strings into one single string; surrounded by brackets *)
       
   156 fun paren_pack [] = ""   (*empty argument list*)
       
   157   | paren_pack strings = "(" ^ commas strings ^ ")";
       
   158 
       
   159 fun tptp_clause strings = "(" ^ space_implode " | " strings ^ ")"
       
   160 
   140 
   161 (*Remove the initial ' character from a type variable, if it is present*)
   141 (*Remove the initial ' character from a type variable, if it is present*)
   162 fun trim_type_var s =
   142 fun trim_type_var s =
   163   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   143   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   164   else error ("trim_type: Malformed type variable encountered: " ^ s);
   144   else error ("trim_type: Malformed type variable encountered: " ^ s);
   258 
   238 
   259 datatype kind = Axiom | Conjecture;
   239 datatype kind = Axiom | Conjecture;
   260 
   240 
   261 (**** Isabelle FOL clauses ****)
   241 (**** Isabelle FOL clauses ****)
   262 
   242 
   263 datatype fol_type =
       
   264   TyVar of name |
       
   265   TyFree of name |
       
   266   TyConstr of name * fol_type list
       
   267 
       
   268 fun string_of_fol_type (TyVar sp) pool = nice_name sp pool
       
   269   | string_of_fol_type (TyFree sp) pool = nice_name sp pool
       
   270   | string_of_fol_type (TyConstr (sp, tys)) pool =
       
   271     let
       
   272       val (s, pool) = nice_name sp pool
       
   273       val (ss, pool) = pool_map string_of_fol_type tys pool
       
   274     in (s ^ paren_pack ss, pool) end
       
   275 
       
   276 (* The first component is the type class; the second is a TVar or TFree. *)
   243 (* The first component is the type class; the second is a TVar or TFree. *)
   277 datatype type_literal =
   244 datatype type_literal =
   278   TyLitVar of string * name |
   245   TyLitVar of string * name |
   279   TyLitFree of string * name
   246   TyLitFree of string * name
   280 
   247 
   339         fun add_super sub super = class_less (sub, super) ? cons (sub, super)
   306         fun add_super sub super = class_less (sub, super) ? cons (sub, super)
   340         fun add_supers sub = fold (add_super sub) supers
   307         fun add_supers sub = fold (add_super sub) supers
   341       in fold add_supers subs [] end
   308       in fold add_supers subs [] end
   342 
   309 
   343 fun make_classrel_clause (sub,super) =
   310 fun make_classrel_clause (sub,super) =
   344   ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
   311   ClassrelClause {axiom_name = classrel_clause_prefix ^ ascii_of sub ^ "_" ^
       
   312                                ascii_of super,
   345                   subclass = make_type_class sub,
   313                   subclass = make_type_class sub,
   346                   superclass = make_type_class super};
   314                   superclass = make_type_class super};
   347 
   315 
   348 fun make_classrel_clauses thy subs supers =
   316 fun make_classrel_clauses thy subs supers =
   349   map make_classrel_clause (class_pairs thy subs supers);
   317   map make_classrel_clause (class_pairs thy subs supers);
   388 
   356 
   389 fun make_arity_clauses thy tycons classes =
   357 fun make_arity_clauses thy tycons classes =
   390   let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
   358   let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
   391   in  (classes', multi_arity_clause cpairs)  end;
   359   in  (classes', multi_arity_clause cpairs)  end;
   392 
   360 
   393 
       
   394 (**** Produce TPTP files ****)
       
   395 
       
   396 fun string_of_clausename (cls_id, ax_name) =
       
   397     clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id
       
   398 
       
   399 fun tptp_sign true s = s
       
   400   | tptp_sign false s = "~ " ^ s
       
   401 
       
   402 fun tptp_of_type_literal pos (TyLitVar (s, name)) =
       
   403     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
       
   404   | tptp_of_type_literal pos (TyLitFree (s, name)) =
       
   405     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
       
   406 
       
   407 fun tptp_cnf name kind formula =
       
   408   "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
       
   409 
       
   410 fun gen_tptp_cls (cls_id, ax_name, Axiom, lits, tylits) =
       
   411       tptp_cnf (string_of_clausename (cls_id, ax_name)) "axiom"
       
   412                (tptp_clause (tylits @ lits))
       
   413   | gen_tptp_cls (cls_id, ax_name, Conjecture, lits, _) =
       
   414       tptp_cnf (string_of_clausename (cls_id, ax_name)) "negated_conjecture"
       
   415                (tptp_clause lits)
       
   416 
       
   417 fun tptp_tfree_clause tfree_lit =
       
   418     tptp_cnf "tfree_tcs" "negated_conjecture" (tptp_clause [tfree_lit])
       
   419 
       
   420 fun tptp_of_arLit (TConsLit (c,t,args)) =
       
   421       tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
       
   422   | tptp_of_arLit (TVarLit (c,str)) =
       
   423       tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
       
   424 
       
   425 fun tptp_arity_clause (ArityClause {axiom_name, conclLit, premLits, ...}) =
       
   426   tptp_cnf (arclause_prefix ^ ascii_of axiom_name) "axiom"
       
   427            (tptp_clause (map tptp_of_arLit (conclLit :: premLits)))
       
   428 
       
   429 fun tptp_classrelLits sub sup =
       
   430   let val tvar = "(T)"
       
   431   in  tptp_clause [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
       
   432 
       
   433 fun tptp_classrel_clause (ClassrelClause {axiom_name, subclass, superclass,
       
   434                                           ...}) =
       
   435   tptp_cnf axiom_name "axiom" (tptp_classrelLits subclass superclass)
       
   436 
       
   437 end;
   361 end;