src/Pure/sign.ML
author skalberg
Sun Feb 13 17:15:14 2005 +0100 (2005-02-13)
changeset 15531 08c8dad8e399
parent 14993 802f3732a54e
child 15570 8d8c70b41bab
permissions -rw-r--r--
Deleted Library.option type.
wenzelm@19
     1
(*  Title:      Pure/sign.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@251
     3
    Author:     Lawrence C Paulson and Markus Wenzel
clasohm@0
     4
wenzelm@251
     5
The abstract type "sg" of signatures.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@3956
     8
(*base names*)
wenzelm@3956
     9
type bstring = string;
wenzelm@3956
    10
type bclass = class;
wenzelm@3956
    11
(*external forms -- partially qualified names*)
wenzelm@3805
    12
type xstring = string;
wenzelm@3805
    13
type xclass = class;
wenzelm@3805
    14
type xsort = sort;
wenzelm@3805
    15
type xtyp = typ;
wenzelm@3805
    16
type xterm = term;
wenzelm@3805
    17
wenzelm@19
    18
signature SIGN =
wenzelm@3791
    19
sig
paulson@1501
    20
  type sg
wenzelm@3967
    21
  type sg_ref
wenzelm@4256
    22
  type data
wenzelm@2197
    23
  val rep_sg: sg ->
wenzelm@3975
    24
   {self: sg_ref,
wenzelm@14784
    25
    tsig: Type.tsig,
wenzelm@14784
    26
    consts: (typ * stamp) Symtab.table,
wenzelm@11501
    27
    path: string list option,
wenzelm@3791
    28
    spaces: (string * NameSpace.T) list,
wenzelm@4256
    29
    data: data}
wenzelm@3975
    30
  val name_of: sg -> string
wenzelm@3975
    31
  val stamp_names_of: sg -> string list
wenzelm@10932
    32
  val exists_stamp: string -> sg -> bool
wenzelm@14784
    33
  val tsig_of: sg -> Type.tsig
wenzelm@14905
    34
  val is_logtype: sg -> string -> bool
wenzelm@3967
    35
  val deref: sg_ref -> sg
wenzelm@3967
    36
  val self_ref: sg -> sg_ref
paulson@1501
    37
  val subsig: sg * sg -> bool
wenzelm@9031
    38
  val joinable: sg * sg -> bool
paulson@1501
    39
  val eq_sg: sg * sg -> bool
paulson@1501
    40
  val same_sg: sg * sg -> bool
paulson@1501
    41
  val is_draft: sg -> bool
wenzelm@4951
    42
  val is_stale: sg -> bool
wenzelm@14645
    43
  val syn_of: sg -> Syntax.syntax
paulson@1501
    44
  val const_type: sg -> string -> typ option
paulson@1501
    45
  val classes: sg -> class list
wenzelm@4844
    46
  val defaultS: sg -> sort
paulson@1501
    47
  val subsort: sg -> sort * sort -> bool
wenzelm@8290
    48
  val nodup_vars: term -> term
wenzelm@4568
    49
  val of_sort: sg -> typ * sort -> bool
wenzelm@7640
    50
  val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
wenzelm@14784
    51
  val universal_witness: sg -> (typ * sort) option
wenzelm@10443
    52
  val typ_instance: sg -> typ * typ -> bool
wenzelm@3810
    53
  val classK: string
wenzelm@3810
    54
  val typeK: string
wenzelm@3810
    55
  val constK: string
wenzelm@3956
    56
  val full_name: sg -> bstring -> string
wenzelm@4844
    57
  val full_name_path: sg -> string -> bstring -> string
wenzelm@3956
    58
  val base_name: string -> bstring
wenzelm@3810
    59
  val intern: sg -> string -> xstring -> string
wenzelm@3810
    60
  val extern: sg -> string -> string -> xstring
wenzelm@3937
    61
  val cond_extern: sg -> string -> string -> xstring
wenzelm@6845
    62
  val cond_extern_table: sg -> string -> 'a Symtab.table -> (xstring * 'a) list
schirmer@14700
    63
  val extern_typ: sg -> typ -> typ
wenzelm@3805
    64
  val intern_class: sg -> xclass -> class
wenzelm@3805
    65
  val intern_tycon: sg -> xstring -> string
wenzelm@3805
    66
  val intern_const: sg -> xstring -> string
wenzelm@3937
    67
  val intern_sort: sg -> xsort -> sort
wenzelm@3937
    68
  val intern_typ: sg -> xtyp -> typ
wenzelm@3937
    69
  val intern_term: sg -> xterm -> term
wenzelm@3937
    70
  val intern_tycons: sg -> xtyp -> typ
paulson@1501
    71
  val pretty_sg: sg -> Pretty.T
wenzelm@4051
    72
  val str_of_sg: sg -> string
paulson@1501
    73
  val pprint_sg: sg -> pprint_args -> unit
wenzelm@14645
    74
  val PureN: string
wenzelm@14645
    75
  val CPureN: string
wenzelm@14645
    76
  val pre_pure: sg
paulson@1501
    77
  val pretty_term: sg -> term -> Pretty.T
wenzelm@12068
    78
  val pretty_term': Syntax.syntax -> sg -> term -> Pretty.T
paulson@1501
    79
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    80
  val pretty_sort: sg -> sort -> Pretty.T
wenzelm@14828
    81
  val pretty_classrel: sg -> class list -> Pretty.T
wenzelm@14828
    82
  val pretty_arity: sg -> arity -> Pretty.T
paulson@1501
    83
  val string_of_term: sg -> term -> string
paulson@1501
    84
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    85
  val string_of_sort: sg -> sort -> string
wenzelm@14828
    86
  val string_of_classrel: sg -> class list -> string
wenzelm@14828
    87
  val string_of_arity: sg -> arity -> string
paulson@1501
    88
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    89
  val pprint_typ: sg -> typ -> pprint_args -> unit
wenzelm@14828
    90
  val pp: sg -> Pretty.pp
wenzelm@8898
    91
  val certify_class: sg -> class -> class
wenzelm@8898
    92
  val certify_sort: sg -> sort -> sort
paulson@1501
    93
  val certify_typ: sg -> typ -> typ
wenzelm@14784
    94
  val certify_typ_raw: sg -> typ -> typ
wenzelm@11720
    95
  val certify_tyname: sg -> string -> string
wenzelm@10443
    96
  val certify_const: sg -> string -> string
wenzelm@14828
    97
  val certify_term: Pretty.pp -> sg -> term -> term * typ * int
wenzelm@8898
    98
  val read_sort: sg -> string -> sort
wenzelm@4227
    99
  val read_raw_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
   100
  val read_typ: sg * (indexname -> sort option) -> string -> typ
wenzelm@12068
   101
  val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@14784
   102
  val read_typ_raw': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@14828
   103
  val inst_typ_tvars: sg -> (indexname * typ) list -> typ -> typ
wenzelm@14828
   104
  val inst_term_tvars: sg -> (indexname * typ) list -> term -> term
wenzelm@14828
   105
  val infer_types: Pretty.pp -> sg -> (indexname -> typ option) ->
paulson@1501
   106
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   107
    -> xterm list * typ -> term * (indexname * typ) list
wenzelm@14828
   108
  val infer_types_simult: Pretty.pp -> sg -> (indexname -> typ option) ->
wenzelm@4249
   109
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   110
    -> (xterm list * typ) list -> term list * (indexname * typ) list
wenzelm@14905
   111
  val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
wenzelm@14828
   112
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@12068
   113
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8607
   114
  val read_def_terms:
wenzelm@8607
   115
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@8607
   116
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8802
   117
  val simple_read_term: sg -> typ -> string -> term
wenzelm@14645
   118
  val const_of_class: class -> string
wenzelm@14645
   119
  val class_of_const: string -> class
wenzelm@3956
   120
  val add_classes: (bclass * xclass list) list -> sg -> sg
wenzelm@3956
   121
  val add_classes_i: (bclass * class list) list -> sg -> sg
wenzelm@3805
   122
  val add_classrel: (xclass * xclass) list -> sg -> sg
wenzelm@3791
   123
  val add_classrel_i: (class * class) list -> sg -> sg
wenzelm@8898
   124
  val add_defsort: string -> sg -> sg
wenzelm@3791
   125
  val add_defsort_i: sort -> sg -> sg
wenzelm@3956
   126
  val add_types: (bstring * int * mixfix) list -> sg -> sg
wenzelm@4844
   127
  val add_nonterminals: bstring list -> sg -> sg
wenzelm@3956
   128
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
wenzelm@3956
   129
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
wenzelm@8898
   130
  val add_arities: (xstring * string list * string) list -> sg -> sg
wenzelm@3791
   131
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
wenzelm@3956
   132
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   133
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   134
  val add_syntax: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   135
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   136
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   137
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
paulson@1501
   138
  val add_trfuns:
wenzelm@4344
   139
    (string * (ast list -> ast)) list *
wenzelm@4344
   140
    (string * (term list -> term)) list *
wenzelm@4344
   141
    (string * (term list -> term)) list *
wenzelm@4344
   142
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
   143
  val add_trfunsT:
