src/Pure/sign.ML
author wenzelm
Thu Apr 22 10:57:12 2004 +0200 (2004-04-22)
changeset 14645 83776a9f0a9c
parent 14383 09aab4710789
child 14688 edb7dacde656
permissions -rw-r--r--
support for advanced translation functions;
wenzelm@19
     1
(*  Title:      Pure/sign.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@251
     3
    Author:     Lawrence C Paulson and Markus Wenzel
wenzelm@12785
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
clasohm@0
     5
wenzelm@251
     6
The abstract type "sg" of signatures.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@3956
     9
(*base names*)
wenzelm@3956
    10
type bstring = string;
wenzelm@3956
    11
type bclass = class;
wenzelm@3956
    12
(*external forms -- partially qualified names*)
wenzelm@3805
    13
type xstring = string;
wenzelm@3805
    14
type xclass = class;
wenzelm@3805
    15
type xsort = sort;
wenzelm@3805
    16
type xtyp = typ;
wenzelm@3805
    17
type xterm = term;
wenzelm@3805
    18
wenzelm@19
    19
signature SIGN =
wenzelm@3791
    20
sig
paulson@1501
    21
  type sg
wenzelm@3967
    22
  type sg_ref
wenzelm@4256
    23
  type data
wenzelm@2197
    24
  val rep_sg: sg ->
wenzelm@3975
    25
   {self: sg_ref,
wenzelm@3967
    26
    tsig: Type.type_sig,
wenzelm@2197
    27
    const_tab: typ Symtab.table,
wenzelm@11501
    28
    path: string list option,
wenzelm@3791
    29
    spaces: (string * NameSpace.T) list,
wenzelm@4256
    30
    data: data}
wenzelm@3975
    31
  val name_of: sg -> string
wenzelm@3975
    32
  val stamp_names_of: sg -> string list
wenzelm@10932
    33
  val exists_stamp: string -> sg -> bool
wenzelm@3967
    34
  val tsig_of: sg -> Type.type_sig
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
paulson@1501
    49
  val norm_sort: sg -> sort -> sort
wenzelm@4568
    50
  val of_sort: sg -> typ * sort -> bool
wenzelm@7640
    51
  val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
wenzelm@7640
    52
  val univ_witness: sg -> (typ * sort) option
wenzelm@10443
    53
  val typ_instance: sg -> typ * typ -> bool
wenzelm@3810
    54
  val classK: string
wenzelm@3810
    55
  val typeK: string
wenzelm@3810
    56
  val constK: string
wenzelm@3956
    57
  val full_name: sg -> bstring -> string
wenzelm@4844
    58
  val full_name_path: sg -> string -> bstring -> string
wenzelm@3956
    59
  val base_name: string -> bstring
wenzelm@3810
    60
  val intern: sg -> string -> xstring -> string
wenzelm@3810
    61
  val extern: sg -> string -> string -> xstring
wenzelm@3937
    62
  val cond_extern: sg -> string -> string -> xstring
wenzelm@6845
    63
  val cond_extern_table: sg -> string -> 'a Symtab.table -> (xstring * 'a) list
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@4249
    81
  val pretty_classrel: sg -> class * class -> Pretty.T
wenzelm@4249
    82
  val pretty_arity: sg -> string * sort list * sort -> 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@3855
    86
  val str_of_sort: sg -> sort -> string
wenzelm@3855
    87
  val str_of_classrel: sg -> class * class -> string
wenzelm@3855
    88
  val str_of_arity: sg -> string * sort list * sort -> string
paulson@1501
    89
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    90
  val pprint_typ: sg -> typ -> pprint_args -> unit
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@9504
    94
  val certify_typ_no_norm: sg -> typ -> typ
wenzelm@10443
    95
  val certify_tycon: sg -> string -> string
wenzelm@10443
    96
  val certify_tyabbr: sg -> string -> string
wenzelm@11720
    97
  val certify_tyname: sg -> string -> string
wenzelm@10443
    98
  val certify_const: sg -> string -> string
paulson@1501
    99
  val certify_term: sg -> term -> term * typ * int
wenzelm@8898
   100
  val read_sort: sg -> string -> sort
wenzelm@4227
   101
  val read_raw_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
   102
  val read_typ: sg * (indexname -> sort option) -> string -> typ
wenzelm@12068
   103
  val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@12068
   104
  val read_typ_no_norm': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
paulson@1501
   105
  val infer_types: sg -> (indexname -> typ option) ->
paulson@1501
   106
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   107
    -> xterm list * typ -> term * (indexname * typ) list
wenzelm@4249
   108
  val infer_types_simult: 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@12068
   111
  val read_def_terms':
wenzelm@12068
   112
    Syntax.syntax -> 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@3791
   192
    tsig: Type.type_sig,                        (*order-sorted signature of types*)
wenzelm@3805
   193
    const_tab: typ 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@3967
   217
fun make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps) =
wenzelm@14645
   218
  Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
wenzelm@14645
   219
    const_tab = const_tab, 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@3975
   233
fun const_type (Sg (_, {const_tab, ...})) c = Symtab.lookup (const_tab, c);
wenzelm@3967
   234
wenzelm@3967
   235
wenzelm@3995
   236
(* id and self *)
wenzelm@3967
   237
wenzelm@3975
   238
