src/HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
changeset 36556 81dc2c20f052
parent 36493 a3357a631b96
child 36692 54b64d4ad524
equal deleted inserted replaced
36555:8ff45c2076da 36556:81dc2c20f052
    42     TyVar of name |
    42     TyVar of name |
    43     TyFree of name |
    43     TyFree of name |
    44     TyConstr of name * fol_type list
    44     TyConstr of name * fol_type list
    45   val string_of_fol_type :
    45   val string_of_fol_type :
    46     fol_type -> name_pool option -> string * name_pool option
    46     fol_type -> name_pool option -> string * name_pool option
    47   datatype type_literal = LTVar of string * string | LTFree of string * string
    47   datatype type_literal =
       
    48     TyLitVar of string * name |
       
    49     TyLitFree of string * name
    48   exception CLAUSE of string * term
    50   exception CLAUSE of string * term
    49   val add_typs : typ list -> type_literal list
    51   val add_type_literals : typ list -> type_literal list
    50   val get_tvar_strs: typ list -> string list
    52   val get_tvar_strs: typ list -> string list
    51   datatype arLit =
    53   datatype arLit =
    52       TConsLit of class * string * string list
    54       TConsLit of class * string * string list
    53     | TVarLit of class * string
    55     | TVarLit of class * string
    54   datatype arity_clause = ArityClause of
    56   datatype arity_clause = ArityClause of
    66   val add_fol_type_funcs: fol_type -> int Symtab.table -> int Symtab.table
    68   val add_fol_type_funcs: fol_type -> int Symtab.table -> int Symtab.table
    67   val add_arity_clause_funcs:
    69   val add_arity_clause_funcs:
    68     arity_clause -> int Symtab.table -> int Symtab.table
    70     arity_clause -> int Symtab.table -> int Symtab.table
    69   val init_functab: int Symtab.table
    71   val init_functab: int Symtab.table
    70   val dfg_sign: bool -> string -> string
    72   val dfg_sign: bool -> string -> string
    71   val dfg_of_typeLit: bool -> type_literal -> string
    73   val dfg_of_type_literal: bool -> type_literal -> string
    72   val gen_dfg_cls: int * string * kind * string list * string list * string list -> string
    74   val gen_dfg_cls: int * string * kind * string list * string list * string list -> string
    73   val string_of_preds: (string * Int.int) list -> string
    75   val string_of_preds: (string * Int.int) list -> string
    74   val string_of_funcs: (string * int) list -> string
    76   val string_of_funcs: (string * int) list -> string
    75   val string_of_symbols: string -> string -> string
    77   val string_of_symbols: string -> string -> string
    76   val string_of_start: string -> string
    78   val string_of_start: string -> string
    77   val string_of_descrip : string -> string
    79   val string_of_descrip : string -> string
    78   val dfg_tfree_clause : string -> string
    80   val dfg_tfree_clause : string -> string
    79   val dfg_classrel_clause: classrel_clause -> string
    81   val dfg_classrel_clause: classrel_clause -> string
    80   val dfg_arity_clause: arity_clause -> string
    82   val dfg_arity_clause: arity_clause -> string
    81   val tptp_sign: bool -> string -> string
    83   val tptp_sign: bool -> string -> string
    82   val tptp_of_typeLit : bool -> type_literal -> string
    84   val tptp_of_type_literal :
       
    85     bool -> type_literal -> name_pool option -> string * name_pool option
    83   val gen_tptp_cls : int * string * kind * string list * string list -> string
    86   val gen_tptp_cls : int * string * kind * string list * string list -> string
    84   val tptp_tfree_clause : string -> string
    87   val tptp_tfree_clause : string -> string
    85   val tptp_arity_clause : arity_clause -> string
    88   val tptp_arity_clause : arity_clause -> string
    86   val tptp_classrel_clause : classrel_clause -> string
    89   val tptp_classrel_clause : classrel_clause -> string
    87 end
    90 end
   171 
   174 
   172 (* convert a list of strings into one single string; surrounded by brackets *)
   175 (* convert a list of strings into one single string; surrounded by brackets *)
   173 fun paren_pack [] = ""   (*empty argument list*)
   176 fun paren_pack [] = ""   (*empty argument list*)
   174   | paren_pack strings = "(" ^ commas strings ^ ")";
   177   | paren_pack strings = "(" ^ commas strings ^ ")";
   175 
   178 
   176 (*TSTP format uses (...) rather than the old [...]*)
   179 fun tptp_clause strings = "(" ^ space_implode " | " strings ^ ")"
   177 fun tptp_pack strings = "(" ^ space_implode " | " strings ^ ")";
       
   178 
       
   179 
   180 
   180 (*Remove the initial ' character from a type variable, if it is present*)
   181 (*Remove the initial ' character from a type variable, if it is present*)
   181 fun trim_type_var s =
   182 fun trim_type_var s =
   182   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   183   if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
   183   else error ("trim_type: Malformed type variable encountered: " ^ s);
   184   else error ("trim_type: Malformed type variable encountered: " ^ s);
   228 type name_pool = string Symtab.table * string Symtab.table
   229 type name_pool = string Symtab.table * string Symtab.table
   229 
   230 
   230 fun empty_name_pool readable_names =
   231 fun empty_name_pool readable_names =
   231   if readable_names then SOME (`I Symtab.empty) else NONE
   232   if readable_names then SOME (`I Symtab.empty) else NONE
   232 
   233 
       
   234 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
   233 fun pool_map f xs =
   235 fun pool_map f xs =
   234   fold_rev (fn x => fn (ys, pool) => f x pool |>> (fn y => y :: ys)) xs
   236   pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
   235   o pair []
       
   236 
   237 
   237 fun add_nice_name full_name nice_prefix j the_pool =
   238 fun add_nice_name full_name nice_prefix j the_pool =
   238   let
   239   let
   239     val nice_name = nice_prefix ^ (if j = 0 then "" else "_" ^ Int.toString j)
   240     val nice_name = nice_prefix ^ (if j = 0 then "" else "_" ^ Int.toString j)
   240   in
   241   in
   304     let
   305     let
   305       val (s, pool) = nice_name sp pool
   306       val (s, pool) = nice_name sp pool
   306       val (ss, pool) = pool_map string_of_fol_type tys pool
   307       val (ss, pool) = pool_map string_of_fol_type tys pool
   307     in (s ^ paren_pack ss, pool) end
   308     in (s ^ paren_pack ss, pool) end
   308 
   309 
   309 (*First string is the type class; the second is a TVar or TFfree*)
   310 (* The first component is the type class; the second is a TVar or TFree. *)
   310 datatype type_literal = LTVar of string * string | LTFree of string * string;
   311 datatype type_literal =
       
   312   TyLitVar of string * name |
       
   313   TyLitFree of string * name
   311 
   314 
   312 exception CLAUSE of string * term;
   315 exception CLAUSE of string * term;
   313 
   316 
   314 (*Make literals for sorted type variables*)
   317 (*Make literals for sorted type variables*)
   315 fun sorts_on_typs_aux (_, [])   = []
   318 fun sorts_on_typs_aux (_, [])   = []
   316   | sorts_on_typs_aux ((x,i),  s::ss) =
   319   | sorts_on_typs_aux ((x,i),  s::ss) =
   317       let val sorts = sorts_on_typs_aux ((x,i), ss)
   320       let val sorts = sorts_on_typs_aux ((x,i), ss)
   318       in
   321       in
   319           if s = "HOL.type" then sorts
   322           if s = "HOL.type" then sorts
   320           else if i = ~1 then LTFree(make_type_class s, make_fixed_type_var x) :: sorts
   323           else if i = ~1 then TyLitFree (make_type_class s, `make_fixed_type_var x) :: sorts
   321           else LTVar(make_type_class s, make_schematic_type_var (x,i)) :: sorts
   324           else TyLitVar (make_type_class s, (make_schematic_type_var (x,i), x)) :: sorts
   322       end;
   325       end;
   323 
   326 
   324 fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
   327 fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
   325   | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
   328   | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
   326 
   329 
   327 fun pred_of_sort (LTVar (s,ty)) = (s,1)
   330 fun pred_of_sort (TyLitVar (s, _)) = (s, 1)
   328   | pred_of_sort (LTFree (s,ty)) = (s,1)
   331   | pred_of_sort (TyLitFree (s, _)) = (s, 1)
   329 
   332 
   330 (*Given a list of sorted type variables, return a list of type literals.*)
   333 (*Given a list of sorted type variables, return a list of type literals.*)
   331 fun add_typs Ts = fold (union (op =)) (map sorts_on_typs Ts) []
   334 fun add_type_literals Ts = fold (union (op =)) (map sorts_on_typs Ts) []
   332 
   335 
   333 (*The correct treatment of TFrees like 'a in lemmas (axiom clauses) is not clear.
   336 (*The correct treatment of TFrees like 'a in lemmas (axiom clauses) is not clear.
   334   * Ignoring them leads to unsound proofs, since we do nothing to ensure that 'a
   337   *  Ignoring them leads to unsound proofs, since we do nothing to ensure that 'a
   335     in a lemma has the same sort as 'a in the conjecture.
   338     in a lemma has the same sort as 'a in the conjecture.
   336   * Deleting such clauses will lead to problems with locales in other use of local results
   339   * Deleting such clauses will lead to problems with locales in other use of local results
   337     where 'a is fixed. Probably we should delete clauses unless the sorts agree.
   340     where 'a is fixed. Probably we should delete clauses unless the sorts agree.
   338   * Currently we include a class constraint in the clause, exactly as with TVars.
   341   * Currently we include a class constraint in the clause, exactly as with TVars.
   339 *)
   342 *)
   497 
   500 
   498 (*Attach sign in DFG syntax: false means negate.*)
   501 (*Attach sign in DFG syntax: false means negate.*)
   499 fun dfg_sign true s = s
   502 fun dfg_sign true s = s
   500   | dfg_sign false s = "not(" ^ s ^ ")"
   503   | dfg_sign false s = "not(" ^ s ^ ")"
   501 
   504 
   502 fun dfg_of_typeLit pos (LTVar (s,ty))  = dfg_sign pos (s ^ "(" ^ ty ^ ")")
   505 fun dfg_of_type_literal pos (TyLitVar (s, (s', _))) =
   503   | dfg_of_typeLit pos (LTFree (s,ty)) = dfg_sign pos (s ^ "(" ^ ty ^ ")");
   506     dfg_sign pos (s ^ "(" ^ s' ^ ")")
       
   507   | dfg_of_type_literal pos (TyLitFree (s, (s', _))) =
       
   508     dfg_sign pos (s ^ "(" ^ s' ^ ")");
   504 
   509 
   505 (*Enclose the clause body by quantifiers, if necessary*)
   510 (*Enclose the clause body by quantifiers, if necessary*)
   506 fun dfg_forall [] body = body
   511 fun dfg_forall [] body = body
   507   | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
   512   | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
   508 
   513 
   561 (**** Produce TPTP files ****)
   566 (**** Produce TPTP files ****)
   562 
   567 
   563 fun tptp_sign true s = s
   568 fun tptp_sign true s = s
   564   | tptp_sign false s = "~ " ^ s
   569   | tptp_sign false s = "~ " ^ s
   565 
   570 
   566 fun tptp_of_typeLit pos (LTVar (s, ty))  = tptp_sign pos (s ^ "(" ^ ty ^ ")")
   571 fun tptp_of_type_literal pos (TyLitVar (s, name)) =
   567   | tptp_of_typeLit pos (LTFree (s, ty)) = tptp_sign pos (s ^ "(" ^ ty ^ ")")
   572     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
       
   573   | tptp_of_type_literal pos (TyLitFree (s, name)) =
       
   574     nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
   568 
   575 
   569 fun tptp_cnf name kind formula =
   576 fun tptp_cnf name kind formula =
   570   "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
   577   "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
   571 
   578 
   572 fun gen_tptp_cls (cls_id, ax_name, Axiom, lits, tylits) =
   579 fun gen_tptp_cls (cls_id, ax_name, Axiom, lits, tylits) =
   573       tptp_cnf (string_of_clausename (cls_id, ax_name)) "axiom"
   580       tptp_cnf (string_of_clausename (cls_id, ax_name)) "axiom"
   574                (tptp_pack (tylits @ lits))
   581                (tptp_clause (tylits @ lits))
   575   | gen_tptp_cls (cls_id, ax_name, Conjecture, lits, _) =
   582   | gen_tptp_cls (cls_id, ax_name, Conjecture, lits, _) =
   576       tptp_cnf (string_of_clausename (cls_id, ax_name)) "negated_conjecture"
   583       tptp_cnf (string_of_clausename (cls_id, ax_name)) "negated_conjecture"
   577                (tptp_pack lits)
   584                (tptp_clause lits)
   578 
   585 
   579 fun tptp_tfree_clause tfree_lit =
   586 fun tptp_tfree_clause tfree_lit =
   580     tptp_cnf "tfree_tcs" "negated_conjecture" (tptp_pack [tfree_lit])
   587     tptp_cnf "tfree_tcs" "negated_conjecture" (tptp_clause [tfree_lit])
   581 
   588 
   582 fun tptp_of_arLit (TConsLit (c,t,args)) =
   589 fun tptp_of_arLit (TConsLit (c,t,args)) =
   583       tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   590       tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
   584   | tptp_of_arLit (TVarLit (c,str)) =
   591   | tptp_of_arLit (TVarLit (c,str)) =
   585       tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
   592       tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
   586 
   593 
   587 fun tptp_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   594 fun tptp_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
   588   tptp_cnf (string_of_ar axiom_name) "axiom"
   595   tptp_cnf (string_of_ar axiom_name) "axiom"
   589            (tptp_pack (map tptp_of_arLit (conclLit :: premLits)))
   596            (tptp_clause (map tptp_of_arLit (conclLit :: premLits)))
   590 
   597 
   591 fun tptp_classrelLits sub sup =
   598 fun tptp_classrelLits sub sup =
   592   let val tvar = "(T)"
   599   let val tvar = "(T)"
   593   in  tptp_pack [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
   600   in  tptp_clause [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
   594 
   601 
   595 fun tptp_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   602 fun tptp_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
   596   tptp_cnf axiom_name "axiom" (tptp_classrelLits subclass superclass)
   603   tptp_cnf axiom_name "axiom" (tptp_classrelLits subclass superclass)
   597 
   604 
   598 end;
   605 end;