src/HOL/Tools/Sledgehammer/metis_clauses.ML
author blanchet
Mon Jun 28 18:02:36 2010 +0200 (2010-06-28)
changeset 37618 fa57a87f92a0
parent 37616 c8d2d84d6011
child 37620 537beae999d7
permissions -rw-r--r--
get rid of Skolem cache by performing CNF-conversion after fact selection
blanchet@37578
     1
(*  Title:      HOL/Tools/Sledgehammer/metis_clauses.ML
wenzelm@33311
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory
blanchet@36393
     3
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     4
wenzelm@33311
     5
Storing/printing FOL clauses and arity clauses.  Typed equality is
wenzelm@33311
     6
treated differently.
paulson@15347
     7
*)
paulson@15347
     8
blanchet@37578
     9
signature METIS_CLAUSES =
wenzelm@24310
    10
sig
blanchet@37577
    11
  type cnf_thm = Clausifier.cnf_thm
blanchet@37577
    12
  type name = string * string
blanchet@37577
    13
  type name_pool = string Symtab.table * string Symtab.table
blanchet@37577
    14
  datatype kind = Axiom | Conjecture
blanchet@37577
    15
  datatype type_literal =
blanchet@37577
    16
    TyLitVar of string * name |
blanchet@37577
    17
    TyLitFree of string * name
blanchet@37577
    18
  datatype arLit =
blanchet@37577
    19
      TConsLit of class * string * string list
blanchet@37577
    20
    | TVarLit of class * string
blanchet@37577
    21
  datatype arity_clause = ArityClause of
blanchet@37577
    22
   {axiom_name: string, conclLit: arLit, premLits: arLit list}
blanchet@37577
    23
  datatype classrel_clause = ClassrelClause of
blanchet@37577
    24
   {axiom_name: string, subclass: class, superclass: class}
blanchet@37577
    25
  datatype combtyp =
blanchet@37577
    26
    TyVar of name |
blanchet@37577
    27
    TyFree of name |
blanchet@37577
    28
    TyConstr of name * combtyp list
blanchet@37577
    29
  datatype combterm =
blanchet@37577
    30
    CombConst of name * combtyp * combtyp list (* Const and Free *) |
blanchet@37577
    31
    CombVar of name * combtyp |
blanchet@37577
    32
    CombApp of combterm * combterm
blanchet@37577
    33
  datatype literal = Literal of bool * combterm
blanchet@37577
    34
  datatype hol_clause =
blanchet@37577
    35
    HOLClause of {clause_id: int, axiom_name: string, th: thm, kind: kind,
blanchet@37577
    36
                  literals: literal list, ctypes_sorts: typ list}
blanchet@37577
    37
  exception TRIVIAL of unit
blanchet@37577
    38
blanchet@37616
    39
  val trace : bool Unsynchronized.ref
blanchet@37509
    40
  val type_wrapper_name : string
wenzelm@24310
    41
  val schematic_var_prefix: string
wenzelm@24310
    42
  val fixed_var_prefix: string
wenzelm@24310
    43
  val tvar_prefix: string
wenzelm@24310
    44
  val tfree_prefix: string
wenzelm@24310
    45
  val const_prefix: string
wenzelm@24310
    46
  val tconst_prefix: string
wenzelm@24310
    47
  val class_prefix: string
wenzelm@24310
    48
  val union_all: ''a list list -> ''a list
blanchet@37572
    49
  val invert_const: string -> string
wenzelm@24310
    50
  val ascii_of: string -> string
wenzelm@24310
    51
  val undo_ascii_of: string -> string
blanchet@37572
    52
  val strip_prefix: string -> string -> string option
wenzelm@24310
    53
  val make_schematic_var : string * int -> string
paulson@18868
    54
  val make_fixed_var : string -> string
paulson@18868
    55
  val make_schematic_type_var : string * int -> string
wenzelm@24310
    56
  val make_fixed_type_var : string -> string
blanchet@37498
    57
  val make_fixed_const : string -> string
blanchet@37498
    58
  val make_fixed_type_const : string -> string
paulson@18868
    59
  val make_type_class : string -> string
blanchet@37618
    60
  val skolem_theory_name: string
blanchet@37618
    61
  val skolem_prefix: string
blanchet@37618
    62
  val skolem_infix: string
blanchet@37618
    63
  val is_skolem_const_name: string -> bool
blanchet@37618
    64
  val num_type_args: theory -> string -> int
blanchet@36169
    65
  val empty_name_pool : bool -> name_pool option
blanchet@36169
    66
  val pool_map : ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
blanchet@36169
    67
  val nice_name : name -> name_pool option -> string * name_pool option
blanchet@36966
    68
  val type_literals_for_types : typ list -> type_literal list
blanchet@35865
    69
  val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
blanchet@35865
    70
  val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
blanchet@37577
    71
  val type_of_combterm : combterm -> combtyp
blanchet@37577
    72
  val strip_combterm_comb : combterm -> combterm * combterm list
blanchet@37577
    73
  val literals_of_term : theory -> term -> literal list * typ list
blanchet@37577
    74
  val conceal_skolem_somes :
blanchet@37577
    75
    int -> (string * term) list -> term -> (string * term) list * term
blanchet@37577
    76
  val is_quasi_fol_theorem : theory -> thm -> bool
blanchet@37577
    77
  val tfree_classes_of_terms : term list -> string list
blanchet@37577
    78
  val tvar_classes_of_terms : term list -> string list
blanchet@37577
    79
  val type_consts_of_terms : theory -> term list -> string list
blanchet@37577
    80
  val prepare_clauses :
blanchet@37577
    81
    bool -> thm list -> cnf_thm list -> cnf_thm list -> theory
blanchet@37577
    82
    -> string vector