fun check_stale (sg as Sg ({id, ...},
wenzelm@3975
   239
        {self = SgRef (Some (ref (Sg ({id = id', ...}, _)))), ...})) =
wenzelm@3967
   240
      if id = id' then sg
wenzelm@3975
   241
      else raise TERM ("Stale signature: " ^ str_of_sg sg, [])
wenzelm@3967
   242
  | check_stale _ = sys_error "Sign.check_stale";
clasohm@0
   243
wenzelm@4951
   244
fun is_stale sg = (check_stale sg; false) handle TERM _ => true;
wenzelm@4951
   245
wenzelm@3995
   246
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale sg; self);
wenzelm@3995
   247
wenzelm@3995
   248
fun deref (SgRef (Some (ref sg))) = sg
wenzelm@3995
   249
  | deref (SgRef None) = sys_error "Sign.deref";
wenzelm@3995
   250
wenzelm@3975
   251
fun name_of (sg as Sg ({id = ref name, ...}, _)) =
wenzelm@4844
   252
  if name = "" orelse ord name = ord "#" then
wenzelm@3975
   253
    raise TERM ("Nameless signature " ^ str_of_sg sg, [])
wenzelm@3975
   254
  else name;
wenzelm@3975
   255
nipkow@206
   256
wenzelm@2979
   257
(* inclusion and equality *)
paulson@2180
   258
wenzelm@2185
   259
local
wenzelm@2185
   260
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
   261
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
   262
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   263
wenzelm@2185
   264
  fun subset_stamp ([], ys) = true
wenzelm@2185
   265
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   266
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
paulson@2180
   267
wenzelm@2185
   268
  (*fast partial test*)
wenzelm@2185
   269
  fun fast_sub ([]: string ref list, _) = true
wenzelm@2185
   270
    | fast_sub (_, []) = false
wenzelm@2185
   271
    | fast_sub (x :: xs, y :: ys) =
wenzelm@2185
   272
        if x = y then fast_sub (xs, ys)
wenzelm@2185
   273
        else fast_sub (x :: xs, ys);
wenzelm@2185
   274
in
wenzelm@3975
   275
  fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
wenzelm@3967
   276
    (check_stale sg1; check_stale sg2; id1 = id2);
wenzelm@2185
   277
wenzelm@3975
   278
  fun subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   279
    eq_sg (sg1, sg2) orelse subset_stamp (s1, s2);
paulson@2180
   280
wenzelm@3975
   281
  fun fast_subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   282
    eq_sg (sg1, sg2) orelse fast_sub (s1, s2);
wenzelm@2185
   283
end;
wenzelm@2185
   284
wenzelm@402
   285
wenzelm@9031
   286
fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
wenzelm@9031
   287
wenzelm@2185
   288
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   289
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   290
  same version of it*)
wenzelm@3975
   291
fun same_sg (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   292
  eq_sg (sg1, sg2) orelse eq_set_string (pairself (map (op !)) (s1, s2));
wenzelm@2185
   293
wenzelm@2185
   294
(*test for drafts*)
wenzelm@14645
   295
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) =
wenzelm@14645
   296
  name = "" orelse ord name = ord "#";
wenzelm@14645
   297
wenzelm@14645
   298
wenzelm@14645
   299
(* syntax *)
wenzelm@14645
   300
wenzelm@14645
   301
fun map_syntax f (Syn (syntax, trfuns)) = Syn (f syntax, trfuns);
wenzelm@14645
   302
wenzelm@14645
   303
fun make_syntax sg (Syn (syntax, (atrs, trs, tr's, atr's))) =
wenzelm@14645
   304
  let
wenzelm@14645
   305
    fun apply (s, (f, _: stamp)) = (s, f sg);
wenzelm@14645
   306
    fun prep tab = map apply (Symtab.dest tab);
wenzelm@14645
   307
    fun prep' tab = map apply (Symtab.dest_multi tab);
wenzelm@14645
   308
  in syntax |> Syntax.extend_trfuns (prep atrs, prep trs, prep' tr's, prep' atr's) end;
wenzelm@14645
   309
wenzelm@14645
   310
fun syn_of (sg as Sg ({syn, ...}, _)) = make_syntax sg syn;
wenzelm@14645
   311
wenzelm@14645
   312
wenzelm@14645
   313
(* advanced translation functions *)
wenzelm@14645
   314
wenzelm@14645
   315
fun extend_trfuns (atrs, trs, tr's, atr's)
wenzelm@14645
   316
    (Syn (syn, (parse_ast_trtab, parse_trtab, print_trtab, print_ast_trtab))) =
wenzelm@14645
   317
  Syn (syn, (Syntax.extend_trtab parse_ast_trtab atrs "parse ast translation",
wenzelm@14645
   318
    Syntax.extend_trtab parse_trtab trs "parse translation",
wenzelm@14645
   319
    Syntax.extend_tr'tab print_trtab tr's,
wenzelm@14645
   320
    Syntax.extend_tr'tab print_ast_trtab atr's));
wenzelm@14645
   321
wenzelm@14645
   322
fun merge_trfuns
wenzelm@14645
   323
    (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
wenzelm@14645
   324
    (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
wenzelm@14645
   325
  (Syntax.merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
wenzelm@14645
   326
    Syntax.merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
wenzelm@14645
   327
    Syntax.merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@14645
   328
    Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
wenzelm@386
   329
clasohm@0
   330
wenzelm@4568
   331
(* classes and sorts *)
wenzelm@4568
   332
wenzelm@7640
   333
val classes = Type.classes o tsig_of;
wenzelm@4844
   334
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   335
val subsort = Type.subsort o tsig_of;
wenzelm@4568
   336
val norm_sort = Type.norm_sort 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@7640
   339
val univ_witness = Type.univ_witness o tsig_of;
wenzelm@10443
   340
fun typ_instance sg (T, U) = Type.typ_instance (tsig_of sg, T, U);
wenzelm@4568
   341
wenzelm@4256
   342
wenzelm@4256
   343
(** signature data **)
wenzelm@4256
   344
wenzelm@4256
   345
(* errors *)
wenzelm@4256
   346
wenzelm@4261
   347
fun of_theory sg = "\nof theory " ^ str_of_sg sg;
wenzelm@4256
   348
wenzelm@4998
   349
fun err_inconsistent kinds =
wenzelm@4998
   350
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
wenzelm@4998
   351
wenzelm@6961
   352
fun err_method name kind e =
wenzelm@6961
   353
  (writeln ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
wenzelm@4256
   354
wenzelm@4256
   355
fun err_dup_init sg kind =
wenzelm@4256
   356
  error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4256
   357
wenzelm@4256
   358
fun err_uninit sg kind =
wenzelm@10404
   359
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^
wenzelm@10404
   360
         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
wenzelm@4256
   381
       None => []
wenzelm@4256
   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
wenzelm@4998
   414
      Some (k, x) =>
wenzelm@4998
   415
        if Object.eq_kind (kind, k) then x
wenzelm@4998
   416
        else err_access sg name
wenzelm@4998
   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
wenzelm@3967
   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@14645
   456
    (sg as Sg ({id = _, stamps, syn}, {self, tsig, const_tab, 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)
wenzelm@4256
   461
      else (SgRef (Some (ref sg)), prep_ext_data data);
wenzelm@3967
   462
  in
wenzelm@3967
   463
    create_sign self' stamps name
wenzelm@14645
   464
      (extfun sg (syn, tsig, const_tab, (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 \"\""
wenzelm@11501
   496
  | full None name = name
wenzelm@11501
   497
  | full (Some path) name =
wenzelm@11501
   498
      if NameSpace.is_qualified name then
wenzelm@11501
   499
        error ("Attempt to declare qualified name " ^ quote name)
wenzelm@11501
   500
      else NameSpace.pack (path @ [name]);
wenzelm@3937
   501
wenzelm@3937
   502
(*base name*)
wenzelm@3937
   503
val base_name = NameSpace.base;
wenzelm@3791
   504
wenzelm@3791
   505
wenzelm@3791
   506
(* intern / extern names *)
wenzelm@3791
   507
wenzelm@3791
   508
local
wenzelm@3791
   509
  (*prepare mapping of names*)
wenzelm@3791
   510
  fun mapping f add_xs t =
wenzelm@3791
   511
    let
wenzelm@3791
   512
      fun f' x = let val y = f x in if x = y then None else Some (x, y) end;
wenzelm@3791
   513
      val table = mapfilter f' (add_xs (t, []));
wenzelm@3791
   514
      fun lookup x = if_none (assoc (table, x)) x;
wenzelm@3791
   515
    in lookup end;
wenzelm@3791
   516
wenzelm@3791
   517
  (*intern / extern typ*)
wenzelm@3791
   518
  fun trn_typ trn T =
wenzelm@3791
   519
    T |> map_typ
wenzelm@3791
   520
      (mapping (trn classK) add_typ_classes T)
wenzelm@3791
   521
      (mapping (trn typeK) add_typ_tycons T);
wenzelm@3791
   522
wenzelm@3791
   523
  (*intern / extern term*)
wenzelm@3791
   524
  fun trn_term trn t =
wenzelm@3791
   525
    t |> map_term
wenzelm@3791
   526
      (mapping (trn classK) add_term_classes t)
wenzelm@3791
   527
      (mapping (trn typeK) add_term_tycons t)
wenzelm@3791
   528
      (mapping (trn constK) add_term_consts t);
wenzelm@3791
   529
wenzelm@3975
   530
  val spaces_of = #spaces o rep_sg;
wenzelm@3791
   531
in
wenzelm@3791
   532
  fun intrn_class spaces = intrn spaces classK;
berghofe@14383
   533
  fun extrn_class spaces = cond_extrn spaces classK;
wenzelm@3937
   534
wenzelm@3791
   535
  val intrn_sort = map o intrn_class;
wenzelm@3791
   536
  val intrn_typ = trn_typ o intrn;
wenzelm@3937
   537
  val intrn_term = trn_term o intrn;
wenzelm@3937
   538
wenzelm@3937
   539
  val extrn_sort = map o extrn_class;
berghofe@14383
   540
  val extrn_typ = trn_typ o cond_extrn;
berghofe@14383
   541
  val extrn_term = trn_term o cond_extrn;
wenzelm@3791
   542
wenzelm@3805
   543
  fun intrn_tycons spaces T =
wenzelm@3805
   544
    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
wenzelm@3805
   545
wenzelm@3810
   546
  val intern = intrn o spaces_of;
wenzelm@3855
   547
  val extern = extrn o spaces_of;
wenzelm@5175
   548
  val cond_extern = cond_extrn o spaces_of;
wenzelm@6845
   549
  fun cond_extern_table sg = cond_extrn_table (spaces_of sg);
wenzelm@3937
   550
wenzelm@3791
   551
  val intern_class = intrn_class o spaces_of;
wenzelm@3791
   552
  val intern_sort = intrn_sort o spaces_of;
wenzelm@3791
   553
  val intern_typ = intrn_typ o spaces_of;
wenzelm@3791
   554
  val intern_term = intrn_term o spaces_of;
wenzelm@3791
   555
wenzelm@3791
   556
  fun intern_tycon sg = intrn (spaces_of sg) typeK;
wenzelm@3791
   557
  fun intern_const sg = intrn (spaces_of sg) constK;
wenzelm@3937
   558
wenzelm@3791
   559
  val intern_tycons = intrn_tycons o spaces_of;
wenzelm@3791
   560
wenzelm@3975
   561
  val full_name = full o #path o rep_sg;
wenzelm@11501
   562
wenzelm@11501
   563
  fun full_name_path sg elems =
wenzelm@11501
   564
    full (Some (if_none (#path (rep_sg sg)) [] @ NameSpace.unpack elems));
wenzelm@11501
   565
wenzelm@3791
   566
end;
wenzelm@620
   567
wenzelm@402
   568
clasohm@0
   569
wenzelm@14645
   570
(** partial Pure signature **)
wenzelm@14645
   571
wenzelm@14645
   572
val pure_syn =
wenzelm@14645
   573
  Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
wenzelm@14645
   574
wenzelm@14645
   575
val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0,
wenzelm@14645
   576
  Symtab.empty, pure_syn, Some [], [], empty_data, []);
wenzelm@14645
   577
wenzelm@14645
   578
val pre_pure =
wenzelm@14645
   579
  create_sign (SgRef (Some (ref dummy_sg))) [] "#"
wenzelm@14645
   580
    (pure_syn, Type.tsig0, Symtab.empty, (Some [], []), empty_data);
wenzelm@14645
   581
wenzelm@14645
   582
val PureN = "Pure";
wenzelm@14645
   583
val CPureN = "CPure";
wenzelm@14645
   584
wenzelm@14645
   585
wenzelm@14645
   586
wenzelm@4249
   587
(** pretty printing of terms, types etc. **)
wenzelm@3937
   588
wenzelm@12068
   589
fun pretty_term' syn (sg as Sg ({stamps, ...}, {spaces, ...})) t =
wenzelm@3937
   590
  Syntax.pretty_term syn
wenzelm@14645
   591
    (exists (equal CPureN o !) stamps)
wenzelm@5175
   592
    (if ! NameSpace.long_names then t else extrn_term spaces t);
wenzelm@3937
   593
wenzelm@12068
   594
fun pretty_term sg = pretty_term' (syn_of sg) sg;
wenzelm@12068
   595
wenzelm@14645
   596
fun pretty_typ (sg as Sg (_, {spaces, ...})) T =
wenzelm@14645
   597
  Syntax.pretty_typ (syn_of sg)
wenzelm@5175
   598
    (if ! NameSpace.long_names then T else extrn_typ spaces T);
wenzelm@3937
   599
wenzelm@14645
   600
fun pretty_sort (sg as Sg (_, {spaces, ...})) S =
wenzelm@14645
   601
  Syntax.pretty_sort (syn_of sg)
wenzelm@5175
   602
    (if ! NameSpace.long_names then S else extrn_sort spaces S);
wenzelm@3937
   603
wenzelm@3937
   604
fun pretty_classrel sg (c1, c2) = Pretty.block
wenzelm@3937
   605
  [pretty_sort sg [c1], Pretty.str " <", Pretty.brk 1, pretty_sort sg [c2]];
wenzelm@3937
   606
wenzelm@3937
   607
fun pretty_arity sg (t, Ss, S) =
wenzelm@3937
   608
  let
wenzelm@3937
   609
    val t' = cond_extern sg typeK t;
wenzelm@3937
   610
    val dom =
wenzelm@3937
   611
      if null Ss then []
wenzelm@3937
   612
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@3937
   613
  in
wenzelm@3937
   614
    Pretty.block
wenzelm@3937
   615
      ([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S])
wenzelm@3937
   616
  end;
wenzelm@3937
   617
wenzelm@3937
   618
fun string_of_term sg t = Pretty.string_of (pretty_term sg t);
wenzelm@3937
   619
fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T);
wenzelm@3937
   620
fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S);
wenzelm@3937
   621
wenzelm@3937
   622
fun str_of_sort sg S = Pretty.str_of (pretty_sort sg S);
wenzelm@3937
   623
fun str_of_classrel sg c1_c2 = Pretty.str_of (pretty_classrel sg c1_c2);
wenzelm@3937
   624
fun str_of_arity sg ar = Pretty.str_of (pretty_arity sg ar);
wenzelm@3937
   625
wenzelm@3937
   626
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@3937
   627
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
wenzelm@3937
   628
wenzelm@3937
   629
wenzelm@3937
   630
wenzelm@8898
   631
(** read sorts **)  (*exception ERROR*)
wenzelm@8898
   632
wenzelm@8898
   633
fun err_in_sort s =
wenzelm@8898
   634
  error ("The error(s) above occurred in sort " ^ quote s);
wenzelm@8898
   635
wenzelm@14645
   636
fun rd_sort sg syn tsig spaces str =
wenzelm@14645
   637
  let val S = intrn_sort spaces (Syntax.read_sort (make_syntax sg syn) str
wenzelm@14645
   638
    handle ERROR => err_in_sort str)
wenzelm@8898
   639
  in Type.cert_sort tsig S handle TYPE (msg, _, _) => (error_msg msg; err_in_sort str) end;
wenzelm@8898
   640
wenzelm@8898
   641
(*read and certify sort wrt a signature*)
wenzelm@14645
   642
fun read_sort (sg as Sg ({syn, ...}, {tsig, spaces, ...})) str =
wenzelm@14645
   643
  (check_stale sg; rd_sort sg syn tsig spaces str);
wenzelm@8898
   644
wenzelm@14645
   645
fun cert_sort _ _ tsig _ = Type.cert_sort tsig;
wenzelm@8898
   646
wenzelm@8898
   647
wenzelm@8898
   648
wenzelm@562
   649
(** read types **)  (*exception ERROR*)
wenzelm@562
   650
wenzelm@562
   651
fun err_in_type s =
wenzelm@562
   652
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   653
wenzelm@4227
   654
fun rd_raw_typ syn tsig spaces def_sort str =
wenzelm@3791
   655
  intrn_tycons spaces
wenzelm@12313
   656
    (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) (intrn_sort spaces) str
wenzelm@3791
   657
      handle ERROR => err_in_type str);
wenzelm@4227
   658
wenzelm@12068
   659
fun read_raw_typ' syn (sg as Sg (_, {tsig, spaces, ...}), def_sort) str =
wenzelm@4227
   660
  (check_stale sg; rd_raw_typ syn tsig spaces def_sort str);
wenzelm@4227
   661
wenzelm@12068
   662
fun read_raw_typ (sg, def_sort) = read_raw_typ' (syn_of sg) (sg, def_sort);
wenzelm@12068
   663
wenzelm@562
   664
(*read and certify typ wrt a signature*)
wenzelm@9504
   665
local
wenzelm@12068
   666
  fun read_typ_aux rd cert (sg, def_sort) str =
wenzelm@12068
   667
    (cert (tsig_of sg) (rd (sg, def_sort) str)
wenzelm@12068
   668
      handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
wenzelm@9504
   669
in
wenzelm@12068
   670
  val read_typ              = read_typ_aux read_raw_typ Type.cert_typ;
wenzelm@12068
   671
  val read_typ_no_norm      = read_typ_aux read_raw_typ Type.cert_typ_no_norm;
wenzelm@12068
   672
  fun read_typ' syn         = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
wenzelm@12068
   673
  fun read_typ_no_norm' syn = read_typ_aux (read_raw_typ' syn) Type.cert_typ_no_norm;
wenzelm@9504
   674
end;
wenzelm@562
   675
wenzelm@562
   676
wenzelm@562
   677
wenzelm@8898
   678
(** certify classes, sorts, types and terms **)   (*exception TYPE*)
wenzelm@251
   679
wenzelm@8898
   680
val certify_class = Type.cert_class o tsig_of;
wenzelm@8898
   681
val certify_sort = Type.cert_sort o tsig_of;
wenzelm@3975
   682
val certify_typ = Type.cert_typ o tsig_of;
wenzelm@9504
   683
val certify_typ_no_norm = Type.cert_typ_no_norm o tsig_of;
wenzelm@10443
   684
wenzelm@10443
   685
fun certify_tycon sg c =
wenzelm@10443
   686
  if is_some (Symtab.lookup (#tycons (Type.rep_tsig (tsig_of sg)), c)) then c
wenzelm@10443
   687
  else raise TYPE ("Undeclared type constructor " ^ quote c, [], []);
wenzelm@10443
   688
wenzelm@10443
   689
fun certify_tyabbr sg c =
wenzelm@10443
   690
  if is_some (Symtab.lookup (#abbrs (Type.rep_tsig (tsig_of sg)), c)) then c
wenzelm@10443
   691
  else raise TYPE ("Unknown type abbreviation " ^ quote c, [], []);
wenzelm@10443
   692
wenzelm@11720
   693
fun certify_tyname sg c =
wenzelm@11720
   694
  certify_tycon sg c handle TYPE _ => certify_tyabbr sg c handle TYPE _ =>
wenzelm@11720
   695
    raise TYPE ("Unknown type name " ^ quote c, [], []);
wenzelm@11720
   696
wenzelm@10443
   697
fun certify_const sg c =
wenzelm@10443
   698
  if is_some (const_type sg c) then c else raise TYPE ("Undeclared constant " ^ quote c, [], []);
wenzelm@251
   699
wenzelm@4961
   700
wenzelm@4961
   701
(* certify_term *)
wenzelm@4961
   702
wenzelm@8290
   703
(*check for duplicate occurrences of TFree/TVar with distinct sorts*)
wenzelm@8290
   704
fun nodup_tvars (env, Type (_, Ts)) = nodup_tvars_list (env, Ts)
wenzelm@8290
   705
  | nodup_tvars (env as (tfrees, tvars), T as TFree (v as (a, S))) =
wenzelm@8290
   706
      (case assoc_string (tfrees, a) of
wenzelm@8290
   707
        Some S' =>
wenzelm@8290
   708
          if S = S' then env
wenzelm@8290
   709
          else raise TYPE ("Type variable " ^ quote a ^
wenzelm@8290
   710
            " has two distinct sorts", [TFree (a, S'), T], [])
wenzelm@8290
   711
      | None => (v :: tfrees, tvars))
wenzelm@8290
   712
  | nodup_tvars (env as (tfrees, tvars), T as TVar (v as (a, S))) =
wenzelm@2979
   713
      (case assoc_string_int (tvars, a) of
wenzelm@2979
   714
        Some S' =>
wenzelm@8290
   715
          if S = S' then env
wenzelm@8290
   716
          else raise TYPE ("Type variable " ^ quote (Syntax.string_of_vname a) ^
wenzelm@3791
   717
            " has two distinct sorts", [TVar (a, S'), T], [])
wenzelm@8290
   718
      | None => (tfrees, v :: tvars))
wenzelm@8290
   719
(*equivalent to foldl nodup_tvars_list, but 3X faster under Poly/ML*)
wenzelm@8290
   720
and nodup_tvars_list (env, []) = env
wenzelm@8290
   721
  | nodup_tvars_list (env, T :: Ts) = nodup_tvars_list (nodup_tvars (env, T), Ts);
nipkow@1494
   722
wenzelm@8290
   723
(*check for duplicate occurrences of Free/Var with distinct types*)
wenzelm@8290
   724
fun nodup_vars tm =
wenzelm@2979
   725
  let
wenzelm@8290
   726
    fun nodups (envs as (env as (frees, vars), envT)) tm =
wenzelm@2979
   727
      (case tm of
wenzelm@8290
   728
        Const (c, T) => (env, nodup_tvars (envT, T))
wenzelm@8290
   729
      | Free (v as (a, T)) =>
wenzelm@8290
   730
          (case assoc_string (frees, a) of
wenzelm@8290
   731
            Some T' =>
wenzelm@8290
   732
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@8290
   733
              else raise TYPE ("Variable " ^ quote a ^
wenzelm@8290
   734
                " has two distinct types", [T', T], [])
wenzelm@8290
   735
          | None => ((v :: frees, vars), nodup_tvars (envT, T)))
wenzelm@2979
   736
      | Var (v as (ixn, T)) =>
wenzelm@2979
   737
          (case assoc_string_int (vars, ixn) of
wenzelm@2979
   738
            Some T' =>
wenzelm@8290
   739
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@8290
   740
              else raise TYPE ("Variable " ^ quote (Syntax.string_of_vname ixn) ^
wenzelm@3791
   741
                " has two distinct types", [T', T], [])
wenzelm@8290
   742
          | None => ((frees, v :: vars), nodup_tvars (envT, T)))
wenzelm@8290
   743
      | Bound _ => envs
wenzelm@8290
   744
      | Abs (_, T, t) => nodups (env, nodup_tvars (envT, T)) t
wenzelm@8290
   745
      | s $ t => nodups (nodups envs s) t)
wenzelm@8290
   746
  in nodups (([], []), ([], [])) tm; tm end;
wenzelm@2979
   747
wenzelm@4961
   748
(*compute and check type of the term*)
wenzelm@4961
   749
fun type_check sg tm =
wenzelm@4961
   750
  let
wenzelm@8725
   751
    val prt = setmp Syntax.show_brackets true (pretty_term sg);
wenzelm@8725
   752
    val prT = pretty_typ sg;
wenzelm@251
   753
wenzelm@4961
   754
    fun err_appl why bs t T u U =
wenzelm@4961
   755
      let
wenzelm@10404
   756
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   757
        val t' = subst_bounds (xs, t);
wenzelm@4961
   758
        val u' = subst_bounds (xs, u);
wenzelm@10404
   759
        val text = cat_lines (TypeInfer.appl_error prt prT why t' T u' U);
wenzelm@4961
   760
      in raise TYPE (text, [T, U], [t', u']) end;
wenzelm@4961
   761
wenzelm@4961
   762
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   763
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   764
      | typ_of (_, Var (_, T)) = T
wenzelm@4961
   765
      | typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ =>
wenzelm@4961
   766
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   767
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   768
      | typ_of (bs, t $ u) =
wenzelm@4961
   769
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   770
            (case T of
wenzelm@4961
   771
              Type ("fun", [T1, T2]) =>
wenzelm@4961
   772
                if T1 = U then T2 else err_appl "Incompatible operand type." bs t T u U
wenzelm@4961
   773
            | _ => err_appl "Operator not of function type." bs t T u U)
wenzelm@4961
   774
          end;
wenzelm@4961
   775
wenzelm@4961
   776
  in typ_of ([], tm) end;
clasohm@0
   777
wenzelm@2979
   778
wenzelm@3975
   779
fun certify_term sg tm =
wenzelm@251
   780
  let
wenzelm@3969
   781
    val _ = check_stale sg;
wenzelm@3975
   782
    val tsig = tsig_of sg;
wenzelm@3969
   783
wenzelm@3975
   784
    fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T);
wenzelm@169
   785
wenzelm@4961
   786
    fun atom_err (errs, Const (a, T)) =
wenzelm@3975
   787
        (case const_type sg a of
wenzelm@4961
   788
          None => ("Undeclared constant " ^ show_const a T) :: errs
wenzelm@3975
   789
        | Some U =>
wenzelm@10404
   790
            if typ_instance sg (T, U) then errs
wenzelm@4961
   791
            else ("Illegal type for constant " ^ show_const a T) :: errs)
wenzelm@4961
   792
      | atom_err (errs, Var ((x, i), _)) =
wenzelm@4961
   793
          if i < 0 then ("Negative index for Var " ^ quote x) :: errs else errs
wenzelm@4961
   794
      | atom_err (errs, _) = errs;
wenzelm@251
   795
wenzelm@251
   796
    val norm_tm =
wenzelm@2185
   797
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@7068
   798
        [] => Type.norm_term tsig tm
wenzelm@3791
   799
      | errs => raise TYPE (cat_lines errs, [], [tm]));
wenzelm@8290
   800
    val _ = nodup_vars norm_tm;
wenzelm@251
   801
  in
wenzelm@4961
   802
    (case foldl_aterms atom_err ([], norm_tm) of
wenzelm@4961
   803
      [] => (norm_tm, type_check sg norm_tm, maxidx_of_term norm_tm)
wenzelm@3791
   804
    | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
wenzelm@251
   805
  end;
wenzelm@251
   806
wenzelm@251
   807
wenzelm@583
   808
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   809
wenzelm@2979
   810
(*
wenzelm@2979
   811
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   812
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   813
  used: list of already used type variables
wenzelm@2979
   814
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   815
wenzelm@4249
   816
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   817
  typs: expected types
wenzelm@2979
   818
*)
wenzelm@2979
   819
wenzelm@4249
   820
fun infer_types_simult sg def_type def_sort used freeze args =
wenzelm@251
   821
  let
wenzelm@3975
   822
    val tsig = tsig_of sg;
wenzelm@8725
   823
    val prt = setmp Syntax.show_brackets true (pretty_term sg);
wenzelm@8725
   824
    val prT = pretty_typ sg;
wenzelm@4249
   825
wenzelm@4249
   826
    val termss = foldr multiply (map fst args, [[]]);
wenzelm@4249
   827
    val typs =
wenzelm@4249
   828
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   829
wenzelm@4249
   830
    fun infer ts = OK
wenzelm@4249
   831
      (Type.infer_types prt prT tsig (const_type sg) def_type def_sort
wenzelm@4249
   832
        (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze typs ts)
wenzelm@4249
   833
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   834
wenzelm@4249
   835
    val err_results = map infer termss;
wenzelm@4249
   836
    val errs = mapfilter get_error err_results;
wenzelm@4249
   837
    val results = mapfilter get_ok err_results;
wenzelm@4249
   838
wenzelm@14645
   839
    val ambiguity = length termss;
wenzelm@14645
   840
wenzelm@4249
   841
    fun ambig_msg () =
wenzelm@4249
   842
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   843
      then
wenzelm@4249
   844
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   845
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   846
      else ();
wenzelm@4249
   847
  in
wenzelm@4249
   848
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   849
    else if length results = 1 then
wenzelm@4249
   850
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   851
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   852
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   853
      else (); hd results)
wenzelm@4249
   854
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
wenzelm@4249
   855
      (cat_lines (map (Pretty.string_of o prt) (flat (map fst results))))))
wenzelm@4249
   856
  end;
clasohm@623
   857
wenzelm@4249
   858
wenzelm@4249
   859
fun infer_types sg def_type def_sort used freeze tsT =
wenzelm@4249
   860
  apfst hd (infer_types_simult sg def_type def_sort used freeze [tsT]);
wenzelm@251
   861
wenzelm@251
   862
wenzelm@8607
   863
(** read_def_terms **)
wenzelm@8607
   864
wenzelm@8607
   865
(*read terms, infer types*)
wenzelm@12068
   866
fun read_def_terms' syn (sign, types, sorts) used freeze sTs =
wenzelm@8607
   867
  let
wenzelm@8607
   868
    fun read (s, T) =
wenzelm@8607
   869
      let val T' = certify_typ sign T handle TYPE (msg, _, _) => error msg
wenzelm@8607
   870
      in (Syntax.read syn T' s, T') end;
wenzelm@8607
   871
    val tsTs = map read sTs;
wenzelm@8607
   872
  in infer_types_simult sign types sorts used freeze tsTs end;
wenzelm@8607
   873
wenzelm@14645
   874
fun read_def_terms (sign, types, sorts) =
wenzelm@14645
   875
  read_def_terms' (syn_of sign) (sign, types, sorts);
wenzelm@12068
   876
wenzelm@8802
   877
fun simple_read_term sign T s =
wenzelm@8802
   878
  (read_def_terms (sign, K None, K None) [] true [(s, T)]
wenzelm@8802
   879
    handle ERROR => error ("The error(s) above occurred for " ^ s)) |> #1 |> hd;
wenzelm@8802
   880
wenzelm@8607
   881
wenzelm@2979
   882
clasohm@623
   883
(** extend signature **)    (*exception ERROR*)
clasohm@623
   884
wenzelm@620
   885
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   886
wenzelm@3791
   887
fun decls_of path name_of mfixs =
wenzelm@3810
   888
  map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs;
wenzelm@3791
   889
wenzelm@14645
   890
fun no_read _ _ _ _ decl = decl;
wenzelm@386
   891
wenzelm@386
   892
wenzelm@386
   893
(* add default sort *)
wenzelm@386
   894
wenzelm@14645
   895
fun ext_defS prep_sort sg (syn, tsig, ctab, (path, spaces), data) S =
wenzelm@14645
   896
  (syn, Type.ext_tsig_defsort tsig (prep_sort sg syn tsig spaces S),
wenzelm@14645
   897
    ctab, (path, spaces), data);
wenzelm@8898
   898
wenzelm@14645
   899
fun ext_defsort arg = ext_defS rd_sort arg;
paulson@8927
   900
fun ext_defsort_i arg = ext_defS cert_sort arg;
wenzelm@386
   901
wenzelm@386
   902
wenzelm@386
   903
(* add type constructors *)
wenzelm@386
   904
wenzelm@14645
   905
fun ext_types _ (syn, tsig, ctab, (path, spaces), data) types =
wenzelm@3791
   906
  let val decls = decls_of path Syntax.type_name types in
wenzelm@14645
   907
    (map_syntax (Syntax.extend_type_gram types) syn,
wenzelm@3791
   908
      Type.ext_tsig_types tsig decls, ctab,
wenzelm@3967
   909
      (path, add_names spaces typeK (map fst decls)), data)
wenzelm@3791
   910
  end;
wenzelm@386
   911
wenzelm@14645
   912
fun ext_nonterminals sign sg nonterms =
wenzelm@14645
   913
  ext_types sign sg (map (fn n => (n, 0, Syntax.NoSyn)) nonterms);
wenzelm@4844
   914
wenzelm@386
   915
wenzelm@386
   916
(* add type abbreviations *)
wenzelm@386
   917
wenzelm@14645
   918
fun read_abbr sg syn tsig spaces (t, vs, rhs_src) =
wenzelm@14645
   919
  (t, vs, rd_raw_typ (make_syntax sg syn) tsig spaces (K None) rhs_src)
wenzelm@386
   920
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   921
wenzelm@14645
   922
fun ext_abbrs rd_abbr sg (syn, tsig, ctab, (path, spaces), data) abbrs =
wenzelm@386
   923
  let
wenzelm@386
   924
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@14645
   925
    val syn' = map_syntax (Syntax.extend_type_gram (map mfix_of abbrs)) syn;
wenzelm@386
   926
wenzelm@3791
   927
    val abbrs' =
wenzelm@3791
   928
      map (fn (t, vs, rhs, mx) =>
wenzelm@3810
   929
        (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
wenzelm@3805
   930
    val spaces' = add_names spaces typeK (map #1 abbrs');
wenzelm@14645
   931
    val decls = map (rd_abbr sg syn' tsig spaces') abbrs';
wenzelm@386
   932
  in
wenzelm@3967
   933
    (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data)
wenzelm@386
   934
  end;
wenzelm@386
   935
wenzelm@3791
   936
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   937
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   938
wenzelm@386
   939
wenzelm@386
   940
(* add type arities *)
wenzelm@386
   941
wenzelm@14645
   942
fun ext_ars int prep_sort sg (syn, tsig, ctab, (path, spaces), data) arities =
wenzelm@386
   943
  let
wenzelm@14645
   944
    val prepS = prep_sort sg syn tsig spaces;
wenzelm@8898
   945
    fun prep_arity (c, Ss, S) =
wenzelm@8898
   946
      (if int then intrn spaces typeK c else c, map prepS Ss, prepS S);
wenzelm@8898
   947
    val tsig' = Type.ext_tsig_arities tsig (map prep_arity arities);
wenzelm@3791
   948
    val log_types = Type.logical_types tsig';
wenzelm@386
   949
  in
wenzelm@14645
   950
    (map_syntax (Syntax.extend_log_types log_types) syn,
wenzelm@14645
   951
      tsig', ctab, (path, spaces), data)
wenzelm@386
   952
  end;
wenzelm@386
   953
wenzelm@14645
   954
fun ext_arities arg = ext_ars true rd_sort arg;
paulson@8927
   955
fun ext_arities_i arg = ext_ars false cert_sort arg;
wenzelm@8898
   956
wenzelm@386
   957
wenzelm@386
   958
(* add term constants and syntax *)
wenzelm@386
   959
wenzelm@3805
   960
fun const_name path c mx =
wenzelm@3810
   961
  full path (Syntax.const_name c mx);
wenzelm@3805
   962
wenzelm@386
   963
fun err_in_const c =
wenzelm@386
   964
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   965
wenzelm@386
   966
fun err_dup_consts cs =
wenzelm@386
   967
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   968
wenzelm@251
   969
wenzelm@14645
   970
fun read_const sg syn tsig (path, spaces) (c, ty_src, mx) =
wenzelm@14645
   971
  (c, rd_raw_typ (make_syntax sg syn) tsig spaces (K None) ty_src, mx)
wenzelm@3805
   972
    handle ERROR => err_in_const (const_name path c mx);
wenzelm@386
   973
wenzelm@14645
   974
fun ext_cnsts rd_const syn_only prmode sg (syn, tsig, ctab, (path, spaces), data)
wenzelm@14645
   975
    raw_consts =
wenzelm@386
   976
  let
wenzelm@2979
   977
    fun prep_const (c, ty, mx) =
wenzelm@3791
   978
      (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@3791
   979
        handle TYPE (msg, _, _) =>
wenzelm@3805
   980
          (error_msg msg; err_in_const (const_name path c mx));
wenzelm@386
   981
wenzelm@14645
   982
    val consts = map (prep_const o rd_const sg syn tsig (path, spaces)) raw_consts;
wenzelm@386
   983
    val decls =
wenzelm@386
   984
      if syn_only then []
wenzelm@3791
   985
      else decls_of path Syntax.const_name consts;
wenzelm@386
   986
  in
wenzelm@14645
   987
    (map_syntax (Syntax.extend_const_gram prmode consts) syn, tsig,
wenzelm@4489
   988
      Symtab.extend (ctab, decls)
wenzelm@3791
   989
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@3967
   990
      (path, add_names spaces constK (map fst decls)), data)
wenzelm@386
   991
  end;
wenzelm@386
   992
wenzelm@14645
   993
fun ext_consts_i x = ext_cnsts no_read false ("", true) x;
wenzelm@14645
   994
fun ext_consts x = ext_cnsts read_const false ("", true) x;
wenzelm@14645
   995
fun ext_syntax_i x = ext_cnsts no_read true ("", true) x;
wenzelm@14645
   996
fun ext_syntax x = ext_cnsts read_const true ("", true) x;
wenzelm@14645
   997
fun ext_modesyntax_i x y (prmode, consts) = ext_cnsts no_read true prmode x y consts;
wenzelm@14645
   998
fun ext_modesyntax x y (prmode, consts) = ext_cnsts read_const true prmode x y consts;
wenzelm@386
   999
wenzelm@386
  1000
wenzelm@386
  1001
(* add type classes *)
wenzelm@386
  1002
wenzelm@386
  1003
fun const_of_class c = c ^ "_class";
wenzelm@386
  1004
wenzelm@386
  1005
fun class_of_const c_class =
wenzelm@386
  1006
  let
wenzelm@3791
  1007
    val c = implode (take (size c_class - size "_class", explode c_class));
wenzelm@386
  1008
  in
wenzelm@386
  1009
    if const_of_class c = c_class then c
wenzelm@3791
  1010
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
  1011
  end;
wenzelm@386
  1012
wenzelm@386
  1013
wenzelm@14645
  1014
fun ext_classes int sg (syn, tsig, ctab, (path, spaces), data) classes =
wenzelm@386
  1015
  let
wenzelm@562
  1016
    val names = map fst classes;
wenzelm@386
  1017
    val consts =
wenzelm@386
  1018
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
  1019
wenzelm@3810
  1020
    val full_names = map (full path) names;
wenzelm@3791
  1021
    val spaces' = add_names spaces classK full_names;
wenzelm@3791
  1022
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
  1023
    val classes' =
wenzelm@3791
  1024
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
  1025
  in
wenzelm@14645
  1026
    ext_consts_i sg
wenzelm@14645
  1027
      (map_syntax (Syntax.extend_consts names) syn,
wenzelm@3967
  1028
        Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data)
wenzelm@3791
  1029
    consts
wenzelm@386
  1030
  end;
wenzelm@386
  1031
wenzelm@386
  1032
wenzelm@2963
  1033
(* add to classrel *)
wenzelm@421
  1034
wenzelm@14645
  1035
fun ext_classrel int _ (syn, tsig, ctab, (path, spaces), data) pairs =
wenzelm@3791
  1036
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@3967
  1037
    (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data)
wenzelm@3791
  1038
  end;
wenzelm@421
  1039
wenzelm@421
  1040
wenzelm@14645
  1041
(* add translation functions *)
wenzelm@14645
  1042
wenzelm@14645
  1043
fun ext_trfs ext non_typed sg (syn, tsig, ctab, names, data) (atrs, trs, tr's, atr's) =
wenzelm@14645
  1044
  let val syn' = syn |> ext (atrs, trs, map (apsnd non_typed) tr's, atr's)
wenzelm@14645
  1045
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1046
wenzelm@14645
  1047
fun ext_trfsT ext sg (syn, tsig, ctab, names, data) tr's =
wenzelm@14645
  1048
  let val syn' = syn |> ext ([], [], tr's, [])
wenzelm@14645
  1049
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1050
wenzelm@14645
  1051
fun ext_trfuns sg = ext_trfs (map_syntax o Syntax.extend_trfuns) Syntax.non_typed_tr' sg;
wenzelm@14645
  1052
fun ext_trfunsT sg = ext_trfsT (map_syntax o Syntax.extend_trfuns) sg;
wenzelm@14645
  1053
fun ext_advanced_trfuns sg = ext_trfs extend_trfuns Syntax.non_typed_tr'' sg;
wenzelm@14645
  1054
fun ext_advanced_trfunsT sg = ext_trfsT extend_trfuns sg;
wenzelm@14645
  1055
wenzelm@14645
  1056
wenzelm@14645
  1057
(* add token translation functions *)
wenzelm@14645
  1058
wenzelm@14645
  1059
fun ext_tokentrfuns sg (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1060
  (map_syntax (Syntax.extend_tokentrfuns args) syn, tsig, ctab, names, data);
wenzelm@14645
  1061
wenzelm@14645
  1062
wenzelm@4619
  1063
(* add translation rules *)
wenzelm@4619
  1064
wenzelm@14645
  1065
fun ext_trrules sg (syn, tsig, ctab, (path, spaces), data) args =
wenzelm@14645
  1066
  (syn |> map_syntax (Syntax.extend_trrules (make_syntax sg syn)
wenzelm@14645
  1067
      (map (Syntax.map_trrule (fn (root, str) => (intrn spaces typeK root, str))) args)),
wenzelm@14645
  1068
    tsig, ctab, (path, spaces), data);
wenzelm@4619
  1069
wenzelm@14645
  1070
fun ext_trrules_i _ (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1071
  (syn |> map_syntax (Syntax.extend_trrules_i args), tsig, ctab, names, data);
wenzelm@3791
  1072
wenzelm@3791
  1073
wenzelm@3791
  1074
(* add to path *)
wenzelm@3791
  1075
wenzelm@14645
  1076
fun ext_path _ (syn, tsig, ctab, (path, spaces), data) elems =
wenzelm@3791
  1077
  let
wenzelm@3791
  1078
    val path' =
wenzelm@11501
  1079
      if elems = "//" then None
wenzelm@11501
  1080
      else if elems = "/" then Some []
wenzelm@11501
  1081
      else if elems = ".." andalso is_some path andalso path <> Some [] then
wenzelm@11501
  1082
        Some (fst (split_last (the path)))
wenzelm@11501
  1083
      else Some (if_none path [] @ NameSpace.unpack elems);
wenzelm@3791
  1084
  in
wenzelm@3967
  1085
    (syn, tsig, ctab, (path', spaces), data)
wenzelm@10404
  1086
  end;
wenzelm@3791
  1087
wenzelm@3791
  1088
wenzelm@8725
  1089
(* change name space *)
wenzelm@8725
  1090
wenzelm@14645
  1091
fun ext_add_space _ (syn, tsig, ctab, (path, spaces), data) (kind, names) =
wenzelm@8725
  1092
  (syn, tsig, ctab, (path, add_names spaces kind names), data);
wenzelm@3791
  1093
wenzelm@14645
  1094
fun ext_hide_space _ (syn, tsig, ctab, (path, spaces), data) (b, (kind, xnames)) =
wenzelm@12588
  1095
  (syn, tsig, ctab, (path, hide_names b spaces kind (map (intrn spaces kind) xnames)), data);
wenzelm@8725
  1096
wenzelm@14645
  1097
fun ext_hide_space_i _ (syn, tsig, ctab, (path, spaces), data) (b, (kind, names)) =
wenzelm@12588
  1098
  (syn, tsig, ctab, (path, hide_names b spaces kind names), data);
wenzelm@386
  1099
wenzelm@386
  1100
wenzelm@3967
  1101
(* signature data *)
wenzelm@386
  1102
wenzelm@12310
  1103
fun ext_init_data sg (syn, tsig, ctab, names, data) (kind, (e, cp, ext, mrg, prt)) =
wenzelm@12310
  1104
  (syn, tsig, ctab, names, init_data_sg sg data kind e cp ext mrg prt);
wenzelm@386
  1105
wenzelm@4998
  1106
fun ext_put_data sg (syn, tsig, ctab, names, data) (kind, f, x) =
wenzelm@4998
  1107
  (syn, tsig, ctab, names, put_data_sg sg data kind f x);
wenzelm@386
  1108
wenzelm@386
  1109
wenzelm@12310
  1110
fun copy_data (k, (e, mths as (cp, _, _, _))) =
wenzelm@6961
  1111
  (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
wenzelm@6546
  1112
wenzelm@14645
  1113
fun copy (sg as Sg ({id = _, stamps, syn}, {self, tsig, const_tab, path, spaces, data})) =
wenzelm@6546
  1114
  let
wenzelm@6546
  1115
    val _ = check_stale sg;
wenzelm@6546
  1116
    val self' = SgRef (Some (ref sg));
wenzelm@6546
  1117
    val Data tab = data;
wenzelm@6546
  1118
    val data' = Data (Symtab.map copy_data tab);
wenzelm@6546
  1119
  in create_sign self' stamps "#" (syn, tsig, const_tab, (path, spaces), data') end;
wenzelm@6546
  1120
wenzelm@6546
  1121
wenzelm@386
  1122
(* the external interfaces *)
wenzelm@386
  1123
wenzelm@14645
  1124
val add_classes          = extend_sign true (ext_classes true) "#";
wenzelm@14645
  1125
val add_classes_i        = extend_sign true (ext_classes false) "#";
wenzelm@14645
  1126
val add_classrel         = extend_sign true (ext_classrel true) "#";
wenzelm@14645
  1127
val add_classrel_i       = extend_sign true (ext_classrel false) "#";
wenzelm@14645
  1128
val add_defsort          = extend_sign true ext_defsort "#";
wenzelm@14645
  1129
val add_defsort_i        = extend_sign true ext_defsort_i "#";
wenzelm@14645
  1130
val add_types            = extend_sign true ext_types "#";
wenzelm@14645
  1131
val add_nonterminals     = extend_sign true ext_nonterminals "#";
wenzelm@14645
  1132
val add_tyabbrs          = extend_sign true ext_tyabbrs "#";
wenzelm@14645
  1133
val add_tyabbrs_i        = extend_sign true ext_tyabbrs_i "#";
wenzelm@14645
  1134
val add_arities          = extend_sign true ext_arities "#";
wenzelm@14645
  1135
val add_arities_i        = extend_sign true ext_arities_i "#";
wenzelm@14645
  1136
val add_consts           = extend_sign true ext_consts "#";
wenzelm@14645
  1137
val add_consts_i         = extend_sign true ext_consts_i "#";
wenzelm@14645
  1138
val add_syntax           = extend_sign true ext_syntax "#";
wenzelm@14645
  1139
val add_syntax_i         = extend_sign true ext_syntax_i "#";
wenzelm@14645
  1140
val add_modesyntax       = extend_sign true ext_modesyntax "#";
wenzelm@14645
  1141
val add_modesyntax_i     = extend_sign true ext_modesyntax_i "#";
wenzelm@14645
  1142
val add_trfuns           = extend_sign true ext_trfuns "#";
wenzelm@14645
  1143
val add_trfunsT          = extend_sign true ext_trfunsT "#";
wenzelm@14645
  1144
val add_advanced_trfuns  = extend_sign true ext_advanced_trfuns "#";
wenzelm@14645
  1145
val add_advanced_trfunsT = extend_sign true ext_advanced_trfunsT "#";
wenzelm@14645
  1146
val add_tokentrfuns      = extend_sign true ext_tokentrfuns "#";
wenzelm@14645
  1147
val add_trrules          = extend_sign true ext_trrules "#";
wenzelm@14645
  1148
val add_trrules_i        = extend_sign true ext_trrules_i "#";
wenzelm@14645
  1149
val add_path             = extend_sign true ext_path "#";
wenzelm@14645
  1150
val add_space            = extend_sign true ext_add_space "#";
wenzelm@14645
  1151
val hide_space           = curry (extend_sign true ext_hide_space "#");
wenzelm@14645
  1152
val hide_space_i         = curry (extend_sign true ext_hide_space_i "#");
wenzelm@14645
  1153
val init_data            = extend_sign true ext_init_data "#";
wenzelm@14645
  1154
fun put_data k f x       = extend_sign true ext_put_data "#" (k, f, x);
wenzelm@14645
  1155
fun add_name name        = extend_sign true (K K) name ();
wenzelm@14645
  1156
val prep_ext             = extend_sign false (K K) "#" ();
wenzelm@386
  1157
wenzelm@386
  1158
wenzelm@3867
  1159
wenzelm@10404
  1160
(** merge signatures **)        (*exception TERM*)
wenzelm@3867
  1161
wenzelm@4228
  1162
(* merge_stamps *)
wenzelm@4228
  1163
wenzelm@4228
  1164
fun merge_stamps stamps1 stamps2 =
wenzelm@12296
  1165
  let val stamps = merge_lists' stamps1 stamps2 in
wenzelm@4228
  1166
    (case duplicates (map ! stamps) of
wenzelm@4228
  1167
      [] => stamps
wenzelm@4228
  1168
    | dups => raise TERM ("Attempt to merge different versions of theories "
wenzelm@4228
  1169
        ^ commas_quote dups, []))
wenzelm@4228
  1170
  end;
wenzelm@4228
  1171
wenzelm@4228
  1172
wenzelm@14645
  1173
(* trivial merge *)
wenzelm@3877
  1174
wenzelm@4228
  1175
fun merge_refs (sgr1 as SgRef (Some (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
wenzelm@4228
  1176
        sgr2 as SgRef (Some (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
wenzelm@3967
  1177
      if fast_subsig (sg2, sg1) then sgr1
wenzelm@3967
  1178
      else if fast_subsig (sg1, sg2) then sgr2
wenzelm@3967
  1179
      else if subsig (sg2, sg1) then sgr1
wenzelm@3967
  1180
      else if subsig (sg1, sg2) then sgr2
wenzelm@4228
  1181
      else (merge_stamps s1 s2; (*check for different versions*)
wenzelm@4228
  1182
        raise TERM ("Attempt to do non-trivial merge of signatures", []))
wenzelm@3967
  1183
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
  1184
wenzelm@4627
  1185
val merge = deref o merge_refs o pairself self_ref;
wenzelm@4627
  1186
wenzelm@3867
  1187
wenzelm@14645
  1188
(* non-trivial merge *)              (*exception TERM/ERROR*)
wenzelm@10932
  1189
wenzelm@6961
  1190
fun nontriv_merge (sg1, sg2) =
wenzelm@3967
  1191
  if subsig (sg2, sg1) then sg1
wenzelm@251
  1192
  else if subsig (sg1, sg2) then sg2
wenzelm@386
  1193
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@3967
  1194
    raise TERM ("Attempt to merge draft signatures", [])
wenzelm@10932
  1195
  else if exists_stamp PureN sg1 andalso exists_stamp CPureN sg2 orelse
wenzelm@10932
  1196
      exists_stamp CPureN sg1 andalso exists_stamp PureN sg2 then
wenzelm@10932
  1197
    raise TERM ("Cannot merge Pure and CPure signatures", [])
wenzelm@251
  1198
  else
wenzelm@251
  1199
    let
wenzelm@14645
  1200
      val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
wenzelm@14645
  1201
          {self = _, tsig = tsig1, const_tab = const_tab1,
wenzelm@14645
  1202
            path = _, spaces = spaces1, data = data1}) = sg1;
wenzelm@14645
  1203
      val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
wenzelm@14645
  1204
          {self = _, tsig = tsig2, const_tab = const_tab2,
wenzelm@14645
  1205
            path = _, spaces = spaces2, data = data2}) = sg2;
wenzelm@386
  1206
wenzelm@3967
  1207
      val id = ref "";
wenzelm@14645
  1208
      val self_ref = ref dummy_sg;
wenzelm@3967
  1209
      val self = SgRef (Some self_ref);
wenzelm@402
  1210
wenzelm@4228
  1211
      val stamps = merge_stamps stamps1 stamps2;
wenzelm@14645
  1212
      val syntax = Syntax.merge_syntaxes syntax1 syntax2;
wenzelm@14645
  1213
      val trfuns = merge_trfuns trfuns1 trfuns2;
wenzelm@2185
  1214
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
  1215
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
  1216
        handle Symtab.DUPS cs =>
wenzelm@3791
  1217
          raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
wenzelm@3791
  1218
wenzelm@11501
  1219
      val path = Some [];
wenzelm@3791
  1220
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
  1221
      val spaces =
wenzelm@3791
  1222
        kinds ~~
wenzelm@3791
  1223
          ListPair.map NameSpace.merge
wenzelm@3791
  1224
            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
wenzelm@3867
  1225
wenzelm@4256
  1226
      val data = merge_data (data1, data2);
wenzelm@3967
  1227
wenzelm@14645
  1228
      val sign = make_sign (id, self, tsig, const_tab, Syn (syntax, trfuns),
wenzelm@14645
  1229
        path, spaces, data, stamps);
wenzelm@14645
  1230
    in self_ref := sign; syn_of sign; sign end;
wenzelm@3791
  1231
clasohm@0
  1232
end;