src/HOL/TPTP/TPTP_Parser/tptp_interpret.ML
author blanchet
Thu, 16 Aug 2012 15:41:36 +0200
changeset 48829 6ed588c4f963
parent 47729 44d1f4e0a46e
child 48881 46e053eda5dd
permissions -rw-r--r--
look in current directory first before looking up includes in the TPTP directory, as required by Geoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46879
a8b1236e0837 tuned headers;
wenzelm
parents: 46844
diff changeset
     1
(*  Title:      HOL/TPTP/TPTP_Parser/tptp_interpret.ML
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
     2
    Author:     Nik Sultana, Cambridge University Computer Laboratory
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
     3
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
     4
Interprets TPTP problems in Isabelle/HOL.
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
     5
*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
     6
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
     7
signature TPTP_INTERPRET =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
     8
sig
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
     9
  (*Signature extension: typing information for variables and constants*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    10
  type var_types = (string * typ option) list
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    11
  type const_map = (string * term) list
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    12
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    13
  (*Mapping from TPTP types to Isabelle/HOL types. This map works over all
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    14
  base types. The map must be total wrt TPTP types.*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    15
  type type_map = (TPTP_Syntax.tptp_type * typ) list
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    16
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    17
  (*Inference info, when available, consists of the name of the inference rule
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    18
  and the names of the inferences involved in the reasoning step.*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    19
  type inference_info = (string * string list) option
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    20
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    21
  (*A parsed annotated formula is represented as a 5-tuple consisting of
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    22
  the formula's label, its role, the TPTP formula, its Isabelle/HOL meaning, and
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    23
  inference info*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    24
  type tptp_formula_meaning =
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
    25
    string * TPTP_Syntax.role * term * inference_info
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    26
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    27
  (*In general, the meaning of a TPTP statement (which, through the Include
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    28
  directive, may include the meaning of an entire TPTP file, is a map from
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    29
  TPTP to Isabelle/HOL types, a map from TPTP constants to their Isabelle/HOL
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    30
  counterparts and their types, and a list of interpreted annotated formulas.*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    31
  type tptp_general_meaning =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    32
    (type_map * const_map * tptp_formula_meaning list)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    33
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    34
  (*cautious = indicates whether additional checks are made to check
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    35
      that all used types have been declared.
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    36
    problem_name = if given then it is used to qualify types & constants*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    37
  type config =
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    38
    {cautious : bool,
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
    39
     problem_name : TPTP_Problem_Name.problem_name option}
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    40
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    41
  (*Generates a fresh Isabelle/HOL type for interpreting a TPTP type in a theory.*)
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    42
  val declare_type : config -> (TPTP_Syntax.tptp_type * string) -> type_map ->
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    43
    theory -> type_map * theory
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    44
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    45
  (*Map TPTP types to Isabelle/HOL types*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    46
  val interpret_type : config -> theory -> type_map ->
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    47
    TPTP_Syntax.tptp_type -> typ
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    48
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    49
  (*Map TPTP terms to Isabelle/HOL terms*)
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
    50
  val interpret_term : bool -> config -> TPTP_Syntax.language ->
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
    51
    const_map -> var_types -> type_map -> TPTP_Syntax.tptp_term -> theory ->
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
    52
    term * theory
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
    53
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
    54
  val interpret_formula : config -> TPTP_Syntax.language -> const_map ->
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
    55
    var_types -> type_map -> TPTP_Syntax.tptp_formula -> theory ->
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
    56
    term * theory
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
    57
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    58
  (*Interpret a TPTP line: return a "tptp_general_meaning" for that line,
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    59
  as well as an updated Isabelle theory including any types & constants
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    60
  which were specified in that line.
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    61
  Note that type/constant declarations do not result in any formulas being
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    62
  returned. A typical TPTP line might update the theory, and/or return one or
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    63
  more formulas. For instance, the "include" directive may update the theory
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    64
  and also return a list of formulas from the included file.
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    65
  Arguments:
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    66
    config = (see above)
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    67
    inclusion list = names of annotated formulas to interpret (since "include"
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    68
      directive can be selective wrt to such names)
47360
sultana
parents: 47359
diff changeset
    69
    type_map = mapping of TPTP-types to Isabelle/HOL types. This argument may be
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    70
      given to force a specific mapping: this is usually done for using an
47360
sultana
parents: 47359
diff changeset
    71
      imported TPTP problem in Isar.
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    72
    const_map = as previous, but for constants.
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
    73
    path_prefixes = paths where TPTP problems etc are located (to help the
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
    74
      "include" directive find the files.
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    75
    line = parsed TPTP line
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    76
    thy = theory where interpreted info will be stored.
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    77
  *)
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
    78
  val interpret_line : config -> string list -> type_map -> const_map ->
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
    79
    Path.T list -> TPTP_Syntax.tptp_line -> theory ->
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
    80
    tptp_general_meaning * theory
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    81
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    82
  (*Like "interpret_line" above, but works over a whole parsed problem.
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    83
    Arguments:
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    84
      config = as previously
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    85
      inclusion list = as previously
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
    86
      path_prefixes = as previously
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    87
      lines = parsed TPTP syntax
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    88
      type_map = as previously
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    89
      const_map = as previously
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    90
      thy = as previously
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    91
  *)
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
    92
  val interpret_problem : config -> string list -> Path.T list ->
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    93
    TPTP_Syntax.tptp_line list -> type_map -> const_map -> theory ->
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    94
    tptp_general_meaning * theory
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    95
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
    96
  (*Like "interpret_problem" above, but it is given a filename rather than
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    97
  a parsed problem.*)
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
    98
  val interpret_file : bool -> Path.T list -> Path.T -> type_map -> const_map ->
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
    99
    theory -> tptp_general_meaning * theory
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   100
47569
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   101
  type position = string * int * int
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   102
  exception MISINTERPRET of position list * exn
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   103
  exception MISINTERPRET_FORMULA of string * TPTP_Syntax.tptp_formula
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   104
  exception MISINTERPRET_SYMBOL of string * TPTP_Syntax.symbol
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   105
  exception MISINTERPRET_TERM of string * TPTP_Syntax.tptp_term
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   106
  exception MISINTERPRET_TYPE of string * TPTP_Syntax.tptp_type
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   107
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   108
  val import_file : bool -> Path.T list -> Path.T -> type_map -> const_map ->
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   109
    theory -> theory
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   110
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   111
  (*Imported TPTP files are stored as "manifests" in the theory.*)
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   112
  type manifest = TPTP_Problem_Name.problem_name * tptp_general_meaning
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   113
  val get_manifests : theory -> manifest list
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   114
end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   115
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   116
structure TPTP_Interpret : TPTP_INTERPRET =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   117
struct
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   118
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   119
open TPTP_Syntax
47569
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   120
type position = string * int * int
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   121
exception MISINTERPRET of position list * exn
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   122
exception MISINTERPRET_FORMULA of string * TPTP_Syntax.tptp_formula
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   123
exception MISINTERPRET_SYMBOL of string * TPTP_Syntax.symbol
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   124
exception MISINTERPRET_TERM of string * TPTP_Syntax.tptp_term
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   125
exception MISINTERPRET_TYPE of string * TPTP_Syntax.tptp_type
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   126
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   127
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   128
(* General stuff *)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   129
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   130
type config =
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   131
  {cautious : bool,
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   132
   problem_name : TPTP_Problem_Name.problem_name option}
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   133
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   134
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   135
(* Interpretation *)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   136
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   137
(** Signatures and other type abbrevations **)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   138
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   139
type const_map = (string * term) list
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   140
type var_types = (string * typ option) list
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   141
type type_map = (TPTP_Syntax.tptp_type * typ) list
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   142
type inference_info = (string * string list) option
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   143
type tptp_formula_meaning =
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   144
  string * TPTP_Syntax.role * term * inference_info
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   145
type tptp_general_meaning =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   146
  (type_map * const_map * tptp_formula_meaning list)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   147
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   148
type manifest = TPTP_Problem_Name.problem_name * tptp_general_meaning
47639
2f7eb28c8b09 more standard Theory_Data setup;
wenzelm
parents: 47570
diff changeset
   149
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   150
structure TPTP_Data = Theory_Data
47639
2f7eb28c8b09 more standard Theory_Data setup;
wenzelm
parents: 47570
diff changeset
   151
(
2f7eb28c8b09 more standard Theory_Data setup;
wenzelm
parents: 47570
diff changeset
   152
  type T = manifest list
2f7eb28c8b09 more standard Theory_Data setup;
wenzelm
parents: 47570
diff changeset
   153
  val empty = []
2f7eb28c8b09 more standard Theory_Data setup;
wenzelm
parents: 47570
diff changeset
   154
  val extend = I
2f7eb28c8b09 more standard Theory_Data setup;
wenzelm
parents: 47570
diff changeset
   155
  fun merge data : T = Library.merge (op =) data
2f7eb28c8b09 more standard Theory_Data setup;
wenzelm
parents: 47570
diff changeset
   156
)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   157
val get_manifests = TPTP_Data.get
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   158
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   159
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   160
(** Adding types to a signature **)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   161
47514
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   162
(*transform quoted names into acceptable ASCII strings*)
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   163
fun alphanumerate c =
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   164
  let
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   165
    val c' = ord c
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   166
    val out_of_range =
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   167
      ((c' > 64) andalso (c' < 91)) orelse ((c' > 96)
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   168
       andalso (c' < 123)) orelse ((c' > 47) andalso (c' < 58))
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   169
  in
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   170
    if (not out_of_range) andalso (not (c = "_")) then
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   171
      "_nom_" ^ Int.toString (ord c) ^ "_"
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   172
    else c
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   173
  end
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   174
fun alphanumerated_name prefix name =
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   175
  prefix ^ (raw_explode #> map alphanumerate #> implode) name
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   176
47332
360e080fd13e dealing with SMLNJ errors 'value type in structure doesn't match signature spec' (which originated from lack of type generalisation) and 'unresolved flex record' wrt tptp_interpret
sultana
parents: 46961
diff changeset
   177
(*SMLNJ complains if type annotation not used below*)
360e080fd13e dealing with SMLNJ errors 'value type in structure doesn't match signature spec' (which originated from lack of type generalisation) and 'unresolved flex record' wrt tptp_interpret
sultana
parents: 46961
diff changeset
   178
fun mk_binding (config : config) ident =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   179
  let
47514
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   180
    val pre_binding = Binding.name (alphanumerated_name "bnd_" ident)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   181
  in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   182
    case #problem_name config of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   183
      NONE => pre_binding
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   184
    | SOME prob =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   185
        Binding.qualify
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   186
         false
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   187
         (TPTP_Problem_Name.mangle_problem_name prob)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   188
         pre_binding
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   189
  end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   190
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   191
fun type_exists config thy ty_name =
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   192
  Sign.declared_tyname thy (Sign.full_name thy (mk_binding config ty_name))
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   193
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   194
(*Returns updated theory and the name of the final type's name -- i.e. if the
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   195
original name is already taken then the function looks for an available
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   196
alternative. It also returns an updated type_map if one has been passed to it.*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   197
fun declare_type (config : config) (thf_type, type_name) ty_map thy =
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   198
  if type_exists config thy type_name then
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   199
    raise MISINTERPRET_TYPE ("Type already exists", Atom_type type_name)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   200
  else
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   201
    let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   202
      val binding = mk_binding config type_name
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   203
      val final_fqn = Sign.full_name thy binding
47510
sultana
parents: 47411
diff changeset
   204
      val thy' =
sultana
parents: 47411
diff changeset
   205
        Typedecl.typedecl_global (mk_binding config type_name, [], NoSyn) thy
sultana
parents: 47411
diff changeset
   206
        |> snd
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   207
      val typ_map_entry = (thf_type, (Type (final_fqn, [])))
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   208
      val ty_map' = typ_map_entry :: ty_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   209
    in (ty_map', thy') end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   210
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   211
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   212
(** Adding constants to signature **)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   213
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   214
fun full_name thy config const_name =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   215
  Sign.full_name thy (mk_binding config const_name)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   216
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   217
fun const_exists config thy const_name =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   218
  Sign.declared_const thy (full_name thy config const_name)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   219
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   220
fun declare_constant config const_name ty thy =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   221
  if const_exists config thy const_name then
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   222
    raise MISINTERPRET_TERM
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   223
     ("Const already declared", Term_Func (Uninterpreted const_name, []))
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   224
  else
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   225
    Theory.specify_const
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   226
     ((mk_binding config const_name, ty), NoSyn) thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   227
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   228
fun declare_const_ifnot config const_name ty thy =
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   229
  if const_exists config thy const_name then
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   230
    (Sign.mk_const thy ((Sign.full_name thy (mk_binding config const_name)), []), thy)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   231
  else declare_constant config const_name ty thy
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   232
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   233
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   234
(** Interpretation functions **)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   235
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   236
(*Types in THF are encoded as formulas. This function translates them to type form.*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   237
(*FIXME possibly incomplete hack*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   238
fun fmlatype_to_type (Atom (THF_Atom_term (Term_Func (TypeSymbol typ, [])))) =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   239
      Defined_type typ
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   240
  | fmlatype_to_type (Atom (THF_Atom_term (Term_Func (Uninterpreted str, [])))) =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   241
      Atom_type str
47360
sultana
parents: 47359
diff changeset
   242
  | fmlatype_to_type (Type_fmla (Fn_type (ty1, ty2))) =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   243
      let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   244
        val ty1' = case ty1 of
47360
sultana
parents: 47359
diff changeset
   245
            Fn_type _ => fmlatype_to_type (Type_fmla ty1)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   246
          | Fmla_type ty1 => fmlatype_to_type ty1
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   247
        val ty2' = case ty2 of
47360
sultana
parents: 47359
diff changeset
   248
            Fn_type _ => fmlatype_to_type (Type_fmla ty2)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   249
          | Fmla_type ty2 => fmlatype_to_type ty2
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   250
      in Fn_type (ty1', ty2') end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   251
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   252
fun interpret_type config thy type_map thf_ty =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   253
  let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   254
    fun lookup_type ty_map thf_ty =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   255
      (case AList.lookup (op =) ty_map thf_ty of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   256
          NONE =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   257
            raise MISINTERPRET_TYPE
47510
sultana
parents: 47411
diff changeset
   258
              ("Could not find the interpretation of this TPTP type in the \
sultana
parents: 47411
diff changeset
   259
               \mapping to Isabelle/HOL", thf_ty)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   260
        | SOME ty => ty)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   261
  in
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   262
    case thf_ty of
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   263
       Prod_type (thf_ty1, thf_ty2) =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   264
         Type ("Product_Type.prod",
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   265
          [interpret_type config thy type_map thf_ty1,
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   266
           interpret_type config thy type_map thf_ty2])
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   267
     | Fn_type (thf_ty1, thf_ty2) =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   268
         Type ("fun",
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   269
          [interpret_type config thy type_map thf_ty1,
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   270
           interpret_type config thy type_map thf_ty2])
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   271
     | Atom_type _ =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   272
         lookup_type type_map thf_ty
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   273
     | Defined_type tptp_base_type =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   274
         (case tptp_base_type of
47519
9c3acd90063a simplified interpretation of '$i';
sultana
parents: 47515
diff changeset
   275
            Type_Ind => @{typ ind}
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   276
          | Type_Bool => HOLogic.boolT
47510
sultana
parents: 47411
diff changeset
   277
          | Type_Type => raise MISINTERPRET_TYPE ("No type interpretation", thf_ty)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   278
         (*FIXME these types are currently unsupported, so they're treated as
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   279
         individuals*)
47690
4c681ad99818 disabled interpreting arithmetic;
sultana
parents: 47688
diff changeset
   280
          | Type_Int =>
4c681ad99818 disabled interpreting arithmetic;
sultana
parents: 47688
diff changeset
   281
              interpret_type config thy type_map (Defined_type Type_Ind)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   282
          | Type_Rat =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   283
              interpret_type config thy type_map (Defined_type Type_Ind)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   284
          | Type_Real =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   285
              interpret_type config thy type_map (Defined_type Type_Ind)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   286
          )
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   287
     | Sum_type _ =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   288
         raise MISINTERPRET_TYPE (*FIXME*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   289
          ("No type interpretation (sum type)", thf_ty)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   290
     | Fmla_type tptp_ty =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   291
        fmlatype_to_type tptp_ty
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   292
        |> interpret_type config thy type_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   293
     | Subtype _ =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   294
         raise MISINTERPRET_TYPE (*FIXME*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   295
          ("No type interpretation (subtype)", thf_ty)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   296
  end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   297
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   298
fun the_const config thy language const_map_payload str =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   299
  if language = THF then
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   300
    (case AList.lookup (op =) const_map_payload str of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   301
        NONE => raise MISINTERPRET_TERM
47510
sultana
parents: 47411
diff changeset
   302
          ("Could not find the interpretation of this constant in the \
sultana
parents: 47411
diff changeset
   303
            \mapping to Isabelle/HOL", Term_Func (Uninterpreted str, []))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   304
      | SOME t => t)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   305
  else
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   306
    if const_exists config thy str then
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   307
       Sign.mk_const thy ((Sign.full_name thy (mk_binding config str)), [])
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   308
    else raise MISINTERPRET_TERM
47510
sultana
parents: 47411
diff changeset
   309
          ("Could not find the interpretation of this constant in the \
sultana
parents: 47411
diff changeset
   310
            \mapping to Isabelle/HOL", Term_Func (Uninterpreted str, []))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   311
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   312
(*Eta-expands n-ary function.
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   313
 "str" is the name of an Isabelle/HOL constant*)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   314
fun mk_n_fun n str =
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   315
  let
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   316
    fun body 0 t = t
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   317
      | body n t = body (n - 1) (t  $ (Bound (n - 1)))
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   318
  in
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   319
    body n (Const (str, Term.dummyT))
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   320
    |> funpow n (Term.absdummy Term.dummyT)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   321
  end
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   322
fun mk_fun_type [] b acc = acc b
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   323
  | mk_fun_type (ty :: tys) b acc =
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   324
      mk_fun_type tys b (fn ty_rest => Type ("fun", [ty, acc ty_rest]))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   325
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   326
(*These arities are not part of the TPTP spec*)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   327
fun arity_of interpreted_symbol = case interpreted_symbol of
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   328
    UMinus => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   329
  | Sum => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   330
  | Difference => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   331
  | Product => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   332
  | Quotient => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   333
  | Quotient_E => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   334
  | Quotient_T => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   335
  | Quotient_F => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   336
  | Remainder_E => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   337
  | Remainder_T => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   338
  | Remainder_F => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   339
  | Floor => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   340
  | Ceiling => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   341
  | Truncate => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   342
  | Round => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   343
  | To_Int => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   344
  | To_Rat => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   345
  | To_Real => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   346
  | Less => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   347
  | LessEq => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   348
  | Greater => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   349
  | GreaterEq => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   350
  | EvalEq => 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   351
  | Is_Int => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   352
  | Is_Rat => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   353
  | Distinct => 1
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   354
  | Apply=> 2
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   355
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   356
fun interpret_symbol config language const_map symb thy =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   357
  case symb of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   358
     Uninterpreted n =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   359
       (*Constant would have been added to the map at the time of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   360
       declaration*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   361
       the_const config thy language const_map n
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   362
   | Interpreted_ExtraLogic interpreted_symbol =>
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   363
       (*FIXME not interpreting*)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   364
       Sign.mk_const thy ((Sign.full_name thy (mk_binding config
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   365
          (string_of_interpreted_symbol interpreted_symbol))), [])
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   366
   | Interpreted_Logic logic_symbol =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   367
       (case logic_symbol of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   368
          Equals => HOLogic.eq_const Term.dummyT
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   369
        | NEquals =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   370
           HOLogic.mk_not (HOLogic.eq_const Term.dummyT $ Bound 1 $ Bound 0)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   371
           |> (Term.absdummy Term.dummyT #> Term.absdummy Term.dummyT)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   372
        | Or => HOLogic.disj
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   373
        | And => HOLogic.conj
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   374
        | Iff => HOLogic.eq_const HOLogic.boolT
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   375
        | If => HOLogic.imp
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   376
        | Fi => @{term "\<lambda> x. \<lambda> y. y \<longrightarrow> x"}
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   377
        | Xor =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   378
            @{term
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   379
              "\<lambda> x. \<lambda> y. \<not> (x \<longleftrightarrow> y)"}
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   380
        | Nor => @{term "\<lambda> x. \<lambda> y. \<not> (x \<or> y)"}
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   381
        | Nand => @{term "\<lambda> x. \<lambda> y. \<not> (x \<and> y)"}
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   382
        | Not => HOLogic.Not
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   383
        | Op_Forall => HOLogic.all_const Term.dummyT
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   384
        | Op_Exists => HOLogic.exists_const Term.dummyT
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   385
        | True => @{term "True"}
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   386
        | False => @{term "False"}
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   387
        )
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   388
   | TypeSymbol _ =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   389
       raise MISINTERPRET_SYMBOL
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   390
        ("Cannot have TypeSymbol in term", symb)
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   391
   | System _ =>
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   392
       raise MISINTERPRET_SYMBOL
47510
sultana
parents: 47411
diff changeset
   393
        ("Cannot interpret system symbol", symb)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   394
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   395
(*Apply a function to a list of arguments*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   396
val mapply = fold (fn x => fn y => y $ x)
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   397
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   398
fun mapply' (args, thy) f =
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   399
  let
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   400
    val (f', thy') = f thy
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   401
  in (mapply args f', thy') end
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   402
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   403
(*As above, but for products*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   404
fun mtimes thy =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   405
  fold (fn x => fn y =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   406
    Sign.mk_const thy
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   407
    ("Product_Type.Pair", [Term.dummyT, Term.dummyT]) $ y $ x)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   408
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   409
fun mtimes' (args, thy) f =
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   410
  let
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   411
    val (f', thy') = f thy
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   412
  in (mtimes thy' args f', thy') end
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   413
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   414
datatype tptp_atom_value =
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   415
    Atom_App of tptp_term
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   416
  | Atom_Arity of string * int (*FIXME instead of int could use type list*)
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   417
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   418
(*Adds constants to signature when explicit type declaration isn't
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   419
expected, e.g. FOL. "formula_level" means that the constants are to be interpreted
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   420
as predicates, otherwise as functions over individuals.*)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   421
fun type_atoms_to_thy config formula_level type_map tptp_atom_value thy =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   422
  let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   423
    val ind_type = interpret_type config thy type_map (Defined_type Type_Ind)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   424
    val value_type =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   425
      if formula_level then
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   426
        interpret_type config thy type_map (Defined_type Type_Bool)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   427
      else ind_type
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   428
  in case tptp_atom_value of
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   429
      Atom_App (Term_Func (symb, tptp_ts)) =>
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   430
        let
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   431
          val thy' = fold (fn t =>
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   432
            type_atoms_to_thy config false type_map (Atom_App t)) tptp_ts thy
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   433
        in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   434
          case symb of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   435
             Uninterpreted const_name =>
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   436
               declare_const_ifnot config const_name
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   437
                (mk_fun_type (replicate (length tptp_ts) ind_type) value_type I) thy'
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   438
               |> snd
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   439
           | _ => thy'
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   440
        end
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   441
    | Atom_App _ => thy
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   442
    | Atom_Arity (const_name, n_args) =>
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   443
        declare_const_ifnot config const_name
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   444
         (mk_fun_type (replicate n_args ind_type) value_type I) thy
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   445
        |> snd
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   446
  end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   447
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   448
(*FIXME only used until interpretation is implemented*)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   449
fun add_interp_symbols_to_thy config type_map thy =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   450
  let
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   451
    val ind_symbols = [UMinus, Sum, Difference, Product, Quotient, Quotient_E,
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   452
      Quotient_T, Quotient_F, Remainder_E, Remainder_T, Remainder_F, Floor,
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   453
      Ceiling, Truncate, Round, To_Int, To_Rat, To_Real, Distinct, Apply]
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   454
    val bool_symbols = [Less, LessEq, Greater, GreaterEq, EvalEq, Is_Int, Is_Rat]
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   455
    fun add_interp_symbol_to_thy formula_level symbol =
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   456
      type_atoms_to_thy config formula_level type_map
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   457
       (Atom_Arity (string_of_interpreted_symbol symbol, arity_of symbol))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   458
  in
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   459
    fold (add_interp_symbol_to_thy false) ind_symbols thy
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   460
    |> fold (add_interp_symbol_to_thy true) bool_symbols
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   461
  end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   462
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   463
(*Next batch of functions give info about Isabelle/HOL types*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   464
fun is_fun (Type ("fun", _)) = true
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   465
  | is_fun _ = false
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   466
fun is_prod (Type ("Product_Type.prod", _)) = true
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   467
  | is_prod _ = false
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   468
fun dom_type (Type ("fun", [ty1, _])) = ty1
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   469
fun is_prod_typed thy config symb =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   470
  let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   471
    fun symb_type const_name =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   472
      Sign.the_const_type thy (full_name thy config const_name)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   473
  in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   474
    case symb of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   475
       Uninterpreted const_name =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   476
         if is_fun (symb_type const_name) then
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   477
           symb_type const_name |> dom_type |> is_prod
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   478
         else false
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   479
     | _ => false
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   480
   end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   481
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   482
(*
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   483
 Information needed to be carried around:
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   484
 - constant mapping: maps constant names to Isabelle terms with fully-qualified
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   485
    names. This is fixed, and it is determined by declaration lines earlier
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   486
    in the script (i.e. constants must be declared before appearing in terms.
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   487
 - type context: maps bound variables to their Isabelle type. This is discarded
47515
e84838b78b6d tuned comments
sultana
parents: 47514
diff changeset
   488
    after each call of interpret_term since variables' cannot be bound across
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   489
    terms.
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   490
*)
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   491
fun interpret_term formula_level config language const_map var_types type_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   492
 tptp_t thy =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   493
  case tptp_t of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   494
    Term_Func (symb, tptp_ts) =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   495
       let
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   496
         val thy' =
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   497
           type_atoms_to_thy config formula_level type_map (Atom_App tptp_t) thy
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   498
       in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   499
         case symb of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   500
           Interpreted_ExtraLogic Apply =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   501
             (*apply the head of the argument list to the tail*)
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   502
             (mapply'
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   503
               (fold_map (interpret_term false config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   504
                var_types type_map) (tl tptp_ts) thy')
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   505
               (interpret_term formula_level config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   506
                var_types type_map (hd tptp_ts)))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   507
           | _ =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   508
              (*apply symb to tptp_ts*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   509
              if is_prod_typed thy' config symb then
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   510
                let
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   511
                  val (t, thy'') =
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   512
                    mtimes'
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   513
                      (fold_map (interpret_term false config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   514
                       var_types type_map) (tl tptp_ts) thy')
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   515
                      (interpret_term false config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   516
                       var_types type_map (hd tptp_ts))
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   517
                in (interpret_symbol config language const_map symb thy' $ t, thy'')
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   518
                end
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   519
              else
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   520
                (
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   521
                mapply'
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   522
                  (fold_map (interpret_term false config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   523
                   var_types type_map) tptp_ts thy')
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   524
                  (`(interpret_symbol config language const_map symb))
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   525
                )
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   526
       end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   527
  | Term_Var n =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   528
     (if language = THF orelse language = TFF then
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   529
        (case AList.lookup (op =) var_types n of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   530
           SOME ty =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   531
             (case ty of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   532
                SOME ty => Free (n, ty)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   533
              | NONE => Free (n, Term.dummyT) (*to infer the variable's type*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   534
             )
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   535
         | NONE =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   536
             raise MISINTERPRET_TERM ("Could not type variable", tptp_t))
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   537
      else (*variables range over individuals*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   538
        Free (n, interpret_type config thy type_map (Defined_type Type_Ind)),
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   539
      thy)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   540
  | Term_Conditional (tptp_formula, tptp_t1, tptp_t2) =>
47691
d9adc3061116 improved interpreting conditionals;
sultana
parents: 47690
diff changeset
   541
      let
d9adc3061116 improved interpreting conditionals;
sultana
parents: 47690
diff changeset
   542
        val (t_fmla, thy') =
d9adc3061116 improved interpreting conditionals;
sultana
parents: 47690
diff changeset
   543
          interpret_formula config language const_map var_types type_map tptp_formula thy
d9adc3061116 improved interpreting conditionals;
sultana
parents: 47690
diff changeset
   544
        val ([t1, t2], thy'') =
d9adc3061116 improved interpreting conditionals;
sultana
parents: 47690
diff changeset
   545
          fold_map (interpret_term formula_level config language const_map var_types type_map)
d9adc3061116 improved interpreting conditionals;
sultana
parents: 47690
diff changeset
   546
           [tptp_t1, tptp_t2] thy'
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   547
      in (mk_n_fun 3 @{const_name If} $ t_fmla $ t1 $ t2, thy'') end
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   548
  | Term_Num (number_kind, num) =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   549
      let
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   550
        (*FIXME hack*)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   551
        val ind_type = interpret_type config thy type_map (Defined_type Type_Ind)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   552
        val prefix = case number_kind of
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   553
            Int_num => "intn_"
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   554
          | Real_num => "realn_"
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   555
          | Rat_num => "ratn_"
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   556
      (*FIXME hack -- for Int_num should be
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   557
       HOLogic.mk_number @{typ "int"} (the (Int.fromString num))*)
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   558
      in declare_const_ifnot config (prefix ^ num) ind_type thy end
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   559
  | Term_Distinct_Object str =>
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   560
      declare_constant config ("do_" ^ str)
47514
0a870584145f improved handling of quoted names in tptp import
sultana
parents: 47513
diff changeset
   561
        (interpret_type config thy type_map (Defined_type Type_Ind)) thy
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   562
47691
d9adc3061116 improved interpreting conditionals;
sultana
parents: 47690
diff changeset
   563
and interpret_formula config language const_map var_types type_map tptp_fmla thy =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   564
  case tptp_fmla of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   565
      Pred app =>
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   566
        interpret_term true config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   567
         var_types type_map (Term_Func app) thy
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   568
    | Fmla (symbol, tptp_formulas) =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   569
       (case symbol of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   570
          Interpreted_ExtraLogic Apply =>
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   571
            mapply'
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   572
              (fold_map (interpret_formula config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   573
               var_types type_map) (tl tptp_formulas) thy)
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   574
              (interpret_formula config language const_map
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   575
               var_types type_map (hd tptp_formulas))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   576
        | Uninterpreted const_name =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   577
            let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   578
              val (args, thy') = (fold_map (interpret_formula config language
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   579
               const_map var_types type_map) tptp_formulas thy)
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   580
              val thy' =
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   581
                type_atoms_to_thy config true type_map
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   582
                 (Atom_Arity (const_name, length tptp_formulas)) thy'
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   583
            in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   584
              (if is_prod_typed thy' config symbol then
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   585
                 mtimes thy' args (interpret_symbol config language
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   586
                  const_map symbol thy')
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   587
               else
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   588
                mapply args
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   589
                 (interpret_symbol config language const_map symbol thy'),
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   590
              thy')
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   591
            end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   592
        | _ =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   593
            let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   594
              val (args, thy') =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   595
                fold_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   596
                 (interpret_formula config language
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   597
                  const_map var_types type_map)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   598
                 tptp_formulas thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   599
            in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   600
              (if is_prod_typed thy' config symbol then
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   601
                 mtimes thy' args (interpret_symbol config language
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   602
                  const_map symbol thy')
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   603
               else
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   604
                 mapply args
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   605
                  (interpret_symbol config language const_map symbol thy'),
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   606
               thy')
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   607
            end)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   608
    | Sequent _ =>
47510
sultana
parents: 47411
diff changeset
   609
        (*FIXME unsupported*)
sultana
parents: 47411
diff changeset
   610
        raise MISINTERPRET_FORMULA ("'Sequent' unsupported", tptp_fmla)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   611
    | Quant (quantifier, bindings, tptp_formula) =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   612
        let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   613
          val var_types' =
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   614
              ListPair.unzip bindings
47729
sultana
parents: 47692
diff changeset
   615
           |> (apsnd ((map o Option.map) (interpret_type config thy type_map)))
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   616
           |> ListPair.zip
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   617
           |> List.rev
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   618
          fun fold_bind f =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   619
            fold
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   620
              (fn (n, ty) => fn t =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   621
                 case ty of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   622
                   NONE =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   623
                     f (n,
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   624
                        if language = THF then Term.dummyT
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   625
                        else
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   626
                          interpret_type config thy type_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   627
                           (Defined_type Type_Ind),
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   628
                        t)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   629
                 | SOME ty => f (n, ty, t)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   630
              )
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   631
              var_types'
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   632
        in case quantifier of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   633
          Forall =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   634
            interpret_formula config language const_map (var_types' @ var_types)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   635
             type_map tptp_formula thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   636
            |>> fold_bind HOLogic.mk_all
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   637
        | Exists =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   638
            interpret_formula config language const_map (var_types' @ var_types)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   639
             type_map tptp_formula thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   640
            |>> fold_bind HOLogic.mk_exists
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   641
        | Lambda =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   642
            interpret_formula config language const_map (var_types' @ var_types)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   643
             type_map tptp_formula thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   644
            |>> fold_bind (fn (n, ty, rest) => lambda (Free (n, ty)) rest)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   645
        | Epsilon =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   646
            let val (t, thy') =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   647
              interpret_formula config language const_map var_types type_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   648
               (Quant (Lambda, bindings, tptp_formula)) thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   649
            in ((HOLogic.choice_const Term.dummyT) $ t, thy') end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   650
        | Iota =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   651
            let val (t, thy') =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   652
              interpret_formula config language const_map var_types type_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   653
               (Quant (Lambda, bindings, tptp_formula)) thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   654
            in (Const (@{const_name The}, Term.dummyT) $ t, thy') end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   655
        | Dep_Prod =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   656
            raise MISINTERPRET_FORMULA ("Unsupported", tptp_fmla)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   657
        | Dep_Sum =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   658
            raise MISINTERPRET_FORMULA ("Unsupported", tptp_fmla)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   659
        end
47359
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   660
    | Conditional (fmla1, fmla2, fmla3) =>
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   661
        let
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   662
          val interp = interpret_formula config language const_map var_types type_map
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   663
          val (((fmla1', fmla2'), fmla3'), thy') =
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   664
            interp fmla1 thy
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   665
            ||>> interp fmla2
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   666
            ||>> interp fmla3
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   667
        in (HOLogic.mk_conj (HOLogic.mk_imp (fmla1', fmla2'),
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   668
                             HOLogic.mk_imp (HOLogic.mk_not fmla1', fmla3')),
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   669
            thy')
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   670
        end
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   671
    | Let (tptp_let_list, tptp_formula) => (*FIXME not yet implemented*)
5a1ff6bcf3dc added interpretation for formula conditional;
sultana
parents: 47332
diff changeset
   672
        raise MISINTERPRET_FORMULA ("'Let' not yet implemented", tptp_fmla)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   673
    | Atom tptp_atom =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   674
        (case tptp_atom of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   675
          TFF_Typed_Atom (symbol, tptp_type_opt) =>
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   676
            (*FIXME ignoring type info*)
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   677
            (interpret_symbol config language const_map symbol thy, thy)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   678
        | THF_Atom_term tptp_term =>
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   679
            interpret_term true config language const_map var_types
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   680
             type_map tptp_term thy
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   681
        | THF_Atom_conn_term symbol =>
47570
df3c9aa6c9dc improved threading of thy-values through interpret functions;
sultana
parents: 47569
diff changeset
   682
            (interpret_symbol config language const_map symbol thy, thy))
47510
sultana
parents: 47411
diff changeset
   683
    | Type_fmla _ =>
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   684
         raise MISINTERPRET_FORMULA
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   685
          ("Cannot interpret types as formulas", tptp_fmla)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   686
    | THF_typing (tptp_formula, _) => (*FIXME ignoring type info*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   687
        interpret_formula config language
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   688
         const_map var_types type_map tptp_formula thy
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   689
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   690
(*Extract the type from a typing*)
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   691
local
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   692
  fun extract_type tptp_type =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   693
    case tptp_type of
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   694
        Fmla_type typ => fmlatype_to_type typ
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   695
      | _ => tptp_type
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   696
in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   697
  fun typeof_typing (THF_typing (_, tptp_type)) = extract_type tptp_type
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   698
  fun typeof_tff_typing (Atom (TFF_Typed_Atom (_, SOME tptp_type))) =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   699
    extract_type tptp_type
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   700
end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   701
fun nameof_typing
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   702
  (THF_typing
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   703
     ((Atom (THF_Atom_term (Term_Func (Uninterpreted str, [])))), _)) = str
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   704
fun nameof_tff_typing (Atom (TFF_Typed_Atom (Uninterpreted str, _))) = str
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   705
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   706
fun resolve_include_path path_prefixes path_suffix =
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   707
  case find_first (fn prefix => File.exists (Path.append prefix path_suffix))
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   708
         path_prefixes of
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   709
    SOME prefix => Path.append prefix path_suffix
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   710
  | NONE =>
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   711
    error ("Cannot find include file " ^ quote (Path.implode path_suffix))
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   712
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   713
fun interpret_line config inc_list type_map const_map path_prefixes line thy =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   714
  case line of
47569
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   715
     Include (_, quoted_path, inc_list) =>
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   716
       interpret_file'
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   717
        config
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   718
        inc_list
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   719
        path_prefixes
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   720
        (resolve_include_path path_prefixes
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   721
           (quoted_path |> unenclose |> Path.explode))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   722
        type_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   723
        const_map
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   724
        thy
47569
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   725
   | Annotated_Formula (_, lang, label, role, tptp_formula, annotation_opt) =>
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   726
       (*interpret a line only if "label" is in "inc_list", or if "inc_list" is
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   727
          empty (in which case interpret all lines)*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   728
       (*FIXME could work better if inc_list were sorted*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   729
       if null inc_list orelse is_some (List.find (fn x => x = label) inc_list) then
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   730
         case role of
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   731
           Role_Type =>
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   732
             let
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   733
               val (tptp_ty, name) =
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   734
                 if lang = THF then
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   735
                   (typeof_typing tptp_formula (*assuming tptp_formula is a typing*),
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   736
                    nameof_typing tptp_formula (*and that the LHS is a (atom) name*))
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   737
                 else
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   738
                   (typeof_tff_typing tptp_formula,
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   739
                    nameof_tff_typing tptp_formula)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   740
             in
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   741
               case tptp_ty of
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   742
                 Defined_type Type_Type => (*add new type*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   743
                   (*generate an Isabelle/HOL type to interpret this TPTP type,
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   744
                   and add it to both the Isabelle/HOL theory and to the type_map*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   745
                    let
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   746
                      val (type_map', thy') =
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   747
                        declare_type
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   748
                         config
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   749
                         (Atom_type name, name)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   750
                         type_map
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   751
                         thy
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   752
                    in ((type_map',
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   753
                         const_map,
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   754
                         []),
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   755
                        thy')
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   756
                    end
47513
50a424b89952 improved naming of 'distinct objects' in tptp import
sultana
parents: 47511
diff changeset
   757
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   758
               | _ => (*declaration of constant*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   759
                  (*Here we populate the map from constants to the Isabelle/HOL
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   760
                  terms they map to (which in turn contain their types).*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   761
                  let
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   762
                    val ty = interpret_type config thy type_map tptp_ty
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   763
                    (*make sure that the theory contains all the types appearing
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   764
                    in this constant's signature. Exception is thrown if encounter
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   765
                    an undeclared types.*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   766
                    val (t, thy') =
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   767
                      let
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   768
                        fun analyse_type thy thf_ty =
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   769
                           if #cautious config then
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   770
                            case thf_ty of
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   771
                               Fn_type (thf_ty1, thf_ty2) =>
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   772
                                 (analyse_type thy thf_ty1;
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   773
                                 analyse_type thy thf_ty2)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   774
                             | Atom_type ty_name =>
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   775
                                 if type_exists config thy ty_name then ()
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   776
                                 else
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   777
                                  raise MISINTERPRET_TYPE
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   778
                                   ("Type (in signature of " ^
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   779
                                      name ^ ") has not been declared",
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   780
                                     Atom_type ty_name)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   781
                             | _ => ()
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   782
                           else () (*skip test if we're not being cautious.*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   783
                      in
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   784
                        analyse_type thy tptp_ty;
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   785
                        declare_constant config name ty thy
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   786
                      end
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   787
                    (*register a mapping from name to t. Constants' type
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   788
                    declarations should occur at most once, so it's justified to
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   789
                    use "::". Note that here we use a constant's name rather
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   790
                    than the possibly- new one, since all references in the
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   791
                    theory will be to this name.*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   792
                    val const_map' = ((name, t) :: const_map)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   793
                  in ((type_map,(*type_map unchanged, since a constant's
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   794
                                  declaration shouldn't include any new types.*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   795
                       const_map',(*typing table of constant was extended*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   796
                       []),(*no formulas were interpreted*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   797
                      thy')(*theory was extended with new constant*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   798
                  end
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   799
             end
47513
50a424b89952 improved naming of 'distinct objects' in tptp import
sultana
parents: 47511
diff changeset
   800
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   801
         | _ => (*i.e. the AF is not a type declaration*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   802
             let
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   803
               (*gather interpreted term, and the map of types occurring in that term*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   804
               val (t, thy') =
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   805
                 interpret_formula config lang
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   806
                  const_map [] type_map tptp_formula thy
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   807
                 |>> HOLogic.mk_Trueprop
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   808
               (*type_maps grow monotonically, so return the newest value (type_mapped);
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   809
               there's no need to unify it with the type_map parameter.*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   810
             in
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   811
              ((type_map, const_map,
47548
60849d8c457d fixed type interpretation;
sultana
parents: 47520
diff changeset
   812
                [(label, role,
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   813
                  Syntax.check_term (Proof_Context.init_global thy') t,
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   814
                  TPTP_Proof.extract_inference_info annotation_opt)]), thy')
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   815
             end
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   816
       else (*do nothing if we're not to includ this AF*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   817
         ((type_map, const_map, []), thy)
47569
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   818
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   819
and interpret_problem config inc_list path_prefixes lines type_map const_map thy =
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   820
  let val thy_with_symbols = add_interp_symbols_to_thy config type_map thy in
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   821
    fold (fn line =>
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   822
           fn ((type_map, const_map, lines), thy) =>
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   823
             let
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   824
               (*process the line, ignoring the type-context for variables*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   825
               val ((type_map', const_map', l'), thy') =
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   826
                 interpret_line config inc_list type_map const_map path_prefixes
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   827
                   line thy
47688
3b53c944bece disabled exception packaging in tptp;
sultana
parents: 47686
diff changeset
   828
                 (*FIXME
47569
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   829
                   handle
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   830
                     (*package all exceptions to include position information,
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   831
                       even relating to multiple levels of "include"ed files*)
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   832
                       (*FIXME "exn" contents may appear garbled due to markup
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   833
                         FIXME this appears as ML source position *)
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   834
                       MISINTERPRET (pos_list, exn)  =>
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   835
                         raise MISINTERPRET
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   836
                           (TPTP_Syntax.pos_of_line line :: pos_list, exn)
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   837
                     | exn => raise MISINTERPRET
fce9d97a3258 exceptions related to interpreting tptp problems now mention the relevant position in the tptp file;
sultana
parents: 47558
diff changeset
   838
                         ([TPTP_Syntax.pos_of_line line], exn)
47688
3b53c944bece disabled exception packaging in tptp;
sultana
parents: 47686
diff changeset
   839
                  *)
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   840
             in
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   841
               ((type_map',
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   842
                 const_map',
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   843
                 l' @ lines),(*append is sufficient, union would be overkill*)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   844
                thy')
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   845
             end)
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   846
         lines (*lines of the problem file*)
47692
3d76c81b5ed2 improved non-interpretation of constants and numbers;
sultana
parents: 47691
diff changeset
   847
         ((type_map, const_map, []), thy_with_symbols) (*initial values*)
47511
016ce79deb01 enforced 'include' restrictions
sultana
parents: 47510
diff changeset
   848
  end
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   849
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   850
and interpret_file' config inc_list path_prefixes file_name =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   851
  let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   852
    val file_name' = Path.expand file_name
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   853
  in
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   854
    if Path.is_absolute file_name' then
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   855
      Path.implode file_name
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   856
      |> TPTP_Parser.parse_file
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   857
      |> interpret_problem config inc_list path_prefixes
47510
sultana
parents: 47411
diff changeset
   858
    else error "Could not determine absolute path to file"
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   859
  end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   860
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   861
and interpret_file cautious path_prefixes file_name =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   862
  let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   863
    val config =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   864
      {cautious = cautious,
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   865
       problem_name =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   866
        SOME (TPTP_Problem_Name.parse_problem_name ((Path.base #> Path.implode)
47520
ef2d04520337 improved exception-handling in tptp;
sultana
parents: 47519
diff changeset
   867
         file_name))}
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   868
  in interpret_file' config [] path_prefixes file_name end
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   869
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   870
fun import_file cautious path_prefixes file_name type_map const_map thy =
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   871
  let
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   872
    val prob_name =
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   873
      TPTP_Problem_Name.parse_problem_name (Path.implode (Path.base file_name))
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   874
    val (result, thy') =
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   875
      interpret_file cautious path_prefixes file_name type_map const_map thy
47520
ef2d04520337 improved exception-handling in tptp;
sultana
parents: 47519
diff changeset
   876
  (*FIXME doesn't check if problem has already been interpreted*)
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   877
  in TPTP_Data.map (cons ((prob_name, result))) thy' end
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   878
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   879
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46951
diff changeset
   880
  Outer_Syntax.improper_command @{command_spec "import_tptp"} "import TPTP problem"
46951
4e032ac36134 more precise TPTP keywords and dependencies;
wenzelm
parents: 46879
diff changeset
   881
    (Parse.path >> (fn path =>
47366
f5a304ca037e improved import_tptp to use standard TPTP directory structure; extended the TPTP testing theory to include an example of using the import_tptp command;
sultana
parents: 47360
diff changeset
   882
      Toplevel.theory (fn thy =>
47558
55b42f9af99d phase out "$TPTP_PROBLEMS_PATH"; prefer "$TPTP" for consistency with CASC setup
blanchet
parents: 47548
diff changeset
   883
       (*NOTE: assumes include files are located at $TPTP/Axioms
55b42f9af99d phase out "$TPTP_PROBLEMS_PATH"; prefer "$TPTP" for consistency with CASC setup
blanchet
parents: 47548
diff changeset
   884
         (which is how TPTP is organised)*)
48829
6ed588c4f963 look in current directory first before looking up includes in the TPTP directory, as required by Geoff
blanchet
parents: 47729
diff changeset
   885
       import_file true [Path.dir path, Path.explode "$TPTP"] path [] [] thy)))
46844
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   886
5d9aab0c609c added tptp parser;
sultana
parents:
diff changeset
   887
end