blanchet@37577
    83
       * (hol_clause list * hol_clause list * hol_clause list * hol_clause list
blanchet@37577
    84
          * classrel_clause list * arity_clause list)
wenzelm@24310
    85
end
paulson@15347
    86
blanchet@37578
    87
structure Metis_Clauses : METIS_CLAUSES =
paulson@15347
    88
struct
paulson@15347
    89
blanchet@37577
    90
open Clausifier
blanchet@37577
    91
blanchet@37616
    92
val trace = Unsynchronized.ref false
blanchet@37616
    93
fun trace_msg msg = if !trace then tracing (msg ()) else ()
blanchet@37616
    94
blanchet@37509
    95
val type_wrapper_name = "ti"
blanchet@37509
    96
paulson@15347
    97
val schematic_var_prefix = "V_";
paulson@15347
    98
val fixed_var_prefix = "v_";
paulson@15347
    99
paulson@17230
   100
val tvar_prefix = "T_";
paulson@17230
   101
val tfree_prefix = "t_";
paulson@15347
   102
blanchet@37509
   103
val classrel_clause_prefix = "clsrel_";
paulson@15347
   104
paulson@17230
   105
val const_prefix = "c_";
wenzelm@24310
   106
val tconst_prefix = "tc_";
wenzelm@24310
   107
val class_prefix = "class_";
paulson@15347
   108
blanchet@36218
   109
fun union_all xss = fold (union (op =)) xss []
paulson@17775
   110
blanchet@36493
   111
(* Readable names for the more common symbolic functions. Do not mess with the
blanchet@37479
   112
   last nine entries of the table unless you know what you are doing. *)
paulson@15347
   113
val const_trans_table =
blanchet@35865
   114
  Symtab.make [(@{const_name "op ="}, "equal"),
blanchet@35865
   115
               (@{const_name "op &"}, "and"),
blanchet@35865
   116
               (@{const_name "op |"}, "or"),
blanchet@35865
   117
               (@{const_name "op -->"}, "implies"),
blanchet@36493
   118
               (@{const_name "op :"}, "in"),
blanchet@36493
   119
               (@{const_name fequal}, "fequal"),
blanchet@36493
   120
               (@{const_name COMBI}, "COMBI"),
blanchet@36493
   121
               (@{const_name COMBK}, "COMBK"),
blanchet@36493
   122
               (@{const_name COMBB}, "COMBB"),
blanchet@36493
   123
               (@{const_name COMBC}, "COMBC"),
blanchet@37479
   124
               (@{const_name COMBS}, "COMBS"),
blanchet@37479
   125
               (@{const_name True}, "True"),
blanchet@37479
   126
               (@{const_name False}, "False"),
blanchet@37572
   127
               (@{const_name If}, "If"),
blanchet@37572
   128
               (@{type_name "*"}, "prod"),
blanchet@37572
   129
               (@{type_name "+"}, "sum")]
paulson@15347
   130
blanchet@37572
   131
(* Invert the table of translations between Isabelle and ATPs. *)
blanchet@37572
   132
val const_trans_table_inv =
blanchet@37572
   133
  Symtab.update ("fequal", @{const_name "op ="})
blanchet@37572
   134
                (Symtab.make (map swap (Symtab.dest const_trans_table)))
blanchet@37572
   135
blanchet@37572
   136
val invert_const = perhaps (Symtab.lookup const_trans_table_inv)
paulson@15347
   137
paulson@15610
   138
(*Escaping of special characters.
paulson@15610
   139
  Alphanumeric characters are left unchanged.
paulson@15610
   140
  The character _ goes to __
paulson@15610
   141
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@24183
   142
  Other printing characters go to _nnn where nnn is the decimal ASCII code.*)
paulson@24183
   143
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   144
paulson@24183
   145
fun stringN_of_int 0 _ = ""
paulson@24183
   146
  | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
paulson@15610
   147
paulson@15347
   148
fun ascii_of_c c =
paulson@15610
   149
  if Char.isAlphaNum c then String.str c
paulson@15610
   150
  else if c = #"_" then "__"
wenzelm@24310
   151
  else if #" " <= c andalso c <= #"/"
paulson@15610
   152
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
wenzelm@24310
   153
  else if Char.isPrint c
paulson@24183
   154
       then ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
paulson@15610
   155
  else ""
paulson@15347
   156
paulson@15610
   157
val ascii_of = String.translate ascii_of_c;
paulson@15610
   158
paulson@24183
   159
(** Remove ASCII armouring from names in proof files **)
paulson@24183
   160
paulson@24183
   161
(*We don't raise error exceptions because this code can run inside the watcher.
paulson@24183
   162
  Also, the errors are "impossible" (hah!)*)
paulson@24183
   163
