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