src/HOL/Tools/Nitpick/nitrox.ML
author blanchet
Tue, 31 May 2011 16:38:36 +0200
changeset 43926 0a2f5b86bdd7
parent 43863 7d3ce43d9464
child 44673 151288f723dc
permissions -rw-r--r--
first step in sharing more code between ATP and Metis translation
blanchet@42919
     1
(*  Title:      HOL/Tools/Nitpick/nitrox.ML
blanchet@42919
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@42919
     3
    Copyright   2010, 2011
blanchet@42919
     4
blanchet@43800
     5
Finite model generation for TPTP first-order formulas (FOF and CNF) via Nitpick.
blanchet@42919
     6
*)
blanchet@42919
     7
blanchet@42919
     8
signature NITROX =
blanchet@42919
     9
sig
blanchet@42919
    10
  val pick_nits_in_fof_file : string -> string
blanchet@42919
    11
end;
blanchet@42919
    12
blanchet@42919
    13
structure Nitrox : NITROX =
blanchet@42919
    14
struct
blanchet@42919
    15
blanchet@43926
    16
open ATP_Util
blanchet@43801
    17
open ATP_Problem
blanchet@43802
    18
open ATP_Proof
blanchet@43863
    19
open Nitpick
blanchet@43863
    20
open Nitpick_Isar
blanchet@42919
    21
blanchet@42919
    22
exception SYNTAX of string
blanchet@42919
    23
blanchet@42919
    24
val parse_keyword = Scan.this_string
blanchet@42919
    25
blanchet@43802
    26
fun parse_file_path (c :: ss) =
blanchet@43802
    27
    if c = "'" orelse c = "\"" then
blanchet@43802
    28
      ss |> chop_while (curry (op <>) c) |>> implode ||> tl
blanchet@43802
    29
    else
blanchet@43802
    30
      raise SYNTAX "invalid file path"
blanchet@43802
    31
  | parse_file_path [] = raise SYNTAX "invalid file path"
blanchet@42919
    32
blanchet@42919
    33
fun parse_include x =
blanchet@42919
    34
  let
blanchet@42919
    35
    val (file_name, rest) =
blanchet@42919
    36
      (parse_keyword "include" |-- $$ "(" |-- parse_file_path --| $$ ")"
blanchet@42919
    37
       --| $$ ".") x
blanchet@42919
    38
  in
blanchet@43802
    39
    ((), (file_name |> Path.explode |> File.read
blanchet@43802
    40
                    |> strip_spaces true (K true)
blanchet@43802
    41
                    |> raw_explode) @ rest)
blanchet@42919
    42
  end
blanchet@42919
    43
blanchet@42919
    44
val parse_fof_or_cnf =
blanchet@42919
    45
  (parse_keyword "fof" || parse_keyword "cnf") |-- $$ "(" |--
blanchet@42919
    46
  Scan.many (not_equal ",") |-- $$ "," |--
blanchet@42919
    47
  (parse_keyword "axiom" || parse_keyword "definition"
blanchet@42919
    48
   || parse_keyword "theorem" || parse_keyword "lemma"
blanchet@42919
    49
   || parse_keyword "hypothesis" || parse_keyword "conjecture"
blanchet@42919
    50
   || parse_keyword "negated_conjecture") --| $$ "," -- parse_formula
blanchet@42919
    51
      --| $$ ")" --| $$ "."
blanchet@42919
    52
  >> (fn ("conjecture", phi) => AConn (ANot, [phi]) | (_, phi) => phi)
blanchet@42919
    53
blanchet@42919
    54
val parse_problem =
blanchet@42919
    55
  Scan.repeat parse_include
blanchet@42919
    56
  |-- Scan.repeat (parse_fof_or_cnf --| Scan.repeat parse_include)
blanchet@42919
    57
blanchet@42919
    58
val parse_tptp_problem =
blanchet@42919
    59
  Scan.finite Symbol.stopper
blanchet@42919
    60
      (Scan.error (!! (fn _ => raise SYNTAX "malformed TPTP input")
blanchet@42919
    61
                  parse_problem))
blanchet@43802
    62
  o raw_explode o strip_spaces true (K true)
blanchet@42919
    63
blanchet@42919
    64
val boolT = @{typ bool}
blanchet@42919
    65
val iotaT = @{typ iota}
blanchet@42919
    66
val quantT = (iotaT --> boolT) --> boolT
blanchet@42919
    67
blanchet@42919
    68
fun is_variable s = Char.isUpper (String.sub (s, 0))
blanchet@42919
    69
blanchet@42919
    70
fun hol_term_from_fo_term res_T (ATerm (x, us)) =
blanchet@42919
    71
  let val ts = map (hol_term_from_fo_term iotaT) us in