fun undo_ascii_aux rcs [] = String.implode(rev rcs)
paulson@24183
   164
  | undo_ascii_aux rcs [#"_"] = undo_ascii_aux (#"_"::rcs) []  (*ERROR*)
paulson@24183
   165
      (*Three types of _ escapes: __, _A to _P, _nnn*)
paulson@24183
   166
  | undo_ascii_aux rcs (#"_" :: #"_" :: cs) = undo_ascii_aux (#"_"::rcs) cs
wenzelm@24310
   167
  | undo_ascii_aux rcs (#"_" :: c :: cs) =
paulson@24183
   168
      if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
paulson@24183
   169
      then undo_ascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
wenzelm@24310
   170
      else
paulson@24183
   171
        let val digits = List.take (c::cs, 3) handle Subscript => []
wenzelm@24310
   172
        in
paulson@24183
   173
            case Int.fromString (String.implode digits) of
paulson@24183
   174
                NONE => undo_ascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
paulson@24183
   175
              | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
paulson@24183
   176
        end
paulson@24183
   177
  | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
paulson@24183
   178
paulson@24183
   179
val undo_ascii_of = undo_ascii_aux [] o String.explode;
paulson@15347
   180
blanchet@37572
   181
(* If string s has the prefix s1, return the result of deleting it,
blanchet@37572
   182
   un-ASCII'd. *)
blanchet@37572
   183
fun strip_prefix s1 s =
blanchet@37572
   184
  if String.isPrefix s1 s then
blanchet@37572
   185
    SOME (undo_ascii_of (String.extract (s, size s1, NONE)))
blanchet@37572
   186
  else
blanchet@37572
   187
    NONE
blanchet@37572
   188
paulson@16925
   189
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   190
fun trim_type_var s =
paulson@16925
   191
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   192
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   193
paulson@16903
   194
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   195
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   196
paulson@17230
   197
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   198
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   199
wenzelm@24310
   200
fun make_schematic_type_var (x,i) =
paulson@16925
   201
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   202
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   203
blanchet@37498
   204
fun lookup_const c =
blanchet@37498
   205
  case Symtab.lookup const_trans_table c of
blanchet@37498
   206
    SOME c' => c'
blanchet@37498
   207
  | NONE => ascii_of c
paulson@23075
   208
blanchet@36062
   209
(* "op =" MUST BE "equal" because it's built into ATPs. *)
blanchet@37498
   210
fun make_fixed_const @{const_name "op ="} = "equal"
blanchet@37498
   211
  | make_fixed_const c = const_prefix ^ lookup_const c
paulson@18411
   212
blanchet@37572
   213
fun make_fixed_type_const c = tconst_prefix ^ lookup_const c
quigley@17150
   214
wenzelm@17261
   215
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   216
blanchet@37618
   217
val skolem_theory_name = "Sledgehammer" ^ Long_Name.separator ^ "Sko"
blanchet@37618
   218
val skolem_prefix = "sko_"
blanchet@37618
   219
val skolem_infix = "$"
blanchet@37618
   220
blanchet@37618
   221
(*Keep the full complexity of the original name*)
blanchet@37618
   222
fun flatten_name s = space_implode "_X" (Long_Name.explode s);
blanchet@37618
   223
blanchet@37618
   224
(* Hack: Could return false positives (e.g., a user happens to declare a
blanchet@37618
   225
   constant called "SomeTheory.sko_means_shoe_in_$wedish". *)
blanchet@37618
   226
val is_skolem_const_name =
blanchet@37618
   227
  Long_Name.base_name
blanchet@37618
   228
  #> String.isPrefix skolem_prefix andf String.isSubstring skolem_infix
blanchet@37618
   229
blanchet@37618
   230
(* The number of type arguments of a constant, zero if it's monomorphic. For
blanchet@37618
   231
   (instances of) Skolem pseudoconstants, this information is encoded in the
blanchet@37618
   232
   constant name. *)
blanchet@37618
   233
fun num_type_args thy s =
blanchet@37618
   234
  if String.isPrefix skolem_theory_name s then
blanchet@37618
   235
    s |> unprefix skolem_theory_name
blanchet@37618
   236
      |> space_explode skolem_infix |> hd
blanchet@37618
   237
      |> space_explode "_" |> List.last |> Int.fromString |> the
blanchet@37618
   238
  else
blanchet@37618
   239
    (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
quigley@17150
   240
blanchet@36169
   241
(**** name pool ****)
blanchet@36169
   242
 
blanchet@36169
   243
type name = string * string
blanchet@36169
   244
type name_pool = string Symtab.table * string Symtab.table
blanchet@36169
   245
blanchet@36222
   246
fun empty_name_pool readable_names =
blanchet@36222
   247
  if readable_names then SOME (`I Symtab.empty) else NONE
blanchet@36169
   248
blanchet@36556
   249
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
blanchet@36169
   250
fun pool_map f xs =
blanchet@36556
   251
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
blanchet@36169
   252
blanchet@36169
   253
fun add_nice_name full_name nice_prefix j the_pool =
blanchet@36169
   254
  let
blanchet@36169
   255
    val nice_name = nice_prefix ^ (if j = 0 then "" else "_" ^ Int.toString j)
blanchet@36169
   256
  in
blanchet@36169
   257
    case Symtab.lookup (snd the_pool) nice_name of
blanchet@36169
   258
      SOME full_name' =>
blanchet@36169
   259
      if full_name = full_name' then (nice_name, the_pool)
blanchet@36169
   260
      else add_nice_name full_name nice_prefix (j + 1) the_pool
blanchet@36169
   261
    | NONE =>
blanchet@36169
   262
      (nice_name, (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@36169
   263
                   Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@36169
   264
  end
blanchet@36169
   265
blanchet@36169
   266
fun translate_first_char f s =
blanchet@36169
   267
  String.str (f (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
blanchet@36169
   268
blanchet@36222
   269
fun readable_name full_name s =
blanchet@36169
   270
  let
blanchet@37575
   271
    val s = s |> Long_Name.base_name |> Name.desymbolize false
blanchet@36169
   272
    val s' = s |> explode |> rev |> dropwhile (curry (op =) "'")
blanchet@36169
   273
    val s' =
blanchet@36169
   274
      (s' |> rev
blanchet@36169
   275
          |> implode
blanchet@36169
   276
          |> String.translate
blanchet@36221
   277
                 (fn c => if Char.isAlphaNum c orelse c = #"_" then String.str c
blanchet@36221
   278
                          else ""))
blanchet@36169
   279
      ^ replicate_string (String.size s - length s') "_"
blanchet@36169
   280
    val s' =
blanchet@36169
   281
      if s' = "" orelse not (Char.isAlpha (String.sub (s', 0))) then "X" ^ s'
blanchet@36169
   282
      else s'
blanchet@36476
   283
    (* Avoid "equal", since it's built into ATPs; and "op" is very ambiguous
blanchet@36476
   284
       ("op &", "op |", etc.). *)
blanchet@36476
   285
    val s' = if s' = "equal" orelse s' = "op" then full_name else s'
blanchet@36169
   286
  in
blanchet@36169
   287
    case (Char.isLower (String.sub (full_name, 0)),
blanchet@36169
   288
          Char.isLower (String.sub (s', 0))) of
blanchet@36169
   289
      (true, false) => translate_first_char Char.toLower s'
blanchet@36169
   290
    | (false, true) => translate_first_char Char.toUpper s'
blanchet@36169
   291
    | _ => s'
blanchet@36169
   292
  end
blanchet@36169
   293
blanchet@36169
   294
fun nice_name (full_name, _) NONE = (full_name, NONE)
blanchet@36169
   295
  | nice_name (full_name, desired_name) (SOME the_pool) =
blanchet@36169
   296
    case Symtab.lookup (fst the_pool) full_name of
blanchet@36169
   297
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@36222
   298
    | NONE => add_nice_name full_name (readable_name full_name desired_name) 0
blanchet@36222
   299
                            the_pool
blanchet@36169
   300
              |> apsnd SOME
blanchet@36169
   301
blanchet@37498
   302
(**** Definitions and functions for FOL clauses for TPTP format output ****)
paulson@15347
   303
blanchet@37577
   304
datatype kind = Axiom | Conjecture
paulson@23385
   305
paulson@15347
   306
(**** Isabelle FOL clauses ****)
paulson@15347
   307
blanchet@36556
   308
(* The first component is the type class; the second is a TVar or TFree. *)
blanchet@36556
   309
datatype type_literal =
blanchet@36556
   310
  TyLitVar of string * name |
blanchet@36556
   311
  TyLitFree of string * name
paulson@15347
   312
paulson@17404
   313
exception CLAUSE of string * term;
paulson@15347
   314
wenzelm@24310
   315
(*Make literals for sorted type variables*)
paulson@24940
   316
fun sorts_on_typs_aux (_, [])   = []
paulson@24940
   317
  | sorts_on_typs_aux ((x,i),  s::ss) =
paulson@24940
   318
      let val sorts = sorts_on_typs_aux ((x,i), ss)
paulson@22643
   319
      in
paulson@22643
   320
          if s = "HOL.type" then sorts
blanchet@36556
   321
          else if i = ~1 then TyLitFree (make_type_class s, `make_fixed_type_var x) :: sorts
blanchet@36556
   322
          else TyLitVar (make_type_class s, (make_schematic_type_var (x,i), x)) :: sorts
paulson@22643
   323
      end;
quigley@17150
   324
paulson@24940
   325
fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
paulson@24940
   326
  | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
paulson@24940
   327
paulson@24937
   328
(*Given a list of sorted type variables, return a list of type literals.*)
blanchet@36966
   329
fun type_literals_for_types Ts =
blanchet@36966
   330
  fold (union (op =)) (map sorts_on_typs Ts) []
mengj@20015
   331
mengj@20015
   332
(** make axiom and conjecture clauses. **)
mengj@20015
   333
paulson@15347
   334
(**** Isabelle arities ****)
paulson@15347
   335
wenzelm@24310
   336
datatype arLit = TConsLit of class * string * string list
paulson@22643
   337
               | TVarLit of class * string;
wenzelm@24310
   338
blanchet@35865
   339
datatype arity_clause =
blanchet@37500
   340
  ArityClause of {axiom_name: string, conclLit: arLit, premLits: arLit list}
paulson@15347
   341
paulson@15347
   342
paulson@18798
   343
fun gen_TVars 0 = []
paulson@18798
   344
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   345
paulson@18411
   346
fun pack_sort(_,[])  = []
paulson@18411
   347
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@22643
   348
  | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
wenzelm@24310
   349
paulson@18411
   350
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
blanchet@37498
   351
fun make_axiom_arity_clause (tcons, axiom_name, (cls,args)) =
paulson@21560
   352
   let val tvars = gen_TVars (length args)
paulson@17845
   353
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   354
   in
blanchet@37498
   355
     ArityClause {axiom_name = axiom_name, 
blanchet@37498
   356
                  conclLit = TConsLit (cls, make_fixed_type_const tcons, tvars),
blanchet@37498
   357
                  premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
paulson@17845
   358
   end;
paulson@15347
   359
paulson@15347
   360
paulson@15347
   361
(**** Isabelle class relations ****)
paulson@15347
   362
blanchet@35865
   363
datatype classrel_clause =
blanchet@37500
   364
  ClassrelClause of {axiom_name: string, subclass: class, superclass: class}
wenzelm@24310
   365
paulson@21290
   366
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
blanchet@37498
   367
fun class_pairs _ [] _ = []
paulson@21432
   368
  | class_pairs thy subs supers =
blanchet@36218
   369
      let
blanchet@36218
   370
        val class_less = Sorts.class_less (Sign.classes_of thy)
blanchet@36218
   371
        fun add_super sub super = class_less (sub, super) ? cons (sub, super)
blanchet@36218
   372
        fun add_supers sub = fold (add_super sub) supers
blanchet@36218
   373
      in fold add_supers subs [] end
paulson@15347
   374
blanchet@35865
   375
fun make_classrel_clause (sub,super) =
blanchet@37509
   376
  ClassrelClause {axiom_name = classrel_clause_prefix ^ ascii_of sub ^ "_" ^
blanchet@37509
   377
                               ascii_of super,
wenzelm@24310
   378
                  subclass = make_type_class sub,
paulson@21290
   379
                  superclass = make_type_class super};
paulson@15347
   380
paulson@21290
   381
fun make_classrel_clauses thy subs supers =
blanchet@35865
   382
  map make_classrel_clause (class_pairs thy subs supers);
paulson@18868
   383
paulson@18868
   384
paulson@18868
   385
(** Isabelle arities **)
paulson@17845
   386
blanchet@37498
   387
fun arity_clause _ _ (_, []) = []
blanchet@37498
   388
  | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
blanchet@37498
   389
      arity_clause seen n (tcons,ars)
blanchet@37498
   390
  | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
haftmann@36692
   391
      if member (op =) seen class then (*multiple arities for the same tycon, class pair*)
blanchet@37572
   392
          make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
blanchet@37498
   393
          arity_clause seen (n+1) (tcons,ars)
paulson@21373
   394
      else
blanchet@37572
   395
          make_axiom_arity_clause (tcons, lookup_const tcons ^ "_" ^ class, ar) ::
blanchet@37498
   396
          arity_clause (class::seen) n (tcons,ars)
paulson@17845
   397
blanchet@37498
   398
fun multi_arity_clause [] = []
blanchet@37498
   399
  | multi_arity_clause ((tcons, ars) :: tc_arlists) =
blanchet@37498
   400
      arity_clause [] 1 (tcons, ars) @ multi_arity_clause tc_arlists
paulson@17845
   401
paulson@22643
   402
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   403
  provided its arguments have the corresponding sorts.*)
paulson@21373
   404
fun type_class_pairs thy tycons classes =
paulson@21373
   405
  let val alg = Sign.classes_of thy
blanchet@36218
   406
      fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
blanchet@36218
   407
      fun add_class tycon class =
blanchet@36218
   408
        cons (class, domain_sorts tycon class)
blanchet@36218
   409
        handle Sorts.CLASS_ERROR _ => I
blanchet@36218
   410
      fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
paulson@21373
   411
  in  map try_classes tycons  end;
paulson@21373
   412
paulson@22643
   413
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
blanchet@37498
   414
fun iter_type_class_pairs _ _ [] = ([], [])
paulson@22643
   415
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   416
      let val cpairs = type_class_pairs thy tycons classes
haftmann@33040
   417
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
haftmann@33040
   418
            |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
wenzelm@24310
   419
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
haftmann@33042
   420
      in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
wenzelm@24310
   421
blanchet@37498
   422
fun make_arity_clauses thy tycons classes =
wenzelm@24310
   423
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
blanchet@37498
   424
  in  (classes', multi_arity_clause cpairs)  end;
paulson@18863
   425
blanchet@37577
   426
datatype combtyp =
blanchet@37577
   427
  TyVar of name |
blanchet@37577
   428
  TyFree of name |
blanchet@37577
   429
  TyConstr of name * combtyp list
blanchet@37577
   430
blanchet@37577
   431
datatype combterm =
blanchet@37577
   432
  CombConst of name * combtyp * combtyp list (* Const and Free *) |
blanchet@37577
   433
  CombVar of name * combtyp |
blanchet@37577
   434
  CombApp of combterm * combterm
blanchet@37577
   435
blanchet@37577
   436
datatype literal = Literal of bool * combterm
blanchet@37577
   437
blanchet@37577
   438
datatype hol_clause =
blanchet@37577
   439
  HOLClause of {clause_id: int, axiom_name: string, th: thm, kind: kind,
blanchet@37577
   440
                literals: literal list, ctypes_sorts: typ list}
blanchet@37577
   441
blanchet@37577
   442
(*********************************************************************)
blanchet@37577
   443
(* convert a clause with type Term.term to a clause with type clause *)
blanchet@37577
   444
(*********************************************************************)
blanchet@37577
   445
blanchet@37577
   446
(*Result of a function type; no need to check that the argument type matches.*)
blanchet@37577
   447
fun result_type (TyConstr (_, [_, tp2])) = tp2
blanchet@37577
   448
  | result_type _ = raise Fail "non-function type"
blanchet@37577
   449
blanchet@37577
   450
fun type_of_combterm (CombConst (_, tp, _)) = tp
blanchet@37577
   451
  | type_of_combterm (CombVar (_, tp)) = tp
blanchet@37577
   452
  | type_of_combterm (CombApp (t1, _)) = result_type (type_of_combterm t1)
blanchet@37577
   453
blanchet@37577
   454
(*gets the head of a combinator application, along with the list of arguments*)
blanchet@37577
   455
fun strip_combterm_comb u =
blanchet@37577
   456
    let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
blanchet@37577
   457
        |   stripc  x =  x
blanchet@37577
   458
    in stripc(u,[]) end
blanchet@37577
   459
blanchet@37577
   460
fun isFalse (Literal (pol, CombConst ((c, _), _, _))) =
blanchet@37577
   461
      (pol andalso c = "c_False") orelse (not pol andalso c = "c_True")
blanchet@37577
   462
  | isFalse _ = false;
blanchet@37577
   463
blanchet@37577
   464
fun isTrue (Literal (pol, CombConst ((c, _), _, _))) =
blanchet@37577
   465
      (pol andalso c = "c_True") orelse
blanchet@37577
   466
      (not pol andalso c = "c_False")
blanchet@37577
   467
  | isTrue _ = false;
blanchet@37577
   468
blanchet@37577
   469
fun isTaut (HOLClause {literals,...}) = exists isTrue literals;
blanchet@37577
   470
blanchet@37577
   471
fun type_of (Type (a, Ts)) =
blanchet@37577
   472
    let val (folTypes,ts) = types_of Ts in
blanchet@37577
   473
      (TyConstr (`make_fixed_type_const a, folTypes), ts)
blanchet@37577
   474
    end
blanchet@37577
   475
  | type_of (tp as TFree (a, _)) = (TyFree (`make_fixed_type_var a), [tp])
blanchet@37577
   476
  | type_of (tp as TVar (x, _)) =
blanchet@37577
   477
    (TyVar (make_schematic_type_var x, string_of_indexname x), [tp])
blanchet@37577
   478
and types_of Ts =
blanchet@37577
   479
    let val (folTyps, ts) = ListPair.unzip (map type_of Ts) in
blanchet@37577
   480
      (folTyps, union_all ts)
blanchet@37577
   481
    end
blanchet@37577
   482
blanchet@37577
   483
(* same as above, but no gathering of sort information *)
blanchet@37577
   484
fun simp_type_of (Type (a, Ts)) =
blanchet@37577
   485
      TyConstr (`make_fixed_type_const a, map simp_type_of Ts)
blanchet@37577
   486
  | simp_type_of (TFree (a, _)) = TyFree (`make_fixed_type_var a)
blanchet@37577
   487
  | simp_type_of (TVar (x, _)) =
blanchet@37577
   488
    TyVar (make_schematic_type_var x, string_of_indexname x)
blanchet@37577
   489
blanchet@37577
   490
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
blanchet@37577
   491
fun combterm_of thy (Const (c, T)) =
blanchet@37577
   492
      let
blanchet@37577
   493
        val (tp, ts) = type_of T
blanchet@37577
   494
        val tvar_list =
blanchet@37577
   495
          (if String.isPrefix skolem_theory_name c then
blanchet@37577
   496
             [] |> Term.add_tvarsT T |> map TVar
blanchet@37577
   497
           else
blanchet@37577
   498
             (c, T) |> Sign.const_typargs thy)
blanchet@37577
   499
          |> map simp_type_of
blanchet@37577
   500
        val c' = CombConst (`make_fixed_const c, tp, tvar_list)
blanchet@37577
   501
      in  (c',ts)  end
blanchet@37577
   502
  | combterm_of _ (Free(v, T)) =
blanchet@37577
   503
      let val (tp,ts) = type_of T
blanchet@37577
   504
          val v' = CombConst (`make_fixed_var v, tp, [])
blanchet@37577
   505
      in  (v',ts)  end
blanchet@37577
   506
  | combterm_of _ (Var(v, T)) =
blanchet@37577
   507
      let val (tp,ts) = type_of T
blanchet@37577
   508
          val v' = CombVar ((make_schematic_var v, string_of_indexname v), tp)
blanchet@37577
   509
      in  (v',ts)  end
blanchet@37577
   510
  | combterm_of thy (P $ Q) =
blanchet@37577
   511
      let val (P', tsP) = combterm_of thy P
blanchet@37577
   512
          val (Q', tsQ) = combterm_of thy Q
blanchet@37577
   513
      in  (CombApp (P', Q'), union (op =) tsP tsQ)  end
blanchet@37577
   514
  | combterm_of _ (t as Abs _) = raise Fail "HOL clause: Abs"
blanchet@37577
   515
blanchet@37577
   516
fun predicate_of thy ((@{const Not} $ P), pos) = predicate_of thy (P, not pos)
blanchet@37577
   517
  | predicate_of thy (t, pos) = (combterm_of thy (Envir.eta_contract t), pos)
blanchet@37577
   518
blanchet@37577
   519
fun literals_of_term1 args thy (@{const Trueprop} $ P) =
blanchet@37577
   520
    literals_of_term1 args thy P
blanchet@37577
   521
  | literals_of_term1 args thy (@{const "op |"} $ P $ Q) =
blanchet@37577
   522
    literals_of_term1 (literals_of_term1 args thy P) thy Q
blanchet@37577
   523
  | literals_of_term1 (lits, ts) thy P =
blanchet@37577
   524
    let val ((pred, ts'), pol) = predicate_of thy (P, true) in
blanchet@37577
   525
      (Literal (pol, pred) :: lits, union (op =) ts ts')
blanchet@37577
   526
    end
blanchet@37577
   527
val literals_of_term = literals_of_term1 ([], [])
blanchet@37577
   528
blanchet@37577
   529
fun skolem_name i j num_T_args =
blanchet@37577
   530
  skolem_prefix ^ (space_implode "_" (map Int.toString [i, j, num_T_args])) ^
blanchet@37577
   531
  skolem_infix ^ "g"
blanchet@37577
   532
blanchet@37577
   533
fun conceal_skolem_somes i skolem_somes t =
blanchet@37577
   534
  if exists_Const (curry (op =) @{const_name skolem_id} o fst) t then
blanchet@37577
   535
    let
blanchet@37577
   536
      fun aux skolem_somes
blanchet@37577
   537
              (t as (Const (@{const_name skolem_id}, Type (_, [_, T])) $ _)) =
blanchet@37577
   538
          let
blanchet@37577
   539
            val (skolem_somes, s) =
blanchet@37577
   540
              if i = ~1 then
blanchet@37577
   541
                (skolem_somes, @{const_name undefined})
blanchet@37577
   542
              else case AList.find (op aconv) skolem_somes t of
blanchet@37577
   543
                s :: _ => (skolem_somes, s)
blanchet@37577
   544
              | [] =>
blanchet@37577
   545
                let
blanchet@37577
   546
                  val s = skolem_theory_name ^ "." ^
blanchet@37577
   547
                          skolem_name i (length skolem_somes)
blanchet@37577
   548
                                        (length (Term.add_tvarsT T []))
blanchet@37577
   549
                in ((s, t) :: skolem_somes, s) end
blanchet@37577
   550
          in (skolem_somes, Const (s, T)) end
blanchet@37577
   551
        | aux skolem_somes (t1 $ t2) =
blanchet@37577
   552
          let
blanchet@37577
   553
            val (skolem_somes, t1) = aux skolem_somes t1
blanchet@37577
   554
            val (skolem_somes, t2) = aux skolem_somes t2
blanchet@37577
   555
          in (skolem_somes, t1 $ t2) end
blanchet@37577
   556
        | aux skolem_somes (Abs (s, T, t')) =
blanchet@37577
   557
          let val (skolem_somes, t') = aux skolem_somes t' in
blanchet@37577
   558
            (skolem_somes, Abs (s, T, t'))
blanchet@37577
   559
          end
blanchet@37577
   560
        | aux skolem_somes t = (skolem_somes, t)
blanchet@37577
   561
    in aux skolem_somes t end
blanchet@37577
   562
  else
blanchet@37577
   563
    (skolem_somes, t)
blanchet@37577
   564
blanchet@37577
   565
fun is_quasi_fol_theorem thy =
blanchet@37577
   566
  Meson.is_fol_term thy o snd o conceal_skolem_somes ~1 [] o prop_of
blanchet@37577
   567
blanchet@37577
   568
(* Trivial problem, which resolution cannot handle (empty clause) *)
blanchet@37577
   569
exception TRIVIAL of unit
blanchet@37577
   570
blanchet@37577
   571
(* making axiom and conjecture clauses *)
blanchet@37577
   572
fun make_clause thy (clause_id, axiom_name, kind, th) skolem_somes =
blanchet@37577
   573
  let
blanchet@37577
   574
    val (skolem_somes, t) =
blanchet@37577
   575
      th |> prop_of |> conceal_skolem_somes clause_id skolem_somes
blanchet@37577
   576
    val (lits, ctypes_sorts) = literals_of_term thy t
blanchet@37577
   577
  in
blanchet@37577
   578
    if forall isFalse lits then
blanchet@37577
   579
      raise TRIVIAL ()
blanchet@37577
   580
    else
blanchet@37577
   581
      (skolem_somes,
blanchet@37577
   582
       HOLClause {clause_id = clause_id, axiom_name = axiom_name, th = th,
blanchet@37577
   583
                  kind = kind, literals = lits, ctypes_sorts = ctypes_sorts})
blanchet@37577
   584
  end
blanchet@37577
   585
blanchet@37577
   586
fun add_axiom_clause thy (th, ((name, id), _ : thm)) (skolem_somes, clss) =
blanchet@37577
   587
  let
blanchet@37577
   588
    val (skolem_somes, cls) = make_clause thy (id, name, Axiom, th) skolem_somes
blanchet@37577
   589
  in (skolem_somes, clss |> not (isTaut cls) ? cons (name, cls)) end
blanchet@37577
   590
blanchet@37577
   591
fun make_axiom_clauses thy clauses =
blanchet@37577
   592
  ([], []) |> fold_rev (add_axiom_clause thy) clauses |> snd
blanchet@37577
   593
blanchet@37577
   594
fun make_conjecture_clauses thy =
blanchet@37577
   595
  let
blanchet@37577
   596
    fun aux _ _ [] = []
blanchet@37577
   597
      | aux n skolem_somes (th :: ths) =
blanchet@37577
   598
        let
blanchet@37577
   599
          val (skolem_somes, cls) =
blanchet@37577
   600
            make_clause thy (n, "conjecture", Conjecture, th) skolem_somes
blanchet@37577
   601
        in cls :: aux (n + 1) skolem_somes ths end
blanchet@37577
   602
  in aux 0 [] end
blanchet@37577
   603
blanchet@37577
   604
(** Helper clauses **)
blanchet@37577
   605
blanchet@37577
   606
fun count_combterm (CombConst ((c, _), _, _)) =
blanchet@37577
   607
    Symtab.map_entry c (Integer.add 1)
blanchet@37577
   608
  | count_combterm (CombVar _) = I
blanchet@37577
   609
  | count_combterm (CombApp (t1, t2)) = count_combterm t1 #> count_combterm t2
blanchet@37577
   610
fun count_literal (Literal (_, t)) = count_combterm t
blanchet@37577
   611
fun count_clause (HOLClause {literals, ...}) = fold count_literal literals
blanchet@37577
   612
blanchet@37577
   613
fun raw_cnf_rules_pairs ps = map (fn (name, thm) => (thm, ((name, 0), thm))) ps
blanchet@37577
   614
fun cnf_helper_thms thy raw =
blanchet@37577
   615
  map (`Thm.get_name_hint)
blanchet@37577
   616
  #> (if raw then raw_cnf_rules_pairs else cnf_rules_pairs thy)
blanchet@37577
   617
blanchet@37577
   618
val optional_helpers =
blanchet@37577
   619
  [(["c_COMBI", "c_COMBK"], (false, @{thms COMBI_def COMBK_def})),
blanchet@37577
   620
   (["c_COMBB", "c_COMBC"], (false, @{thms COMBB_def COMBC_def})),
blanchet@37577
   621
   (["c_COMBS"], (false, @{thms COMBS_def}))]
blanchet@37577
   622
val optional_typed_helpers =
blanchet@37577
   623
  [(["c_True", "c_False"], (true, @{thms True_or_False})),
blanchet@37577
   624
   (["c_If"], (true, @{thms if_True if_False True_or_False}))]
blanchet@37577
   625
val mandatory_helpers = @{thms fequal_imp_equal equal_imp_fequal}
blanchet@37577
   626
blanchet@37577
   627
val init_counters =
blanchet@37577
   628
  Symtab.make (maps (maps (map (rpair 0) o fst))
blanchet@37577
   629
                    [optional_helpers, optional_typed_helpers])
blanchet@37577
   630
blanchet@37577
   631
fun get_helper_clauses thy is_FO full_types conjectures axcls =
blanchet@37577
   632
  let
blanchet@37577
   633
    val axclauses = map snd (make_axiom_clauses thy axcls)
blanchet@37577
   634
    val ct = fold (fold count_clause) [conjectures, axclauses] init_counters
blanchet@37577
   635
    fun is_needed c = the (Symtab.lookup ct c) > 0
blanchet@37577
   636
    val cnfs =
blanchet@37577
   637
      (optional_helpers
blanchet@37577
   638
       |> full_types ? append optional_typed_helpers
blanchet@37577
   639
       |> maps (fn (ss, (raw, ths)) =>
blanchet@37577
   640
                   if exists is_needed ss then cnf_helper_thms thy raw ths
blanchet@37577
   641
                   else []))
blanchet@37577
   642
      @ (if is_FO then [] else cnf_helper_thms thy false mandatory_helpers)
blanchet@37577
   643
  in map snd (make_axiom_clauses thy cnfs) end
blanchet@37577
   644
blanchet@37577
   645
fun make_clause_table xs =
blanchet@37577
   646
  fold (Termtab.update o `(prop_of o fst)) xs Termtab.empty
blanchet@37577
   647
blanchet@37577
   648
blanchet@37577
   649
(***************************************************************)
blanchet@37577
   650
(* Type Classes Present in the Axiom or Conjecture Clauses     *)
blanchet@37577
   651
(***************************************************************)
blanchet@37577
   652
blanchet@37577
   653
fun set_insert (x, s) = Symtab.update (x, ()) s
blanchet@37577
   654
blanchet@37577
   655
fun add_classes (sorts, cset) = List.foldl set_insert cset (flat sorts)
blanchet@37577
   656
blanchet@37577
   657
(*Remove this trivial type class*)
blanchet@37577
   658
fun delete_type cset = Symtab.delete_safe (the_single @{sort HOL.type}) cset;
blanchet@37577
   659
blanchet@37577
   660
fun tfree_classes_of_terms ts =
blanchet@37577
   661
  let val sorts_list = map (map #2 o OldTerm.term_tfrees) ts
blanchet@37577
   662
  in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
blanchet@37577
   663
blanchet@37577
   664
fun tvar_classes_of_terms ts =
blanchet@37577
   665
  let val sorts_list = map (map #2 o OldTerm.term_tvars) ts
blanchet@37577
   666
  in  Symtab.keys (delete_type (List.foldl add_classes Symtab.empty sorts_list))  end;
blanchet@37577
   667
blanchet@37577
   668
(*fold type constructors*)
blanchet@37577
   669
fun fold_type_consts f (Type (a, Ts)) x = fold (fold_type_consts f) Ts (f (a,x))
blanchet@37577
   670
  | fold_type_consts _ _ x = x;
blanchet@37577
   671
blanchet@37577
   672
(*Type constructors used to instantiate overloaded constants are the only ones needed.*)
blanchet@37577
   673
fun add_type_consts_in_term thy =
blanchet@37577
   674
  let
blanchet@37577
   675
    val const_typargs = Sign.const_typargs thy
blanchet@37577
   676
    fun aux (Const x) = fold (fold_type_consts set_insert) (const_typargs x)
blanchet@37577
   677
      | aux (Abs (_, _, u)) = aux u
blanchet@37577
   678
      | aux (Const (@{const_name skolem_id}, _) $ _) = I
blanchet@37577
   679
      | aux (t $ u) = aux t #> aux u
blanchet@37577
   680
      | aux _ = I
blanchet@37577
   681
  in aux end
blanchet@37577
   682
blanchet@37577
   683
fun type_consts_of_terms thy ts =
blanchet@37577
   684
  Symtab.keys (fold (add_type_consts_in_term thy) ts Symtab.empty);
blanchet@37577
   685
blanchet@37577
   686
(* Remove existing axiom clauses from the conjecture clauses, as this can
blanchet@37577
   687
   dramatically boost an ATP's performance (for some reason). *)
blanchet@37577
   688
fun subtract_cls ax_clauses =
blanchet@37577
   689
  filter_out (Termtab.defined (make_clause_table ax_clauses) o prop_of)
blanchet@37577
   690
blanchet@37577
   691
(* prepare for passing to writer,
blanchet@37577
   692
   create additional clauses based on the information from extra_cls *)
blanchet@37577
   693
fun prepare_clauses full_types goal_cls axcls extra_cls thy =
blanchet@37577
   694
  let
blanchet@37577
   695
    val is_FO = forall (Meson.is_fol_term thy o prop_of) goal_cls
blanchet@37577
   696
    val ccls = subtract_cls extra_cls goal_cls
blanchet@37577
   697
    val _ = app (fn th => trace_msg (fn _ => Display.string_of_thm_global thy th)) ccls
blanchet@37577
   698
    val ccltms = map prop_of ccls
blanchet@37577
   699
    and axtms = map (prop_of o #1) extra_cls
blanchet@37577
   700
    val subs = tfree_classes_of_terms ccltms
blanchet@37577
   701
    and supers = tvar_classes_of_terms axtms
blanchet@37577
   702
    and tycons = type_consts_of_terms thy (ccltms @ axtms)
blanchet@37577
   703
    (*TFrees in conjecture clauses; TVars in axiom clauses*)
blanchet@37577
   704
    val conjectures = make_conjecture_clauses thy ccls
blanchet@37577
   705
    val (_, extra_clauses) = ListPair.unzip (make_axiom_clauses thy extra_cls)
blanchet@37577
   706
    val (clnames, axiom_clauses) = ListPair.unzip (make_axiom_clauses thy axcls)
blanchet@37577
   707
    val helper_clauses =
blanchet@37577
   708
      get_helper_clauses thy is_FO full_types conjectures extra_cls
blanchet@37577
   709
    val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@37577
   710
    val classrel_clauses = make_classrel_clauses thy subs supers'
blanchet@37577
   711
  in
blanchet@37577
   712
    (Vector.fromList clnames,
blanchet@37577
   713
      (conjectures, axiom_clauses, extra_clauses, helper_clauses, classrel_clauses, arity_clauses))
blanchet@37577
   714
  end
blanchet@37577
   715
paulson@15347
   716
end;