wenzelm@4344
   144
    (string * (bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@14645
   145
  val add_advanced_trfuns:
wenzelm@14645
   146
    (string * (sg -> ast list -> ast)) list *
wenzelm@14645
   147
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   148
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   149
    (string * (sg -> ast list -> ast)) list -> sg -> sg
wenzelm@14645
   150
  val add_advanced_trfunsT:
wenzelm@14645
   151
    (string * (sg -> bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
   152
  val add_tokentrfuns:
wenzelm@6311
   153
    (string * string * (string -> string * real)) list -> sg -> sg
wenzelm@4619
   154
  val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
paulson@1810
   155
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
   156
  val add_path: string -> sg -> sg
wenzelm@3810
   157
  val add_space: string * string list -> sg -> sg
wenzelm@12588
   158
  val hide_space: bool -> string * string list -> sg -> sg
wenzelm@12588
   159
  val hide_space_i: bool -> string * string list -> sg -> sg
paulson@1501
   160
  val add_name: string -> sg -> sg
wenzelm@4256
   161
  val data_kinds: data -> string list
wenzelm@3967
   162
  val merge_refs: sg_ref * sg_ref -> sg_ref
wenzelm@4627
   163
  val merge: sg * sg -> sg
wenzelm@12123
   164
  val copy: sg -> sg
wenzelm@3975
   165
  val prep_ext: sg -> sg
wenzelm@4627
   166
  val nontriv_merge: sg * sg -> sg
wenzelm@3791
   167
end;
clasohm@0
   168
wenzelm@6191
   169
signature PRIVATE_SIGN =
wenzelm@5642
   170
sig
wenzelm@5642
   171
  include SIGN
wenzelm@6546
   172
  val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
wenzelm@12310
   173
    (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
wenzelm@5642
   174
  val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
wenzelm@5642
   175
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
wenzelm@5642
   176
  val print_data: Object.kind -> sg -> unit
wenzelm@5642
   177
end;
wenzelm@5642
   178
wenzelm@6191
   179
structure Sign: PRIVATE_SIGN =
wenzelm@143
   180
struct
clasohm@0
   181
wenzelm@251
   182
(** datatype sg **)
wenzelm@251
   183
wenzelm@14645
   184
(* types sg, data, syn, sg_ref *)
wenzelm@4256
   185
wenzelm@19
   186
datatype sg =
wenzelm@3975
   187
  Sg of
wenzelm@3975
   188
   {id: string ref,                             (*id*)
wenzelm@14645
   189
    stamps: string ref list,                    (*unique theory indentifier*)
wenzelm@14645
   190
    syn: syn} *                                 (*syntax for parsing and printing*)
wenzelm@3975
   191
   {self: sg_ref,                               (*mutable self reference*)
wenzelm@14784
   192
    tsig: Type.tsig,                            (*order-sorted signature of types*)
wenzelm@14784
   193
    consts: (typ * stamp) Symtab.table,         (*type schemes of constants*)
wenzelm@11501
   194
    path: string list option,                   (*current name space entry prefix*)
wenzelm@3975
   195
    spaces: (string * NameSpace.T) list,        (*name spaces for consts, types etc.*)
wenzelm@4256
   196
    data: data}                                 (*anytype data*)
wenzelm@4256
   197
and data =
wenzelm@4256
   198
  Data of
wenzelm@10404
   199
    (Object.kind *                              (*kind (for authorization)*)
wenzelm@10404
   200
      (Object.T *                               (*value*)
wenzelm@12123
   201
        ((Object.T -> Object.T) *               (*copy method*)
wenzelm@12123
   202
          (Object.T -> Object.T) *              (*prepare extend method*)
wenzelm@4998
   203
          (Object.T * Object.T -> Object.T) *   (*merge and prepare extend method*)
wenzelm@4998
   204
          (sg -> Object.T -> unit))))           (*print method*)
wenzelm@4256
   205
    Symtab.table
wenzelm@14645
   206
and syn =
wenzelm@14645
   207
  Syn of
wenzelm@14645
   208
    Syntax.syntax *
wenzelm@14645
   209
     (((sg -> ast list -> ast) * stamp) Symtab.table *
wenzelm@14645
   210
      ((sg -> term list -> term) * stamp) Symtab.table *
wenzelm@14645
   211
      ((sg -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
wenzelm@14645
   212
      ((sg -> ast list -> ast) * stamp) list Symtab.table)
wenzelm@3967
   213
and sg_ref =
wenzelm@4998
   214
  SgRef of sg ref option;
clasohm@0
   215
wenzelm@3967
   216
(*make signature*)
wenzelm@14784
   217
fun make_sign (id, self, tsig, consts, syn, path, spaces, data, stamps) =
wenzelm@14645
   218
  Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
wenzelm@14784
   219
    consts = consts, path = path, spaces = spaces, data = data});
wenzelm@3975
   220
wenzelm@3975
   221
wenzelm@4256
   222
(* basic operations *)
wenzelm@3975
   223
wenzelm@3975
   224
fun rep_sg (Sg (_, args)) = args;
wenzelm@3967
   225
wenzelm@3975
   226
fun stamp_names_of (Sg ({stamps, ...}, _)) = rev (map ! stamps);
wenzelm@10932
   227
fun exists_stamp name (Sg ({stamps, ...}, _)) = exists (equal name o !) stamps;
wenzelm@3975
   228
fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg);
wenzelm@3975
   229
val str_of_sg = Pretty.str_of o pretty_sg;
wenzelm@3975
   230
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@3975
   231
wenzelm@402
   232
val tsig_of = #tsig o rep_sg;
wenzelm@14905
   233
fun is_logtype sg c = c mem_string Type.logical_types (tsig_of sg);
wenzelm@14784
   234
fun const_type (Sg (_, {consts, ...})) c = apsome #1 (Symtab.lookup (consts, c));
wenzelm@3967
   235
wenzelm@3967
   236
wenzelm@3995
   237
(* id and self *)
wenzelm@3967
   238
wenzelm@3975
   239
fun check_stale (sg as Sg ({id, ...},
skalberg@15531
   240
        {self = SgRef (SOME (ref (Sg ({id = id', ...}, _)))), ...})) =
wenzelm@3967
   241
      if id = id' then sg
wenzelm@3975
   242
      else raise TERM ("Stale signature: " ^ str_of_sg sg, [])
wenzelm@3967
   243
  | check_stale _ = sys_error "Sign.check_stale";
clasohm@0
   244
wenzelm@4951
   245
fun is_stale sg = (check_stale sg; false) handle TERM _ => true;
wenzelm@4951
   246
wenzelm@3995
   247
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale sg; self);
wenzelm@3995
   248
skalberg@15531
   249
fun deref (SgRef (SOME (ref sg))) = sg
skalberg@15531
   250
  | deref (SgRef NONE) = sys_error "Sign.deref";
wenzelm@3995
   251
wenzelm@3975
   252
fun name_of (sg as Sg ({id = ref name, ...}, _)) =
wenzelm@4844
   253
  if name = "" orelse ord name = ord "#" then
wenzelm@3975
   254
    raise TERM ("Nameless signature " ^ str_of_sg sg, [])
wenzelm@3975
   255
  else name;
wenzelm@3975
   256
nipkow@206
   257
wenzelm@2979
   258
(* inclusion and equality *)
paulson@2180
   259
wenzelm@2185
   260
local
wenzelm@2185
   261
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
   262
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
   263
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   264
wenzelm@2185
   265
  fun subset_stamp ([], ys) = true
wenzelm@2185
   266
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   267
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
paulson@2180
   268
wenzelm@2185
   269
  (*fast partial test*)
wenzelm@2185
   270
  fun fast_sub ([]: string ref list, _) = true
wenzelm@2185
   271
    | fast_sub (_, []) = false
wenzelm@2185
   272
    | fast_sub (x :: xs, y :: ys) =
wenzelm@2185
   273
        if x = y then fast_sub (xs, ys)
wenzelm@2185
   274
        else fast_sub (x :: xs, ys);
wenzelm@2185
   275
in
wenzelm@3975
   276
  fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
wenzelm@3967
   277
    (check_stale sg1; check_stale sg2; id1 = id2);
wenzelm@2185
   278
wenzelm@3975
   279
  fun subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   280
    eq_sg (sg1, sg2) orelse subset_stamp (s1, s2);
paulson@2180
   281
wenzelm@3975
   282
  fun fast_subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   283
    eq_sg (sg1, sg2) orelse fast_sub (s1, s2);
wenzelm@2185
   284
end;
wenzelm@2185
   285
wenzelm@402
   286
wenzelm@9031
   287
fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
wenzelm@9031
   288
wenzelm@2185
   289
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   290
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   291
  same version of it*)
wenzelm@3975
   292
fun same_sg (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   293
  eq_sg (sg1, sg2) orelse eq_set_string (pairself (map (op !)) (s1, s2));
wenzelm@2185
   294
wenzelm@2185
   295
(*test for drafts*)
wenzelm@14645
   296
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) =
wenzelm@14645
   297
  name = "" orelse ord name = ord "#";
wenzelm@14645
   298
wenzelm@14645
   299
wenzelm@14645
   300
(* syntax *)
wenzelm@14645
   301
wenzelm@14645
   302
fun map_syntax f (Syn (syntax, trfuns)) = Syn (f syntax, trfuns);
wenzelm@14645
   303
wenzelm@14645
   304
fun make_syntax sg (Syn (syntax, (atrs, trs, tr's, atr's))) =
wenzelm@14645
   305
  let
wenzelm@14645
   306
    fun apply (s, (f, _: stamp)) = (s, f sg);
wenzelm@14645
   307
    fun prep tab = map apply (Symtab.dest tab);
wenzelm@14645
   308
    fun prep' tab = map apply (Symtab.dest_multi tab);
wenzelm@14645
   309
  in syntax |> Syntax.extend_trfuns (prep atrs, prep trs, prep' tr's, prep' atr's) end;
wenzelm@14645
   310
wenzelm@14645
   311
fun syn_of (sg as Sg ({syn, ...}, _)) = make_syntax sg syn;
wenzelm@14645
   312
wenzelm@14645
   313
wenzelm@14645
   314
(* advanced translation functions *)
wenzelm@14645
   315
wenzelm@14645
   316
fun extend_trfuns (atrs, trs, tr's, atr's)
wenzelm@14645
   317
    (Syn (syn, (parse_ast_trtab, parse_trtab, print_trtab, print_ast_trtab))) =
wenzelm@14645
   318
  Syn (syn, (Syntax.extend_trtab parse_ast_trtab atrs "parse ast translation",
wenzelm@14645
   319
    Syntax.extend_trtab parse_trtab trs "parse translation",
wenzelm@14645
   320
    Syntax.extend_tr'tab print_trtab tr's,
wenzelm@14645
   321
    Syntax.extend_tr'tab print_ast_trtab atr's));
wenzelm@14645
   322
wenzelm@14645
   323
fun merge_trfuns
wenzelm@14645
   324
    (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
wenzelm@14645
   325
    (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
wenzelm@14645
   326
  (Syntax.merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
wenzelm@14645
   327
    Syntax.merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
wenzelm@14645
   328
    Syntax.merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@14645
   329
    Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
wenzelm@386
   330
clasohm@0
   331
wenzelm@4568
   332
(* classes and sorts *)
wenzelm@4568
   333
wenzelm@7640
   334
val classes = Type.classes o tsig_of;
wenzelm@4844
   335
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   336
val subsort = Type.subsort o tsig_of;
wenzelm@7640
   337
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   338
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@14784
   339
val universal_witness = Type.universal_witness o tsig_of;
wenzelm@14784
   340
val typ_instance = Type.typ_instance o tsig_of;
wenzelm@4568
   341
wenzelm@4256
   342
wenzelm@14828
   343
wenzelm@4256
   344
(** signature data **)
wenzelm@4256
   345
wenzelm@4256
   346
(* errors *)
wenzelm@4256
   347
wenzelm@4261
   348
fun of_theory sg = "\nof theory " ^ str_of_sg sg;
wenzelm@4256
   349
wenzelm@4998
   350
fun err_inconsistent kinds =
wenzelm@4998
   351
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
wenzelm@4998
   352
wenzelm@6961
   353
fun err_method name kind e =
wenzelm@6961
   354
  (writeln ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
wenzelm@4256
   355
wenzelm@4256
   356
fun err_dup_init sg kind =
wenzelm@4256
   357
  error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4256
   358
wenzelm@4256
   359
fun err_uninit sg kind =
wenzelm@14784
   360
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4256
   361
paulson@6040
   362
(*Trying to access theory data using get / put operations from a different
paulson@6040
   363
  instance of the TheoryDataFun result.  Typical cure: re-load all files*)
wenzelm@4998
   364
fun err_access sg kind =
wenzelm@4998
   365
  error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4998
   366
wenzelm@4256
   367
wenzelm@4256
   368
(* prepare data *)
wenzelm@4256
   369
wenzelm@4489
   370
val empty_data = Data Symtab.empty;
wenzelm@4256
   371
wenzelm@4256
   372
fun merge_data (Data tab1, Data tab2) =
wenzelm@4256
   373
  let
wenzelm@4998
   374
    val data1 = map snd (Symtab.dest tab1);
wenzelm@4998
   375
    val data2 = map snd (Symtab.dest tab2);
wenzelm@4256
   376
    val all_data = data1 @ data2;
wenzelm@4998
   377
    val kinds = gen_distinct Object.eq_kind (map fst all_data);
wenzelm@4256
   378
wenzelm@4256
   379
   fun entry data kind =
wenzelm@4998
   380
     (case gen_assoc Object.eq_kind (data, kind) of
skalberg@15531
   381
       NONE => []
skalberg@15531
   382
     | SOME x => [(kind, x)]);
wenzelm@4256
   383
wenzelm@12310
   384
    fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
wenzelm@6961
   385
          (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
wenzelm@12310
   386
      | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
wenzelm@6961
   387
          (kind, (mrg (e1, e2)
wenzelm@6961
   388
            handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
wenzelm@4256
   389
      | merge_entries _ = sys_error "merge_entries";
wenzelm@4256
   390
wenzelm@4256
   391
    val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
wenzelm@4998
   392
    val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
wenzelm@4998
   393
  in
wenzelm@4998
   394
    Data (Symtab.make data_idx)
wenzelm@4998
   395
      handle Symtab.DUPS dups => err_inconsistent dups
wenzelm@4998
   396
  end;
wenzelm@4256
   397
wenzelm@4256
   398
fun prep_ext_data data = merge_data (data, empty_data);
wenzelm@4256
   399
wenzelm@12310
   400
fun init_data_sg sg (Data tab) kind e cp ext mrg prt =
wenzelm@4998
   401
  let val name = Object.name_of_kind kind in
wenzelm@12310
   402
    Data (Symtab.update_new ((name, (kind, (e, (cp, ext, mrg, prt)))), tab))
wenzelm@4998
   403
      handle Symtab.DUP _ => err_dup_init sg name
wenzelm@4998
   404
  end;
wenzelm@4256
   405
wenzelm@4256
   406
wenzelm@4256
   407
(* access data *)
wenzelm@4256
   408
wenzelm@4256
   409
fun data_kinds (Data tab) = map fst (Symtab.dest tab);
wenzelm@4256
   410
wenzelm@4256
   411
fun lookup_data sg tab kind =
wenzelm@4998
   412
  let val name = Object.name_of_kind kind in
wenzelm@4998
   413
    (case Symtab.lookup (tab, name) of
skalberg@15531
   414
      SOME (k, x) =>
wenzelm@4998
   415
        if Object.eq_kind (kind, k) then x
wenzelm@4998
   416
        else err_access sg name
skalberg@15531
   417
    | NONE => err_uninit sg name)
wenzelm@4998
   418
  end;
wenzelm@4256
   419
wenzelm@4998
   420
fun get_data kind f (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@4998
   421
  let val x = fst (lookup_data sg tab kind)
wenzelm@4998
   422
  in f x handle Match => Object.kind_error kind end;
wenzelm@4998
   423
wenzelm@4998
   424
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@12310
   425
  let val (e, (_, _, _, prt)) = lookup_data sg tab kind
wenzelm@6961
   426
  in prt sg e handle exn => err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn end;
wenzelm@4256
   427
wenzelm@4998
   428
fun put_data_sg sg (Data tab) kind f x =
wenzelm@4998
   429
  Data (Symtab.update ((Object.name_of_kind kind,
wenzelm@4998
   430
    (kind, (f x, snd (lookup_data sg tab kind)))), tab));
wenzelm@4256
   431
wenzelm@4256
   432
wenzelm@4256
   433
wenzelm@4256
   434
(** build signatures **)
wenzelm@3967
   435
wenzelm@3967
   436
fun ext_stamps stamps (id as ref name) =
wenzelm@3967
   437
  let val stmps = (case stamps of ref "#" :: ss => ss | ss => ss) in
wenzelm@3967
   438
    if exists (equal name o !) stmps then
wenzelm@3967
   439
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@3967
   440
    else id :: stmps
wenzelm@3967
   441
  end;
wenzelm@3967
   442
wenzelm@3967
   443
fun create_sign self stamps name (syn, tsig, ctab, (path, spaces), data) =
wenzelm@3967
   444
  let
wenzelm@3967
   445
    val id = ref name;
wenzelm@3967
   446
    val sign =
wenzelm@3967
   447
      make_sign (id, self, tsig, ctab, syn, path, spaces, data, ext_stamps stamps id);
wenzelm@3967
   448
  in
wenzelm@3967
   449
    (case self of
skalberg@15531
   450
      SgRef (SOME r) => r := sign
wenzelm@3967
   451
    | _ => sys_error "Sign.create_sign");
wenzelm@3967
   452
    sign
wenzelm@3967
   453
  end;
wenzelm@3967
   454
wenzelm@3975
   455
fun extend_sign keep extfun name decls
wenzelm@14784
   456
    (sg as Sg ({id = _, stamps, syn}, {self, tsig, consts, path, spaces, data})) =
wenzelm@3967
   457
  let
wenzelm@3967
   458
    val _ = check_stale sg;
wenzelm@3967
   459
    val (self', data') =
wenzelm@3975
   460
      if is_draft sg andalso keep then (self, data)
skalberg@15531
   461
      else (SgRef (SOME (ref sg)), prep_ext_data data);
wenzelm@3967
   462
  in
wenzelm@3967
   463
    create_sign self' stamps name
wenzelm@14784
   464
      (extfun sg (syn, tsig, consts, (path, spaces), data') decls)
wenzelm@3967
   465
  end;
wenzelm@3967
   466
wenzelm@3967
   467
wenzelm@3791
   468
wenzelm@3791
   469
(** name spaces **)
wenzelm@3791
   470
wenzelm@3791
   471
(* kinds *)
wenzelm@3791
   472
wenzelm@3791
   473
val classK = "class";
wenzelm@3791
   474
val typeK = "type";
wenzelm@3791
   475
val constK = "const";
wenzelm@3791
   476
wenzelm@3791
   477
wenzelm@8725
   478
(* declare and retrieve names *)
wenzelm@3791
   479
wenzelm@3791
   480
fun space_of spaces kind =
wenzelm@3791
   481
  if_none (assoc (spaces, kind)) NameSpace.empty;
wenzelm@3791
   482
wenzelm@3791
   483
(*input and output of qualified names*)
wenzelm@3995
   484
fun intrn spaces kind = NameSpace.intern (space_of spaces kind);
wenzelm@3995
   485
fun extrn spaces kind = NameSpace.extern (space_of spaces kind);
wenzelm@5175
   486
fun cond_extrn spaces kind = NameSpace.cond_extern (space_of spaces kind);
wenzelm@6845
   487
fun cond_extrn_table spaces kind tab = NameSpace.cond_extern_table (space_of spaces kind) tab;
wenzelm@3791
   488
wenzelm@8725
   489
(*add / hide names*)
wenzelm@8725
   490
fun change_space f spaces kind x = overwrite (spaces, (kind, f (space_of spaces kind, x)));
wenzelm@8730
   491
fun add_names x = change_space NameSpace.extend x;
wenzelm@12588
   492
fun hide_names b x = change_space (NameSpace.hide b) x;
wenzelm@3791
   493
wenzelm@3810
   494
(*make full names*)
wenzelm@11501
   495
fun full _ "" = error "Attempt to declare empty name \"\""
skalberg@15531
   496
  | full NONE bname = bname
skalberg@15531
   497
  | full (SOME path) bname =
wenzelm@14784
   498
      if NameSpace.is_qualified bname then
wenzelm@14784
   499
        error ("Attempt to declare qualified name " ^ quote bname)
wenzelm@14688
   500
      else
wenzelm@14784
   501
        let val name = NameSpace.pack (path @ [bname]) in
wenzelm@14784
   502
          if Syntax.is_identifier bname then ()
wenzelm@14784
   503
          else warning ("Declared non-identifier " ^ quote name); name
wenzelm@14784
   504
        end;
wenzelm@3937
   505
wenzelm@3937
   506
(*base name*)
wenzelm@3937
   507
val base_name = NameSpace.base;
wenzelm@3791
   508
wenzelm@3791
   509
wenzelm@3791
   510
(* intern / extern names *)
wenzelm@3791
   511
wenzelm@3791
   512
local
wenzelm@3791
   513
  (*prepare mapping of names*)
wenzelm@3791
   514
  fun mapping f add_xs t =
wenzelm@3791
   515
    let
skalberg@15531
   516
      fun f' x = let val y = f x in if x = y then NONE else SOME (x, y) end;
wenzelm@3791
   517
      val table = mapfilter f' (add_xs (t, []));
wenzelm@3791
   518
      fun lookup x = if_none (assoc (table, x)) x;
wenzelm@3791
   519
    in lookup end;
wenzelm@3791
   520
wenzelm@3791
   521
  (*intern / extern typ*)
wenzelm@3791
   522
  fun trn_typ trn T =
wenzelm@3791
   523
    T |> map_typ
wenzelm@3791
   524
      (mapping (trn classK) add_typ_classes T)
wenzelm@3791
   525
      (mapping (trn typeK) add_typ_tycons T);
wenzelm@3791
   526
wenzelm@3791
   527
  (*intern / extern term*)
wenzelm@3791
   528
  fun trn_term trn t =
wenzelm@3791
   529
    t |> map_term
wenzelm@3791
   530
      (mapping (trn classK) add_term_classes t)
wenzelm@3791
   531
      (mapping (trn typeK) add_term_tycons t)
wenzelm@3791
   532
      (mapping (trn constK) add_term_consts t);
wenzelm@3791
   533
wenzelm@3975
   534
  val spaces_of = #spaces o rep_sg;
wenzelm@3791
   535
in
wenzelm@3791
   536
  fun intrn_class spaces = intrn spaces classK;
berghofe@14383
   537
  fun extrn_class spaces = cond_extrn spaces classK;
wenzelm@3937
   538
wenzelm@3791
   539
  val intrn_sort = map o intrn_class;
wenzelm@3791
   540
  val intrn_typ = trn_typ o intrn;
wenzelm@3937
   541
  val intrn_term = trn_term o intrn;
wenzelm@3937
   542
wenzelm@3937
   543
  val extrn_sort = map o extrn_class;
berghofe@14383
   544
  val extrn_typ = trn_typ o cond_extrn;
berghofe@14383
   545
  val extrn_term = trn_term o cond_extrn;
wenzelm@3791
   546
wenzelm@3805
   547
  fun intrn_tycons spaces T =
wenzelm@3805
   548
    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
wenzelm@3805
   549
wenzelm@3810
   550
  val intern = intrn o spaces_of;
wenzelm@3855
   551
  val extern = extrn o spaces_of;
wenzelm@5175
   552
  val cond_extern = cond_extrn o spaces_of;
wenzelm@6845
   553
  fun cond_extern_table sg = cond_extrn_table (spaces_of sg);
wenzelm@3937
   554
schirmer@14700
   555
  fun extern_typ (sg as Sg (_, {spaces, ...})) T =
schirmer@14700
   556
       if ! NameSpace.long_names then T else extrn_typ spaces T;
schirmer@14700
   557
wenzelm@3791
   558
  val intern_class = intrn_class o spaces_of;
wenzelm@3791
   559
  val intern_sort = intrn_sort o spaces_of;
wenzelm@3791
   560
  val intern_typ = intrn_typ o spaces_of;
wenzelm@3791
   561
  val intern_term = intrn_term o spaces_of;
wenzelm@3791
   562
wenzelm@3791
   563
  fun intern_tycon sg = intrn (spaces_of sg) typeK;
wenzelm@3791
   564
  fun intern_const sg = intrn (spaces_of sg) constK;
wenzelm@3937
   565
wenzelm@3791
   566
  val intern_tycons = intrn_tycons o spaces_of;
wenzelm@3791
   567
wenzelm@3975
   568
  val full_name = full o #path o rep_sg;
wenzelm@11501
   569
wenzelm@11501
   570
  fun full_name_path sg elems =
skalberg@15531
   571
    full (SOME (if_none (#path (rep_sg sg)) [] @ NameSpace.unpack elems));
wenzelm@11501
   572
wenzelm@3791
   573
end;
wenzelm@620
   574
wenzelm@402
   575
clasohm@0
   576
wenzelm@14645
   577
(** partial Pure signature **)
wenzelm@14645
   578
wenzelm@14645
   579
val pure_syn =
wenzelm@14645
   580
  Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
wenzelm@14645
   581
skalberg@15531
   582
val dummy_sg = make_sign (ref "", SgRef NONE, Type.empty_tsig,
skalberg@15531
   583
  Symtab.empty, pure_syn, SOME [], [], empty_data, []);
wenzelm@14645
   584
wenzelm@14645
   585
val pre_pure =
skalberg@15531
   586
  create_sign (SgRef (SOME (ref dummy_sg))) [] "#"
skalberg@15531
   587
    (pure_syn, Type.empty_tsig, Symtab.empty, (SOME [], []), empty_data);
wenzelm@14645
   588
wenzelm@14645
   589
val PureN = "Pure";
wenzelm@14645
   590
val CPureN = "CPure";
wenzelm@14645
   591
wenzelm@14645
   592
wenzelm@14645
   593
wenzelm@4249
   594
(** pretty printing of terms, types etc. **)
wenzelm@3937
   595
wenzelm@12068
   596
fun pretty_term' syn (sg as Sg ({stamps, ...}, {spaces, ...})) t =
wenzelm@3937
   597
  Syntax.pretty_term syn
wenzelm@14645
   598
    (exists (equal CPureN o !) stamps)
wenzelm@5175
   599
    (if ! NameSpace.long_names then t else extrn_term spaces t);
wenzelm@3937
   600
wenzelm@14974
   601
fun pretty_term sg t = pretty_term' (syn_of sg) sg t;
wenzelm@12068
   602
schirmer@14700
   603
fun pretty_typ sg T =
schirmer@14700
   604
  Syntax.pretty_typ (syn_of sg) (extern_typ sg T);
wenzelm@3937
   605
wenzelm@14645
   606
fun pretty_sort (sg as Sg (_, {spaces, ...})) S =
wenzelm@14645
   607
  Syntax.pretty_sort (syn_of sg)
wenzelm@5175
   608
    (if ! NameSpace.long_names then S else extrn_sort spaces S);
wenzelm@3937
   609
wenzelm@14828
   610
fun pretty_classrel sg cs = Pretty.block (flat
wenzelm@14828
   611
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort sg o single) cs)));
wenzelm@3937
   612
wenzelm@3937
   613
fun pretty_arity sg (t, Ss, S) =
wenzelm@3937
   614
  let
wenzelm@3937
   615
    val t' = cond_extern sg typeK t;
wenzelm@3937
   616
    val dom =
wenzelm@3937
   617
      if null Ss then []
wenzelm@3937
   618
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@3937
   619
  in
wenzelm@3937
   620
    Pretty.block
wenzelm@3937
   621
      ([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S])
wenzelm@3937
   622
  end;
wenzelm@3937
   623
wenzelm@14828
   624
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@14828
   625
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@14828
   626
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@14828
   627
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@14828
   628
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@3937
   629
wenzelm@3937
   630
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@3937
   631
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
wenzelm@3937
   632
wenzelm@14974
   633
fun pp sg = Pretty.pp (pretty_term sg, pretty_typ sg, pretty_sort sg,
wenzelm@14974
   634
  pretty_classrel sg, pretty_arity sg);
wenzelm@14828
   635
wenzelm@3937
   636
wenzelm@3937
   637
wenzelm@8898
   638
(** read sorts **)  (*exception ERROR*)
wenzelm@8898
   639
wenzelm@8898
   640
fun err_in_sort s =
wenzelm@8898
   641
  error ("The error(s) above occurred in sort " ^ quote s);
wenzelm@8898
   642
wenzelm@14645
   643
fun rd_sort sg syn tsig spaces str =
wenzelm@14645
   644
  let val S = intrn_sort spaces (Syntax.read_sort (make_syntax sg syn) str
wenzelm@14645
   645
    handle ERROR => err_in_sort str)
wenzelm@8898
   646
  in Type.cert_sort tsig S handle TYPE (msg, _, _) => (error_msg msg; err_in_sort str) end;
wenzelm@8898
   647
wenzelm@8898
   648
(*read and certify sort wrt a signature*)
wenzelm@14645
   649
fun read_sort (sg as Sg ({syn, ...}, {tsig, spaces, ...})) str =
wenzelm@14645
   650
  (check_stale sg; rd_sort sg syn tsig spaces str);
wenzelm@8898
   651
wenzelm@14645
   652
fun cert_sort _ _ tsig _ = Type.cert_sort tsig;
wenzelm@8898
   653
wenzelm@8898
   654
wenzelm@8898
   655
wenzelm@562
   656
(** read types **)  (*exception ERROR*)
wenzelm@562
   657
wenzelm@562
   658
fun err_in_type s =
wenzelm@562
   659
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   660
wenzelm@4227
   661
fun rd_raw_typ syn tsig spaces def_sort str =
wenzelm@14784
   662
  intrn_tycons spaces (Syntax.read_typ syn
wenzelm@14784
   663
    (TypeInfer.get_sort tsig def_sort (intrn_sort spaces)) (intrn_sort spaces) str
wenzelm@3791
   664
      handle ERROR => err_in_type str);
wenzelm@4227
   665
wenzelm@12068
   666
fun read_raw_typ' syn (sg as Sg (_, {tsig, spaces, ...}), def_sort) str =
wenzelm@4227
   667
  (check_stale sg; rd_raw_typ syn tsig spaces def_sort str);
wenzelm@4227
   668
wenzelm@12068
   669
fun read_raw_typ (sg, def_sort) = read_raw_typ' (syn_of sg) (sg, def_sort);
wenzelm@12068
   670
wenzelm@562
   671
(*read and certify typ wrt a signature*)
wenzelm@9504
   672
local
wenzelm@12068
   673
  fun read_typ_aux rd cert (sg, def_sort) str =
wenzelm@14993
   674
    cert (tsig_of sg) (rd (sg, def_sort) str)
wenzelm@14993
   675
      handle TYPE (msg, _, _) => (error_msg msg; err_in_type str);
wenzelm@9504
   676
in
wenzelm@14784
   677
  val read_typ          = read_typ_aux read_raw_typ Type.cert_typ;
wenzelm@14784
   678
  val read_typ_raw      = read_typ_aux read_raw_typ Type.cert_typ_raw;
wenzelm@14784
   679
  fun read_typ' syn     = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
wenzelm@14784
   680
  fun read_typ_raw' syn = read_typ_aux (read_raw_typ' syn) Type.cert_typ_raw;
wenzelm@9504
   681
end;
wenzelm@562
   682
wenzelm@562
   683
wenzelm@562
   684
wenzelm@8898
   685
(** certify classes, sorts, types and terms **)   (*exception TYPE*)
wenzelm@251
   686
wenzelm@8898
   687
val certify_class = Type.cert_class o tsig_of;
wenzelm@8898
   688
val certify_sort = Type.cert_sort o tsig_of;
wenzelm@14993
   689
val certify_typ = Type.cert_typ o tsig_of;
wenzelm@14993
   690
val certify_typ_raw = Type.cert_typ_raw o tsig_of;
wenzelm@10443
   691
wenzelm@11720
   692
fun certify_tyname sg c =
wenzelm@14784
   693
  if is_some (Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c)) then c
wenzelm@14784
   694
  else raise TYPE ("Undeclared type constructor: " ^ quote c, [], []);
wenzelm@11720
   695
wenzelm@10443
   696
fun certify_const sg c =
wenzelm@14784
   697
  if is_some (const_type sg c) then c
wenzelm@14784
   698
  else raise TYPE ("Undeclared constant: " ^ quote c, [], []);
wenzelm@251
   699
wenzelm@4961
   700
wenzelm@4961
   701
(* certify_term *)
wenzelm@4961
   702
wenzelm@14987
   703
local
nipkow@1494
   704
wenzelm@4961
   705
(*compute and check type of the term*)
wenzelm@14828
   706
fun type_check pp sg tm =
wenzelm@4961
   707
  let
wenzelm@4961
   708
    fun err_appl why bs t T u U =
wenzelm@4961
   709
      let
wenzelm@10404
   710
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   711
        val t' = subst_bounds (xs, t);
wenzelm@4961
   712
        val u' = subst_bounds (xs, u);
wenzelm@14828
   713
        val text = cat_lines
wenzelm@14828
   714
          (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
wenzelm@4961
   715
      in raise TYPE (text, [T, U], [t', u']) end;
wenzelm@4961
   716
wenzelm@4961
   717
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   718
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   719
      | typ_of (_, Var (_, T)) = T
wenzelm@4961
   720
      | typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ =>
wenzelm@4961
   721
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   722
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   723
      | typ_of (bs, t $ u) =
wenzelm@4961
   724
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   725
            (case T of
wenzelm@4961
   726
              Type ("fun", [T1, T2]) =>
wenzelm@14828
   727
                if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
wenzelm@14828
   728
            | _ => err_appl "Operator not of function type" bs t T u U)
wenzelm@4961
   729
          end;
wenzelm@4961
   730
wenzelm@4961
   731
  in typ_of ([], tm) end;
clasohm@0
   732
wenzelm@14987
   733
(*check for duplicate occurrences of TFree/TVar with distinct sorts*)
wenzelm@14987
   734
fun nodup_tvars (env, Type (_, Ts)) = nodup_tvars_list (env, Ts)
wenzelm@14987
   735
  | nodup_tvars (env as (tfrees, tvars), T as TFree (v as (a, S))) =
wenzelm@14987
   736
      (case assoc_string (tfrees, a) of
skalberg@15531
   737
        SOME S' =>
wenzelm@14987
   738
          if S = S' then env
wenzelm@14987
   739
          else raise TYPE ("Type variable " ^ quote a ^
wenzelm@14987
   740
            " has two distinct sorts", [TFree (a, S'), T], [])
skalberg@15531
   741
      | NONE => (v :: tfrees, tvars))
wenzelm@14987
   742
  | nodup_tvars (env as (tfrees, tvars), T as TVar (v as (a, S))) =
wenzelm@14987
   743
      (case assoc_string_int (tvars, a) of
skalberg@15531
   744
        SOME S' =>
wenzelm@14987
   745
          if S = S' then env
wenzelm@14987
   746
          else raise TYPE ("Type variable " ^ quote (Syntax.string_of_vname a) ^
wenzelm@14987
   747
            " has two distinct sorts", [TVar (a, S'), T], [])
skalberg@15531
   748
      | NONE => (tfrees, v :: tvars))
wenzelm@14987
   749
(*equivalent to foldl nodup_tvars_list, but 3X faster under Poly/ML*)
wenzelm@14987
   750
and nodup_tvars_list (env, []) = env
wenzelm@14987
   751
  | nodup_tvars_list (env, T :: Ts) = nodup_tvars_list (nodup_tvars (env, T), Ts);
wenzelm@14987
   752
wenzelm@14987
   753
in
wenzelm@14987
   754
wenzelm@14987
   755
(*check for duplicate occurrences of Free/Var with distinct types*)
wenzelm@14987
   756
fun nodup_vars tm =
wenzelm@14987
   757
  let
wenzelm@14987
   758
    fun nodups (envs as (env as (frees, vars), envT)) tm =
wenzelm@14987
   759
      (case tm of
wenzelm@14987
   760
        Const (c, T) => (env, nodup_tvars (envT, T))
wenzelm@14987
   761
      | Free (v as (a, T)) =>
wenzelm@14987
   762
          (case assoc_string (frees, a) of
skalberg@15531
   763
            SOME T' =>
wenzelm@14987
   764
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@14987
   765
              else raise TYPE ("Variable " ^ quote a ^
wenzelm@14987
   766
                " has two distinct types", [T', T], [])
skalberg@15531
   767
          | NONE => ((v :: frees, vars), nodup_tvars (envT, T)))
wenzelm@14987
   768
      | Var (v as (ixn, T)) =>
wenzelm@14987
   769
          (case assoc_string_int (vars, ixn) of
skalberg@15531
   770
            SOME T' =>
wenzelm@14987
   771
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@14987
   772
              else raise TYPE ("Variable " ^ quote (Syntax.string_of_vname ixn) ^
wenzelm@14987
   773
                " has two distinct types", [T', T], [])
skalberg@15531
   774
          | NONE => ((frees, v :: vars), nodup_tvars (envT, T)))
wenzelm@14987
   775
      | Bound _ => envs
wenzelm@14987
   776
      | Abs (_, T, t) => nodups (env, nodup_tvars (envT, T)) t
wenzelm@14987
   777
      | s $ t => nodups (nodups envs s) t)
wenzelm@14987
   778
  in nodups (([], []), ([], [])) tm; tm end;
wenzelm@14987
   779
wenzelm@14828
   780
fun certify_term pp sg tm =
wenzelm@251
   781
  let
wenzelm@3969
   782
    val _ = check_stale sg;
wenzelm@14784
   783
wenzelm@14993
   784
    val tm' = map_term_types (Type.cert_typ (tsig_of sg)) tm;
wenzelm@14784
   785
    val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
wenzelm@14784
   786
wenzelm@14784
   787
    fun err msg = raise TYPE (msg, [], [tm']);
wenzelm@3969
   788
wenzelm@14828
   789
    fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
wenzelm@169
   790
wenzelm@14784
   791
    fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
wenzelm@14784
   792
      | check_atoms (Abs (_, _, t)) = check_atoms t
wenzelm@14784
   793
      | check_atoms (Const (a, T)) =
wenzelm@14784
   794
          (case const_type sg a of
skalberg@15531
   795
            NONE => err ("Undeclared constant " ^ show_const a T)
skalberg@15531
   796
          | SOME U =>
wenzelm@14784
   797
              if typ_instance sg (T, U) then ()
wenzelm@14784
   798
              else err ("Illegal type for constant " ^ show_const a T))
wenzelm@14784
   799
      | check_atoms (Var ((x, i), _)) =
wenzelm@14784
   800
          if i < 0 then err ("Malformed variable: " ^ quote x) else ()
wenzelm@14784
   801
      | check_atoms _ = ();
wenzelm@14784
   802
  in
wenzelm@14784
   803
    check_atoms tm';
wenzelm@14784
   804
    nodup_vars tm';
wenzelm@14993
   805
    (tm', type_check pp sg tm', maxidx_of_term tm')
wenzelm@14784
   806
  end;
wenzelm@251
   807
wenzelm@14987
   808
end;
wenzelm@14987
   809
wenzelm@251
   810
wenzelm@251
   811
wenzelm@14828
   812
(** instantiation **)
wenzelm@14828
   813
wenzelm@14828
   814
fun inst_typ_tvars sg = Type.inst_typ_tvars (pp sg) (tsig_of sg);
wenzelm@14828
   815
fun inst_term_tvars sg = Type.inst_term_tvars (pp sg) (tsig_of sg);
wenzelm@14828
   816
wenzelm@14828
   817
wenzelm@14828
   818
wenzelm@583
   819
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   820
wenzelm@2979
   821
(*
wenzelm@2979
   822
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   823
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   824
  used: list of already used type variables
wenzelm@2979
   825
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   826
wenzelm@4249
   827
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   828
  typs: expected types
wenzelm@2979
   829
*)
wenzelm@2979
   830
wenzelm@14828
   831
fun infer_types_simult pp sg def_type def_sort used freeze args =
wenzelm@251
   832
  let
wenzelm@3975
   833
    val tsig = tsig_of sg;
wenzelm@4249
   834
wenzelm@4249
   835
    val termss = foldr multiply (map fst args, [[]]);
wenzelm@4249
   836
    val typs =
wenzelm@4249
   837
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   838
wenzelm@14828
   839
    fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) tsig
wenzelm@14828
   840
        (const_type sg) def_type def_sort (intern_const sg) (intern_tycons sg)
wenzelm@14828
   841
        (intern_sort sg) used freeze typs ts)
wenzelm@4249
   842
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   843
wenzelm@4249
   844
    val err_results = map infer termss;
wenzelm@4249
   845
    val errs = mapfilter get_error err_results;
wenzelm@4249
   846
    val results = mapfilter get_ok err_results;
wenzelm@4249
   847
wenzelm@14645
   848
    val ambiguity = length termss;
wenzelm@14645
   849
wenzelm@4249
   850
    fun ambig_msg () =
wenzelm@4249
   851
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   852
      then
wenzelm@4249
   853
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   854
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   855
      else ();
wenzelm@4249
   856
  in
wenzelm@4249
   857
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   858
    else if length results = 1 then
wenzelm@4249
   859
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   860
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   861
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   862
      else (); hd results)
wenzelm@4249
   863
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
wenzelm@14828
   864
      cat_lines (map (Pretty.string_of_term pp) (flat (map fst results)))))
wenzelm@4249
   865
  end;
clasohm@623
   866
wenzelm@4249
   867
wenzelm@14828
   868
fun infer_types pp sg def_type def_sort used freeze tsT =
wenzelm@14828
   869
  apfst hd (infer_types_simult pp sg def_type def_sort used freeze [tsT]);
wenzelm@251
   870
wenzelm@251
   871
wenzelm@8607
   872
(** read_def_terms **)
wenzelm@8607
   873
wenzelm@8607
   874
(*read terms, infer types*)
wenzelm@14905
   875
fun read_def_terms' pp is_logtype syn (sign, types, sorts) used freeze sTs =
wenzelm@8607
   876
  let
wenzelm@8607
   877
    fun read (s, T) =
wenzelm@8607
   878
      let val T' = certify_typ sign T handle TYPE (msg, _, _) => error msg
wenzelm@14905
   879
      in (Syntax.read is_logtype syn T' s, T') end;
wenzelm@8607
   880
    val tsTs = map read sTs;
wenzelm@14828
   881
  in infer_types_simult pp sign types sorts used freeze tsTs end;
wenzelm@8607
   882
wenzelm@14645
   883
fun read_def_terms (sign, types, sorts) =
wenzelm@14905
   884
  read_def_terms' (pp sign) (is_logtype sign) (syn_of sign) (sign, types, sorts);
wenzelm@12068
   885
wenzelm@8802
   886
fun simple_read_term sign T s =
skalberg@15531
   887
  (read_def_terms (sign, K NONE, K NONE) [] true [(s, T)]
wenzelm@8802
   888
    handle ERROR => error ("The error(s) above occurred for " ^ s)) |> #1 |> hd;
wenzelm@8802
   889
wenzelm@8607
   890
wenzelm@2979
   891
clasohm@623
   892
(** extend signature **)    (*exception ERROR*)
clasohm@623
   893
wenzelm@620
   894
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   895
wenzelm@3791
   896
fun decls_of path name_of mfixs =
wenzelm@3810
   897
  map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs;
wenzelm@3791
   898
wenzelm@14645
   899
fun no_read _ _ _ _ decl = decl;
wenzelm@386
   900
wenzelm@386
   901
wenzelm@386
   902
(* add default sort *)
wenzelm@386
   903
wenzelm@14645
   904
fun ext_defS prep_sort sg (syn, tsig, ctab, (path, spaces), data) S =
wenzelm@14784
   905
  (syn, Type.set_defsort (prep_sort sg syn tsig spaces S) tsig,
wenzelm@14645
   906
    ctab, (path, spaces), data);
wenzelm@8898
   907
wenzelm@14645
   908
fun ext_defsort arg = ext_defS rd_sort arg;
paulson@8927
   909
fun ext_defsort_i arg = ext_defS cert_sort arg;
wenzelm@386
   910
wenzelm@386
   911
wenzelm@386
   912
(* add type constructors *)
wenzelm@386
   913
wenzelm@14905
   914
fun ext_types sg (syn, tsig, ctab, (path, spaces), data) types =
wenzelm@14856
   915
  let
wenzelm@14856
   916
    val decls = decls_of path Syntax.type_name types;
wenzelm@14905
   917
    val syn' = map_syntax (Syntax.extend_type_gram types) syn;
wenzelm@14856
   918
    val tsig' = Type.add_types decls tsig;
wenzelm@14905
   919
  in (syn', tsig', ctab, (path, add_names spaces typeK (map fst decls)), data) end;
wenzelm@386
   920
wenzelm@386
   921
wenzelm@386
   922
(* add type abbreviations *)
wenzelm@386
   923
wenzelm@14645
   924
fun read_abbr sg syn tsig spaces (t, vs, rhs_src) =
skalberg@15531
   925
  (t, vs, rd_raw_typ (make_syntax sg syn) tsig spaces (K NONE) rhs_src)
wenzelm@386
   926
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   927
wenzelm@14645
   928
fun ext_abbrs rd_abbr sg (syn, tsig, ctab, (path, spaces), data) abbrs =
wenzelm@386
   929
  let
wenzelm@386
   930
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@14905
   931
    val syn' = syn |> map_syntax (Syntax.extend_type_gram (map mfix_of abbrs));
wenzelm@386
   932
wenzelm@3791
   933
    val abbrs' =
wenzelm@3791
   934
      map (fn (t, vs, rhs, mx) =>
wenzelm@3810
   935
        (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
wenzelm@3805
   936
    val spaces' = add_names spaces typeK (map #1 abbrs');
wenzelm@14645
   937
    val decls = map (rd_abbr sg syn' tsig spaces') abbrs';
wenzelm@14905
   938
  in (syn', Type.add_abbrs decls tsig, ctab, (path, spaces'), data) end;
wenzelm@386
   939
wenzelm@3791
   940
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   941
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   942
wenzelm@386
   943
wenzelm@14784
   944
(* add nonterminals *)
wenzelm@14784
   945
wenzelm@14784
   946
fun ext_nonterminals _ (syn, tsig, ctab, (path, spaces), data) bnames =
wenzelm@14784
   947
  let val names = map (full path) bnames in
wenzelm@14784
   948
    (map_syntax (Syntax.extend_consts names) syn,
wenzelm@14784
   949
      Type.add_nonterminals names tsig, ctab,
wenzelm@14784
   950
      (path, add_names spaces typeK names), data)
wenzelm@14784
   951
  end;
wenzelm@14784
   952
wenzelm@14784
   953
wenzelm@386
   954
(* add type arities *)
wenzelm@386
   955
wenzelm@14645
   956
fun ext_ars int prep_sort sg (syn, tsig, ctab, (path, spaces), data) arities =
wenzelm@386
   957
  let
wenzelm@14645
   958
    val prepS = prep_sort sg syn tsig spaces;
wenzelm@8898
   959
    fun prep_arity (c, Ss, S) =
wenzelm@8898
   960
      (if int then intrn spaces typeK c else c, map prepS Ss, prepS S);
wenzelm@14828
   961
    val tsig' = Type.add_arities (pp sg) (map prep_arity arities) tsig;
wenzelm@14856
   962
  in (syn, tsig', ctab, (path, spaces), data) end;
wenzelm@386
   963
wenzelm@14645
   964
fun ext_arities arg = ext_ars true rd_sort arg;
paulson@8927
   965
fun ext_arities_i arg = ext_ars false cert_sort arg;
wenzelm@8898
   966
wenzelm@386
   967
wenzelm@386
   968
(* add term constants and syntax *)
wenzelm@386
   969
wenzelm@3805
   970
fun const_name path c mx =
wenzelm@3810
   971
  full path (Syntax.const_name c mx);
wenzelm@3805
   972
wenzelm@386
   973
fun err_in_const c =
wenzelm@386
   974
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   975
wenzelm@386
   976
fun err_dup_consts cs =
wenzelm@386
   977
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   978
wenzelm@251
   979
wenzelm@14645
   980
fun read_const sg syn tsig (path, spaces) (c, ty_src, mx) =
skalberg@15531
   981
  (c, rd_raw_typ (make_syntax sg syn) tsig spaces (K NONE) ty_src, mx)
wenzelm@3805
   982
    handle ERROR => err_in_const (const_name path c mx);
wenzelm@386
   983
wenzelm@14645
   984
fun ext_cnsts rd_const syn_only prmode sg (syn, tsig, ctab, (path, spaces), data)
wenzelm@14645
   985
    raw_consts =
wenzelm@386
   986
  let
wenzelm@14784
   987
    val prep_typ = compress_type o Type.varifyT o Type.no_tvars o
wenzelm@14993
   988
     (if syn_only then Type.cert_typ_syntax tsig else Term.no_dummyT o Type.cert_typ tsig);
wenzelm@14784
   989
    fun prep_const (c, ty, mx) = (c, prep_typ ty, mx) handle TYPE (msg, _, _) =>
wenzelm@14784
   990
      (error_msg msg; err_in_const (const_name path c mx));
wenzelm@386
   991
wenzelm@14645
   992
    val consts = map (prep_const o rd_const sg syn tsig (path, spaces)) raw_consts;
wenzelm@386
   993
    val decls =
wenzelm@386
   994
      if syn_only then []
wenzelm@3791
   995
      else decls_of path Syntax.const_name consts;
wenzelm@386
   996
  in
wenzelm@14905
   997
    (map_syntax (Syntax.extend_const_gram (is_logtype sg) prmode consts) syn, tsig,
wenzelm@14784
   998
      Symtab.extend (ctab, map (fn (c, T) => (c, (T, stamp ()))) decls)
wenzelm@3791
   999
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@3967
  1000
      (path, add_names spaces constK (map fst decls)), data)
wenzelm@386
  1001
  end;
wenzelm@386
  1002
wenzelm@14905
  1003
fun ext_consts_i x = ext_cnsts no_read false Syntax.default_mode x;
wenzelm@14905
  1004
fun ext_consts x = ext_cnsts read_const false Syntax.default_mode x;
wenzelm@14905
  1005
fun ext_syntax_i x = ext_cnsts no_read true Syntax.default_mode x;
wenzelm@14905
  1006
fun ext_syntax x = ext_cnsts read_const true Syntax.default_mode x;
wenzelm@14645
  1007
fun ext_modesyntax_i x y (prmode, consts) = ext_cnsts no_read true prmode x y consts;
wenzelm@14645
  1008
fun ext_modesyntax x y (prmode, consts) = ext_cnsts read_const true prmode x y consts;
wenzelm@386
  1009
wenzelm@386
  1010
wenzelm@386
  1011
(* add type classes *)
wenzelm@386
  1012
wenzelm@386
  1013
fun const_of_class c = c ^ "_class";
wenzelm@386
  1014
wenzelm@386
  1015
fun class_of_const c_class =
wenzelm@386
  1016
  let
wenzelm@3791
  1017
    val c = implode (take (size c_class - size "_class", explode c_class));
wenzelm@386
  1018
  in
wenzelm@386
  1019
    if const_of_class c = c_class then c
wenzelm@3791
  1020
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
  1021
  end;
wenzelm@386
  1022
wenzelm@386
  1023
wenzelm@14645
  1024
fun ext_classes int sg (syn, tsig, ctab, (path, spaces), data) classes =
wenzelm@386
  1025
  let
wenzelm@562
  1026
    val names = map fst classes;
wenzelm@386
  1027
    val consts =
wenzelm@386
  1028
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
  1029
wenzelm@3810
  1030
    val full_names = map (full path) names;
wenzelm@3791
  1031
    val spaces' = add_names spaces classK full_names;
wenzelm@3791
  1032
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
  1033
    val classes' =
wenzelm@3791
  1034
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
  1035
  in
wenzelm@14645
  1036
    ext_consts_i sg
wenzelm@14645
  1037
      (map_syntax (Syntax.extend_consts names) syn,
wenzelm@14828
  1038
        Type.add_classes (pp sg) classes' tsig, ctab, (path, spaces'), data)
wenzelm@3791
  1039
    consts
wenzelm@386
  1040
  end;
wenzelm@386
  1041
wenzelm@386
  1042
wenzelm@2963
  1043
(* add to classrel *)
wenzelm@421
  1044
wenzelm@14828
  1045
fun ext_classrel int sg (syn, tsig, ctab, (path, spaces), data) pairs =
wenzelm@3791
  1046
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@14828
  1047
    (syn, Type.add_classrel (pp sg) (intrn pairs) tsig, ctab, (path, spaces), data)
wenzelm@3791
  1048
  end;
wenzelm@421
  1049
wenzelm@421
  1050
wenzelm@14645
  1051
(* add translation functions *)
wenzelm@14645
  1052
wenzelm@14645
  1053
fun ext_trfs ext non_typed sg (syn, tsig, ctab, names, data) (atrs, trs, tr's, atr's) =
wenzelm@14645
  1054
  let val syn' = syn |> ext (atrs, trs, map (apsnd non_typed) tr's, atr's)
wenzelm@14645
  1055
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1056
wenzelm@14645
  1057
fun ext_trfsT ext sg (syn, tsig, ctab, names, data) tr's =
wenzelm@14645
  1058
  let val syn' = syn |> ext ([], [], tr's, [])
wenzelm@14645
  1059
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1060
wenzelm@14645
  1061
fun ext_trfuns sg = ext_trfs (map_syntax o Syntax.extend_trfuns) Syntax.non_typed_tr' sg;
wenzelm@14645
  1062
fun ext_trfunsT sg = ext_trfsT (map_syntax o Syntax.extend_trfuns) sg;
wenzelm@14645
  1063
fun ext_advanced_trfuns sg = ext_trfs extend_trfuns Syntax.non_typed_tr'' sg;
wenzelm@14645
  1064
fun ext_advanced_trfunsT sg = ext_trfsT extend_trfuns sg;
wenzelm@14645
  1065
wenzelm@14645
  1066
wenzelm@14645
  1067
(* add token translation functions *)
wenzelm@14645
  1068
wenzelm@14645
  1069
fun ext_tokentrfuns sg (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1070
  (map_syntax (Syntax.extend_tokentrfuns args) syn, tsig, ctab, names, data);
wenzelm@14645
  1071
wenzelm@14645
  1072
wenzelm@4619
  1073
(* add translation rules *)
wenzelm@4619
  1074
wenzelm@14645
  1075
fun ext_trrules sg (syn, tsig, ctab, (path, spaces), data) args =
wenzelm@14905
  1076
  (syn |> map_syntax (Syntax.extend_trrules (is_logtype sg) (make_syntax sg syn)
wenzelm@14645
  1077
      (map (Syntax.map_trrule (fn (root, str) => (intrn spaces typeK root, str))) args)),
wenzelm@14645
  1078
    tsig, ctab, (path, spaces), data);
wenzelm@4619
  1079
wenzelm@14645
  1080
fun ext_trrules_i _ (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1081
  (syn |> map_syntax (Syntax.extend_trrules_i args), tsig, ctab, names, data);
wenzelm@3791
  1082
wenzelm@3791
  1083
wenzelm@3791
  1084
(* add to path *)
wenzelm@3791
  1085
wenzelm@14645
  1086
fun ext_path _ (syn, tsig, ctab, (path, spaces), data) elems =
wenzelm@3791
  1087
  let
wenzelm@3791
  1088
    val path' =
skalberg@15531
  1089
      if elems = "//" then NONE
skalberg@15531
  1090
      else if elems = "/" then SOME []
skalberg@15531
  1091
      else if elems = ".." andalso is_some path andalso path <> SOME [] then
skalberg@15531
  1092
        SOME (fst (split_last (the path)))
skalberg@15531
  1093
      else SOME (if_none path [] @ NameSpace.unpack elems);
wenzelm@3791
  1094
  in
wenzelm@3967
  1095
    (syn, tsig, ctab, (path', spaces), data)
wenzelm@10404
  1096
  end;
wenzelm@3791
  1097
wenzelm@3791
  1098
wenzelm@8725
  1099
(* change name space *)
wenzelm@8725
  1100
wenzelm@14645
  1101
fun ext_add_space _ (syn, tsig, ctab, (path, spaces), data) (kind, names) =
wenzelm@8725
  1102
  (syn, tsig, ctab, (path, add_names spaces kind names), data);
wenzelm@3791
  1103
wenzelm@14645
  1104
fun ext_hide_space _ (syn, tsig, ctab, (path, spaces), data) (b, (kind, xnames)) =
wenzelm@12588
  1105
  (syn, tsig, ctab, (path, hide_names b spaces kind (map (intrn spaces kind) xnames)), data);
wenzelm@8725
  1106
wenzelm@14645
  1107
fun ext_hide_space_i _ (syn, tsig, ctab, (path, spaces), data) (b, (kind, names)) =
wenzelm@12588
  1108
  (syn, tsig, ctab, (path, hide_names b spaces kind names), data);
wenzelm@386
  1109
wenzelm@386
  1110
wenzelm@3967
  1111
(* signature data *)
wenzelm@386
  1112
wenzelm@12310
  1113
fun ext_init_data sg (syn, tsig, ctab, names, data) (kind, (e, cp, ext, mrg, prt)) =
wenzelm@12310
  1114
  (syn, tsig, ctab, names, init_data_sg sg data kind e cp ext mrg prt);
wenzelm@386
  1115
wenzelm@4998
  1116
fun ext_put_data sg (syn, tsig, ctab, names, data) (kind, f, x) =
wenzelm@4998
  1117
  (syn, tsig, ctab, names, put_data_sg sg data kind f x);
wenzelm@386
  1118
wenzelm@386
  1119
wenzelm@12310
  1120
fun copy_data (k, (e, mths as (cp, _, _, _))) =
wenzelm@6961
  1121
  (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
wenzelm@6546
  1122
wenzelm@14784
  1123
fun copy (sg as Sg ({id = _, stamps, syn}, {self, tsig, consts, path, spaces, data})) =
wenzelm@6546
  1124
  let
wenzelm@6546
  1125
    val _ = check_stale sg;
skalberg@15531
  1126
    val self' = SgRef (SOME (ref sg));
wenzelm@6546
  1127
    val Data tab = data;
wenzelm@6546
  1128
    val data' = Data (Symtab.map copy_data tab);
wenzelm@14784
  1129
  in create_sign self' stamps "#" (syn, tsig, consts, (path, spaces), data') end;
wenzelm@6546
  1130
wenzelm@6546
  1131
wenzelm@386
  1132
(* the external interfaces *)
wenzelm@386
  1133
wenzelm@14645
  1134
val add_classes          = extend_sign true (ext_classes true) "#";
wenzelm@14645
  1135
val add_classes_i        = extend_sign true (ext_classes false) "#";
wenzelm@14645
  1136
val add_classrel         = extend_sign true (ext_classrel true) "#";
wenzelm@14645
  1137
val add_classrel_i       = extend_sign true (ext_classrel false) "#";
wenzelm@14645
  1138
val add_defsort          = extend_sign true ext_defsort "#";
wenzelm@14645
  1139
val add_defsort_i        = extend_sign true ext_defsort_i "#";
wenzelm@14645
  1140
val add_types            = extend_sign true ext_types "#";
wenzelm@14645
  1141
val add_nonterminals     = extend_sign true ext_nonterminals "#";
wenzelm@14645
  1142
val add_tyabbrs          = extend_sign true ext_tyabbrs "#";
wenzelm@14645
  1143
val add_tyabbrs_i        = extend_sign true ext_tyabbrs_i "#";
wenzelm@14645
  1144
val add_arities          = extend_sign true ext_arities "#";
wenzelm@14645
  1145
val add_arities_i        = extend_sign true ext_arities_i "#";
wenzelm@14645
  1146
val add_consts           = extend_sign true ext_consts "#";
wenzelm@14645
  1147
val add_consts_i         = extend_sign true ext_consts_i "#";
wenzelm@14645
  1148
val add_syntax           = extend_sign true ext_syntax "#";
wenzelm@14645
  1149
val add_syntax_i         = extend_sign true ext_syntax_i "#";
wenzelm@14645
  1150
val add_modesyntax       = extend_sign true ext_modesyntax "#";
wenzelm@14645
  1151
val add_modesyntax_i     = extend_sign true ext_modesyntax_i "#";
wenzelm@14645
  1152
val add_trfuns           = extend_sign true ext_trfuns "#";
wenzelm@14645
  1153
val add_trfunsT          = extend_sign true ext_trfunsT "#";
wenzelm@14645
  1154
val add_advanced_trfuns  = extend_sign true ext_advanced_trfuns "#";
wenzelm@14645
  1155
val add_advanced_trfunsT = extend_sign true ext_advanced_trfunsT "#";
wenzelm@14645
  1156
val add_tokentrfuns      = extend_sign true ext_tokentrfuns "#";
wenzelm@14645
  1157
val add_trrules          = extend_sign true ext_trrules "#";
wenzelm@14645
  1158
val add_trrules_i        = extend_sign true ext_trrules_i "#";
wenzelm@14645
  1159
val add_path             = extend_sign true ext_path "#";
wenzelm@14645
  1160
val add_space            = extend_sign true ext_add_space "#";
wenzelm@14645
  1161
val hide_space           = curry (extend_sign true ext_hide_space "#");
wenzelm@14645
  1162
val hide_space_i         = curry (extend_sign true ext_hide_space_i "#");
wenzelm@14645
  1163
val init_data            = extend_sign true ext_init_data "#";
wenzelm@14645
  1164
fun put_data k f x       = extend_sign true ext_put_data "#" (k, f, x);
wenzelm@14645
  1165
fun add_name name        = extend_sign true (K K) name ();
wenzelm@14645
  1166
val prep_ext             = extend_sign false (K K) "#" ();
wenzelm@386
  1167
wenzelm@386
  1168
wenzelm@3867
  1169
wenzelm@10404
  1170
(** merge signatures **)        (*exception TERM*)
wenzelm@3867
  1171
wenzelm@4228
  1172
(* merge_stamps *)
wenzelm@4228
  1173
wenzelm@4228
  1174
fun merge_stamps stamps1 stamps2 =
wenzelm@12296
  1175
  let val stamps = merge_lists' stamps1 stamps2 in
wenzelm@4228
  1176
    (case duplicates (map ! stamps) of
wenzelm@4228
  1177
      [] => stamps
wenzelm@4228
  1178
    | dups => raise TERM ("Attempt to merge different versions of theories "
wenzelm@4228
  1179
        ^ commas_quote dups, []))
wenzelm@4228
  1180
  end;
wenzelm@4228
  1181
wenzelm@4228
  1182
wenzelm@14645
  1183
(* trivial merge *)
wenzelm@3877
  1184
skalberg@15531
  1185
fun merge_refs (sgr1 as SgRef (SOME (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
skalberg@15531
  1186
        sgr2 as SgRef (SOME (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
wenzelm@3967
  1187
      if fast_subsig (sg2, sg1) then sgr1
wenzelm@3967
  1188
      else if fast_subsig (sg1, sg2) then sgr2
wenzelm@3967
  1189
      else if subsig (sg2, sg1) then sgr1
wenzelm@3967
  1190
      else if subsig (sg1, sg2) then sgr2
wenzelm@4228
  1191
      else (merge_stamps s1 s2; (*check for different versions*)
wenzelm@4228
  1192
        raise TERM ("Attempt to do non-trivial merge of signatures", []))
wenzelm@3967
  1193
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
  1194
wenzelm@4627
  1195
val merge = deref o merge_refs o pairself self_ref;
wenzelm@4627
  1196
wenzelm@3867
  1197
wenzelm@14645
  1198
(* non-trivial merge *)              (*exception TERM/ERROR*)
wenzelm@10932
  1199
wenzelm@6961
  1200
fun nontriv_merge (sg1, sg2) =
wenzelm@3967
  1201
  if subsig (sg2, sg1) then sg1
wenzelm@251
  1202
  else if subsig (sg1, sg2) then sg2
wenzelm@386
  1203
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@3967
  1204
    raise TERM ("Attempt to merge draft signatures", [])
wenzelm@10932
  1205
  else if exists_stamp PureN sg1 andalso exists_stamp CPureN sg2 orelse
wenzelm@10932
  1206
      exists_stamp CPureN sg1 andalso exists_stamp PureN sg2 then
wenzelm@10932
  1207
    raise TERM ("Cannot merge Pure and CPure signatures", [])
wenzelm@251
  1208
  else
wenzelm@251
  1209
    let
wenzelm@14645
  1210
      val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
wenzelm@14784
  1211
          {self = _, tsig = tsig1, consts = consts1,
wenzelm@14645
  1212
            path = _, spaces = spaces1, data = data1}) = sg1;
wenzelm@14645
  1213
      val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
wenzelm@14784
  1214
          {self = _, tsig = tsig2, consts = consts2,
wenzelm@14645
  1215
            path = _, spaces = spaces2, data = data2}) = sg2;
wenzelm@386
  1216
wenzelm@4228
  1217
      val stamps = merge_stamps stamps1 stamps2;
wenzelm@14645
  1218
      val syntax = Syntax.merge_syntaxes syntax1 syntax2;
wenzelm@14645
  1219
      val trfuns = merge_trfuns trfuns1 trfuns2;
wenzelm@14784
  1220
      val consts = Symtab.merge eq_snd (consts1, consts2)
wenzelm@14784
  1221
        handle Symtab.DUPS cs => err_dup_consts cs;
wenzelm@3791
  1222
skalberg@15531
  1223
      val path = SOME [];
wenzelm@3791
  1224
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
  1225
      val spaces =
wenzelm@3791
  1226
        kinds ~~
wenzelm@3791
  1227
          ListPair.map NameSpace.merge
wenzelm@3791
  1228
            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
wenzelm@3867
  1229
wenzelm@4256
  1230
      val data = merge_data (data1, data2);
wenzelm@3967
  1231
skalberg@15531
  1232
      val pre_sign = make_sign (ref "", SgRef (SOME (ref dummy_sg)),
wenzelm@14905
  1233
        tsig1, consts, Syn (syntax, trfuns), path, spaces, data, ref "#" :: stamps1);
wenzelm@14905
  1234
      val tsig = Type.merge_tsigs (pp pre_sign) (tsig1, tsig2);
wenzelm@14905
  1235
wenzelm@14905
  1236
      val self_ref = ref dummy_sg;
skalberg@15531
  1237
      val self = SgRef (SOME self_ref);
wenzelm@14905
  1238
      val sign = make_sign (ref "", self, tsig, consts, Syn (syntax, trfuns),
wenzelm@14645
  1239
        path, spaces, data, stamps);
wenzelm@14645
  1240
    in self_ref := sign; syn_of sign; sign end;
wenzelm@3791
  1241
clasohm@0
  1242
end;