blanchet@42919
    72
    list_comb ((case x of
blanchet@42919
    73
                  "$true" => @{const_name True}
blanchet@42919
    74
                | "$false" => @{const_name False}
blanchet@42919
    75
                | "=" => @{const_name HOL.eq}
blanchet@42919
    76
                | _ => x, map fastype_of ts ---> res_T)
blanchet@42919
    77
               |> (if is_variable x then Free else Const), ts)
blanchet@42919
    78
  end
blanchet@42919
    79
blanchet@42919
    80
fun hol_prop_from_formula phi =
blanchet@42919
    81
  case phi of
blanchet@42919
    82
    AQuant (_, [], phi') => hol_prop_from_formula phi'
blanchet@43801
    83
  | AQuant (q, (x, _) :: xs, phi') =>
blanchet@42919
    84
    Const (case q of AForall => @{const_name All} | AExists => @{const_name Ex},
blanchet@42919
    85
           quantT)
blanchet@42919
    86
    $ lambda (Free (x, iotaT)) (hol_prop_from_formula (AQuant (q, xs, phi')))
blanchet@42919
    87
  | AConn (ANot, [u']) => HOLogic.mk_not (hol_prop_from_formula u')
blanchet@42919
    88
  | AConn (c, [u1, u2]) =>
blanchet@42919
    89
    pairself hol_prop_from_formula (u1, u2)
blanchet@42919
    90
    |> (case c of
blanchet@42919
    91
          AAnd => HOLogic.mk_conj
blanchet@42919
    92
        | AOr => HOLogic.mk_disj
blanchet@42919
    93
        | AImplies => HOLogic.mk_imp
blanchet@42919
    94
        | AIf => HOLogic.mk_imp o swap
blanchet@42919
    95
        | AIff => HOLogic.mk_eq
blanchet@42919
    96
        | ANotIff => HOLogic.mk_not o HOLogic.mk_eq
blanchet@42919
    97
        | ANot => raise Fail "binary \"ANot\"")
blanchet@42919
    98
  | AConn _ => raise Fail "malformed AConn"
blanchet@43801
    99
  | AAtom u => hol_term_from_fo_term boolT u
blanchet@42919
   100
blanchet@42919
   101
fun mk_all x t = Const (@{const_name All}, quantT) $ lambda x t
blanchet@42919
   102
blanchet@42919
   103
fun close_hol_prop t = fold (mk_all o Free) (Term.add_frees t []) t
blanchet@42919
   104
blanchet@42919
   105
fun pick_nits_in_fof_file file_name =
blanchet@42919
   106
  case parse_tptp_problem (File.read (Path.explode file_name)) of
blanchet@42919
   107
    (_, s :: ss) => raise SYNTAX ("cannot parse " ^ quote (implode (s :: ss)))
blanchet@42919
   108
  | (phis, []) =>
blanchet@42919
   109
    let
blanchet@42919
   110
      val ts = map (HOLogic.mk_Trueprop o close_hol_prop
blanchet@42919
   111
                    o hol_prop_from_formula) phis
blanchet@42919
   112
(*
blanchet@42919
   113
      val _ = warning (PolyML.makestring phis)
blanchet@42919
   114
      val _ = app (warning o Syntax.string_of_term @{context}) ts
blanchet@42919
   115
*)
blanchet@42919
   116
      val state = Proof.init @{context}
blanchet@42919
   117
      val params =
blanchet@43800
   118
        [("card", "1\<emdash>8"),
blanchet@42919
   119
         ("box", "false"),
blanchet@42919
   120
         ("sat_solver", "smart"),
blanchet@42919
   121
         ("max_threads", "1"),
blanchet@42919
   122
         ("batch_size", "10"),
blanchet@42919
   123
         (* ("debug", "true"), *)
blanchet@42919
   124
         ("verbose", "true"),
blanchet@42919
   125
         (* ("overlord", "true"), *)
blanchet@42919
   126
         ("show_consts", "true"),
blanchet@42919
   127
         ("format", "1000"),
blanchet@42919
   128
         ("max_potential", "0"),
blanchet@42919
   129
         (* ("timeout", "240 s"), *)
blanchet@43863
   130
         ("expect", genuineN)]
blanchet@43863
   131
        |> default_params @{theory}
blanchet@42919
   132
      val i = 1
blanchet@42919
   133
      val n = 1
blanchet@42919
   134
      val step = 0
blanchet@42919
   135
      val subst = []
blanchet@42919
   136
    in
blanchet@43863
   137
      pick_nits_in_term state params Normal i n step subst ts @{prop False}
blanchet@43863
   138
      |> fst
blanchet@42919
   139
    end
blanchet@42919
   140
blanchet@42919
   141
end;