src/HOL/TPTP/atp_problem_import.ML
changeset 46324 e4bccf5ec61e
child 46325 b170ab46513a
equal deleted inserted replaced
46323:588c81d08a7c 46324:e4bccf5ec61e
       
     1 (*  Title:      HOL/TPTP/atp_problem_import.ML
       
     2     Author:     Jasmin Blanchette, TU Muenchen
       
     3     Copyright   2012
       
     4 
       
     5 Import TPTP problems as Isabelle terms or goals.
       
     6 *)
       
     7 
       
     8 signature ATP_PROBLEM_IMPORT =
       
     9 sig
       
    10   val isabelle_tptp_file : string -> unit
       
    11   val nitpick_tptp_file : string -> unit
       
    12   val refute_tptp_file : string -> unit
       
    13   val sledgehammer_tptp_file : string -> unit
       
    14   val translate_tptp_file : string -> unit
       
    15 end;
       
    16 
       
    17 structure ATP_Problem_Import : ATP_PROBLEM_IMPORT =
       
    18 struct
       
    19 
       
    20 open ATP_Util
       
    21 open ATP_Problem
       
    22 open ATP_Proof
       
    23 open Nitpick_Util
       
    24 open Nitpick
       
    25 open Nitpick_Isar
       
    26 
       
    27 
       
    28 (** General TPTP parsing **)
       
    29 
       
    30 exception SYNTAX of string
       
    31 
       
    32 val tptp_explode = raw_explode o strip_spaces_except_between_idents
       
    33 
       
    34 fun parse_file_path (c :: ss) =
       
    35     if c = "'" orelse c = "\"" then
       
    36       ss |> chop_while (curry (op <>) c) |>> implode ||> tl
       
    37     else
       
    38       raise SYNTAX "invalid file path"
       
    39   | parse_file_path [] = raise SYNTAX "invalid file path"
       
    40 
       
    41 fun parse_include x =
       
    42   let
       
    43     val (file_name, rest) =
       
    44       (Scan.this_string "include" |-- $$ "(" |-- parse_file_path --| $$ ")"
       
    45        --| $$ ".") x
       
    46     val path = file_name |> Path.explode
       
    47     val path =
       
    48       path |> not (Path.is_absolute path) ? Path.append (Path.explode "$TPTP")
       
    49   in ((), (path |> File.read |> tptp_explode) @ rest) end
       
    50 
       
    51 val parse_cnf_or_fof =
       
    52   (Scan.this_string "cnf" || Scan.this_string "fof") |-- $$ "(" |--
       
    53   Scan.many (not_equal ",") |-- $$ "," |--
       
    54   (Scan.this_string "axiom" || Scan.this_string "definition"
       
    55    || Scan.this_string "theorem" || Scan.this_string "lemma"
       
    56    || Scan.this_string "hypothesis" || Scan.this_string "conjecture"
       
    57    || Scan.this_string "negated_conjecture") --| $$ "," -- parse_formula
       
    58       --| $$ ")" --| $$ "."
       
    59   >> (fn ("conjecture", phi) => AConn (ANot, [phi]) | (_, phi) => phi)
       
    60 
       
    61 val parse_problem =
       
    62   Scan.repeat parse_include
       
    63   |-- Scan.repeat (parse_cnf_or_fof --| Scan.repeat parse_include)
       
    64 
       
    65 val parse_tptp_problem =
       
    66   Scan.finite Symbol.stopper
       
    67       (Scan.error (!! (fn _ => raise SYNTAX "malformed TPTP input")
       
    68                   parse_problem))
       
    69   o tptp_explode
       
    70 
       
    71 val iota_T = @{typ iota}
       
    72 val quant_T = (iota_T --> bool_T) --> bool_T
       
    73 
       
    74 fun is_variable s = Char.isUpper (String.sub (s, 0))
       
    75 
       
    76 fun hol_term_from_fo_term res_T (ATerm (x, us)) =
       
    77   let val ts = map (hol_term_from_fo_term iota_T) us in
       
    78     list_comb ((case x of
       
    79                   "$true" => @{const_name True}
       
    80                 | "$false" => @{const_name False}
       
    81                 | "=" => @{const_name HOL.eq}
       
    82                 | "equal" => @{const_name HOL.eq}
       
    83                 | _ => x, map fastype_of ts ---> res_T)
       
    84                |> (if is_variable x then Free else Const), ts)
       
    85   end
       
    86 
       
    87 fun hol_prop_from_formula phi =
       
    88   case phi of
       
    89     AQuant (_, [], phi') => hol_prop_from_formula phi'
       
    90   | AQuant (q, (x, _) :: xs, phi') =>
       
    91     Const (case q of AForall => @{const_name All} | AExists => @{const_name Ex},
       
    92            quant_T)
       
    93     $ lambda (Free (x, iota_T)) (hol_prop_from_formula (AQuant (q, xs, phi')))
       
    94   | AConn (ANot, [u']) => HOLogic.mk_not (hol_prop_from_formula u')
       
    95   | AConn (c, [u1, u2]) =>
       
    96     pairself hol_prop_from_formula (u1, u2)
       
    97     |> (case c of
       
    98           AAnd => HOLogic.mk_conj
       
    99         | AOr => HOLogic.mk_disj
       
   100         | AImplies => HOLogic.mk_imp
       
   101         | AIff => HOLogic.mk_eq
       
   102         | ANot => raise Fail "binary \"ANot\"")
       
   103   | AConn _ => raise Fail "malformed AConn"
       
   104   | AAtom u => hol_term_from_fo_term bool_T u
       
   105 
       
   106 fun mk_all x t = Const (@{const_name All}, quant_T) $ lambda x t
       
   107 
       
   108 fun close_hol_prop t = fold (mk_all o Free) (Term.add_frees t []) t
       
   109 
       
   110 
       
   111 (** Isabelle (combination of provers) **)
       
   112 
       
   113 fun isabelle_tptp_file file_name = ()
       
   114 
       
   115 
       
   116 (** Nitpick (alias Nitrox) **)
       
   117 
       
   118 fun nitpick_tptp_file file_name =
       
   119   case parse_tptp_problem (File.read (Path.explode file_name)) of
       
   120     (_, s :: ss) => raise SYNTAX ("cannot parse " ^ quote (implode (s :: ss)))
       
   121   | (phis, []) =>
       
   122     let
       
   123       val ts = map (HOLogic.mk_Trueprop o close_hol_prop
       
   124                     o hol_prop_from_formula) phis
       
   125 (*
       
   126       val _ = warning (PolyML.makestring phis)
       
   127       val _ = app (warning o Syntax.string_of_term @{context}) ts
       
   128 *)
       
   129       val state = Proof.init @{context}
       
   130       val params =
       
   131         [("card iota", "1\<emdash>100"),
       
   132          ("card", "1\<emdash>8"),
       
   133          ("box", "false"),
       
   134          ("sat_solver", "smart"),
       
   135          ("max_threads", "1"),
       
   136          ("batch_size", "10"),
       
   137          (* ("debug", "true"), *)
       
   138          ("verbose", "true"),
       
   139          (* ("overlord", "true"), *)
       
   140          ("show_consts", "true"),
       
   141          ("format", "1000"),
       
   142          ("max_potential", "0"),
       
   143          ("timeout", "none"),
       
   144          ("expect", genuineN)]
       
   145         |> default_params @{theory}
       
   146       val i = 1
       
   147       val n = 1
       
   148       val step = 0
       
   149       val subst = []
       
   150     in
       
   151       pick_nits_in_term state params Normal i n step subst ts @{prop False};
       
   152       ()
       
   153     end
       
   154 
       
   155 
       
   156 (** Refute **)
       
   157 
       
   158 fun refute_tptp_file file_name = ()
       
   159 
       
   160 
       
   161 (** Sledgehammer **)
       
   162 
       
   163 fun sledgehammer_tptp_file file_name = ()
       
   164 
       
   165 
       
   166 (** Translator between TPTP(-like) file formats **)
       
   167 
       
   168 fun translate_tptp_file file_name = ()
       
   169 
       
   170 end;