src/Pure/sign.ML
author schirmer
Mon May 03 23:22:17 2004 +0200 (2004-05-03)
changeset 14700 2f885b7e5ba7
parent 14688 edb7dacde656
child 14784 e65d77313a94
permissions -rw-r--r--
reimplementation of HOL records; only one type is created for
each record extension, instead of one type for each field. See NEWS.
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
schirmer@14700
    64
  val extern_typ: sg -> typ -> typ
wenzelm@3805
    65
  val intern_class: sg -> xclass -> class
wenzelm@3805
    66
  val intern_tycon: sg -> xstring -> string
wenzelm@3805
    67
  val intern_const: sg -> xstring -> string
wenzelm@3937
    68
  val intern_sort: sg -> xsort -> sort
wenzelm@3937
    69
  val intern_typ: sg -> xtyp -> typ
wenzelm@3937
    70
  val intern_term: sg -> xterm -> term
wenzelm@3937
    71
  val intern_tycons: sg -> xtyp -> typ
paulson@1501
    72
  val pretty_sg: sg -> Pretty.T
wenzelm@4051
    73
  val str_of_sg: sg -> string
paulson@1501
    74
  val pprint_sg: sg -> pprint_args -> unit
wenzelm@14645
    75
  val PureN: string
wenzelm@14645
    76
  val CPureN: string
wenzelm@14645
    77
  val pre_pure: sg
paulson@1501
    78
  val pretty_term: sg -> term -> Pretty.T
wenzelm@12068
    79
  val pretty_term': Syntax.syntax -> sg -> term -> Pretty.T
paulson@1501
    80
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    81
  val pretty_sort: sg -> sort -> Pretty.T
wenzelm@4249
    82
  val pretty_classrel: sg -> class * class -> Pretty.T
wenzelm@4249
    83
  val pretty_arity: sg -> string * sort list * sort -> Pretty.T
paulson@1501
    84
  val string_of_term: sg -> term -> string
paulson@1501
    85
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    86
  val string_of_sort: sg -> sort -> string
wenzelm@3855
    87
  val str_of_sort: sg -> sort -> string
wenzelm@3855
    88
  val str_of_classrel: sg -> class * class -> string
wenzelm@3855
    89
  val str_of_arity: sg -> string * sort list * sort -> string
paulson@1501
    90
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    91
  val pprint_typ: sg -> typ -> pprint_args -> unit
wenzelm@8898
    92
  val certify_class: sg -> class -> class
wenzelm@8898
    93
  val certify_sort: sg -> sort -> sort
paulson@1501
    94
  val certify_typ: sg -> typ -> typ
wenzelm@9504
    95
  val certify_typ_no_norm: sg -> typ -> typ
wenzelm@10443
    96
  val certify_tycon: sg -> string -> string
wenzelm@10443
    97
  val certify_tyabbr: sg -> string -> string
wenzelm@11720
    98
  val certify_tyname: sg -> string -> string
wenzelm@10443
    99
  val certify_const: sg -> string -> string
paulson@1501
   100
  val certify_term: sg -> term -> term * typ * int
wenzelm@8898
   101
  val read_sort: sg -> string -> sort
wenzelm@4227
   102
  val read_raw_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
   103
  val read_typ: sg * (indexname -> sort option) -> string -> typ
wenzelm@12068
   104
  val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@12068
   105
  val read_typ_no_norm': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
paulson@1501
   106
  val infer_types: sg -> (indexname -> typ option) ->
paulson@1501
   107
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   108
    -> xterm list * typ -> term * (indexname * typ) list
wenzelm@4249
   109
  val infer_types_simult: sg -> (indexname -> typ option) ->
wenzelm@4249
   110
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   111
    -> (xterm list * typ) list -> term list * (indexname * typ) list
wenzelm@12068
   112
  val read_def_terms':
wenzelm@12068
   113
    Syntax.syntax -> sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@12068
   114
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8607
   115
  val read_def_terms:
wenzelm@8607
   116
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@8607
   117
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8802
   118
  val simple_read_term: sg -> typ -> string -> term
wenzelm@14645
   119
  val const_of_class: class -> string
wenzelm@14645
   120
  val class_of_const: string -> class
wenzelm@3956
   121
  val add_classes: (bclass * xclass list) list -> sg -> sg
wenzelm@3956
   122
  val add_classes_i: (bclass * class list) list -> sg -> sg
wenzelm@3805
   123
  val add_classrel: (xclass * xclass) list -> sg -> sg
wenzelm@3791
   124
  val add_classrel_i: (class * class) list -> sg -> sg
wenzelm@8898
   125
  val add_defsort: string -> sg -> sg
wenzelm@3791
   126
  val add_defsort_i: sort -> sg -> sg
wenzelm@3956
   127
  val add_types: (bstring * int * mixfix) list -> sg -> sg
wenzelm@4844
   128
  val add_nonterminals: bstring list -> sg -> sg
wenzelm@3956
   129
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
wenzelm@3956
   130
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
wenzelm@8898
   131
  val add_arities: (xstring * string list * string) list -> sg -> sg
wenzelm@3791
   132
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
wenzelm@3956
   133
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   134
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   135
  val add_syntax: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   136
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   137
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   138
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
paulson@1501
   139
  val add_trfuns:
wenzelm@4344
   140
    (string * (ast list -> ast)) list *
wenzelm@4344
   141
    (string * (term list -> term)) list *
wenzelm@4344
   142
    (string * (term list -> term)) list *
wenzelm@4344
   143
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
   144
  val add_trfunsT:
wenzelm@4344
   145
    (string * (bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@14645
   146
  val add_advanced_trfuns:
wenzelm@14645
   147
    (string * (sg -> ast list -> ast)) list *
wenzelm@14645
   148
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   149
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   150
    (string * (sg -> ast list -> ast)) list -> sg -> sg
wenzelm@14645
   151
  val add_advanced_trfunsT:
wenzelm@14645
   152
    (string * (sg -> bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
   153
  val add_tokentrfuns:
wenzelm@6311
   154
    (string * string * (string -> string * real)) list -> sg -> sg
wenzelm@4619
   155
  val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
paulson@1810
   156
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
   157
  val add_path: string -> sg -> sg
wenzelm@3810
   158
  val add_space: string * string list -> sg -> sg
wenzelm@12588
   159
  val hide_space: bool -> string * string list -> sg -> sg
wenzelm@12588
   160
  val hide_space_i: bool -> string * string list -> sg -> sg
paulson@1501
   161
  val add_name: string -> sg -> sg
wenzelm@4256
   162
  val data_kinds: data -> string list
wenzelm@3967
   163
  val merge_refs: sg_ref * sg_ref -> sg_ref
wenzelm@4627
   164
  val merge: sg * sg -> sg
wenzelm@12123
   165
  val copy: sg -> sg
wenzelm@3975
   166
  val prep_ext: sg -> sg
wenzelm@4627
   167
  val nontriv_merge: sg * sg -> sg
wenzelm@3791
   168
end;
clasohm@0
   169
wenzelm@6191
   170
signature PRIVATE_SIGN =
wenzelm@5642
   171
sig
wenzelm@5642
   172
  include SIGN
wenzelm@6546
   173
  val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
wenzelm@12310
   174
    (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
wenzelm@5642
   175
  val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
wenzelm@5642
   176
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
wenzelm@5642
   177
  val print_data: Object.kind -> sg -> unit
wenzelm@5642
   178
end;
wenzelm@5642
   179
wenzelm@6191
   180
structure Sign: PRIVATE_SIGN =
wenzelm@143
   181
struct
clasohm@0
   182
wenzelm@251
   183
(** datatype sg **)
wenzelm@251
   184
wenzelm@14645
   185
(* types sg, data, syn, sg_ref *)
wenzelm@4256
   186
wenzelm@19
   187
datatype sg =
wenzelm@3975
   188
  Sg of
wenzelm@3975
   189
   {id: string ref,                             (*id*)
wenzelm@14645
   190
    stamps: string ref list,                    (*unique theory indentifier*)
wenzelm@14645
   191
    syn: syn} *                                 (*syntax for parsing and printing*)
wenzelm@3975
   192
   {self: sg_ref,                               (*mutable self reference*)
wenzelm@3791
   193
    tsig: Type.type_sig,                        (*order-sorted signature of types*)
wenzelm@3805
   194
    const_tab: typ Symtab.table,                (*type schemes of constants*)
wenzelm@11501
   195
    path: string list option,                   (*current name space entry prefix*)
wenzelm@3975
   196
    spaces: (string * NameSpace.T) list,        (*name spaces for consts, types etc.*)
wenzelm@4256
   197
    data: data}                                 (*anytype data*)
wenzelm@4256
   198
and data =
wenzelm@4256
   199
  Data of
wenzelm@10404
   200
    (Object.kind *                              (*kind (for authorization)*)
wenzelm@10404
   201
      (Object.T *                               (*value*)
wenzelm@12123
   202
        ((Object.T -> Object.T) *               (*copy method*)
wenzelm@12123
   203
          (Object.T -> Object.T) *              (*prepare extend method*)
wenzelm@4998
   204
          (Object.T * Object.T -> Object.T) *   (*merge and prepare extend method*)
wenzelm@4998
   205
          (sg -> Object.T -> unit))))           (*print method*)
wenzelm@4256
   206
    Symtab.table
wenzelm@14645
   207
and syn =
wenzelm@14645
   208
  Syn of
wenzelm@14645
   209
    Syntax.syntax *
wenzelm@14645
   210
     (((sg -> ast list -> ast) * stamp) Symtab.table *
wenzelm@14645
   211
      ((sg -> term list -> term) * stamp) Symtab.table *
wenzelm@14645
   212
      ((sg -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
wenzelm@14645
   213
      ((sg -> ast list -> ast) * stamp) list Symtab.table)
wenzelm@3967
   214
and sg_ref =
wenzelm@4998
   215
  SgRef of sg ref option;
clasohm@0
   216
wenzelm@3967
   217
(*make signature*)
wenzelm@3967
   218
fun make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps) =
wenzelm@14645
   219
  Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
wenzelm@14645
   220
    const_tab = const_tab, path = path, spaces = spaces, data = data});
wenzelm@3975
   221
wenzelm@3975
   222
wenzelm@4256
   223
(* basic operations *)
wenzelm@3975
   224
wenzelm@3975
   225
fun rep_sg (Sg (_, args)) = args;
wenzelm@3967
   226
wenzelm@3975
   227
fun stamp_names_of (Sg ({stamps, ...}, _)) = rev (map ! stamps);
wenzelm@10932
   228
fun exists_stamp name (Sg ({stamps, ...}, _)) = exists (equal name o !) stamps;
wenzelm@3975
   229
fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg);
wenzelm@3975
   230
val str_of_sg = Pretty.str_of o pretty_sg;
wenzelm@3975
   231
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@3975
   232
wenzelm@402
   233
val tsig_of = #tsig o rep_sg;
wenzelm@3975
   234
fun const_type (Sg (_, {const_tab, ...})) c = Symtab.lookup (const_tab, 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, ...},
wenzelm@3975
   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
wenzelm@3995
   249
fun deref (SgRef (Some (ref sg))) = sg
wenzelm@3995
   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@4568
   337
val norm_sort = Type.norm_sort o tsig_of;
wenzelm@7640
   338
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   339
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@7640
   340
val univ_witness = Type.univ_witness o tsig_of;
wenzelm@10443
   341
fun typ_instance sg (T, U) = Type.typ_instance (tsig_of sg, T, U);
wenzelm@4568
   342
wenzelm@4256
   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@10404
   360
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^
wenzelm@10404
   361
         of_theory sg);
wenzelm@4256
   362
paulson@6040
   363
(*Trying to access theory data using get / put operations from a different
paulson@6040
   364
  instance of the TheoryDataFun result.  Typical cure: re-load all files*)
wenzelm@4998
   365
fun err_access sg kind =
wenzelm@4998
   366
  error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4998
   367
wenzelm@4256
   368
wenzelm@4256
   369
(* prepare data *)
wenzelm@4256
   370
wenzelm@4489
   371
val empty_data = Data Symtab.empty;
wenzelm@4256
   372
wenzelm@4256
   373
fun merge_data (Data tab1, Data tab2) =
wenzelm@4256
   374
  let
wenzelm@4998
   375
    val data1 = map snd (Symtab.dest tab1);
wenzelm@4998
   376
    val data2 = map snd (Symtab.dest tab2);
wenzelm@4256
   377
    val all_data = data1 @ data2;
wenzelm@4998
   378
    val kinds = gen_distinct Object.eq_kind (map fst all_data);
wenzelm@4256
   379
wenzelm@4256
   380
   fun entry data kind =
wenzelm@4998
   381
     (case gen_assoc Object.eq_kind (data, kind) of
wenzelm@4256
   382
       None => []
wenzelm@4256
   383
     | Some x => [(kind, x)]);
wenzelm@4256
   384
wenzelm@12310
   385
    fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
wenzelm@6961
   386
          (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
wenzelm@12310
   387
      | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
wenzelm@6961
   388
          (kind, (mrg (e1, e2)
wenzelm@6961
   389
            handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
wenzelm@4256
   390
      | merge_entries _ = sys_error "merge_entries";
wenzelm@4256
   391
wenzelm@4256
   392
    val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
wenzelm@4998
   393
    val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
wenzelm@4998
   394
  in
wenzelm@4998
   395
    Data (Symtab.make data_idx)
wenzelm@4998
   396
      handle Symtab.DUPS dups => err_inconsistent dups
wenzelm@4998
   397
  end;
wenzelm@4256
   398
wenzelm@4256
   399
fun prep_ext_data data = merge_data (data, empty_data);
wenzelm@4256
   400
wenzelm@12310
   401
fun init_data_sg sg (Data tab) kind e cp ext mrg prt =
wenzelm@4998
   402
  let val name = Object.name_of_kind kind in
wenzelm@12310
   403
    Data (Symtab.update_new ((name, (kind, (e, (cp, ext, mrg, prt)))), tab))
wenzelm@4998
   404
      handle Symtab.DUP _ => err_dup_init sg name
wenzelm@4998
   405
  end;
wenzelm@4256
   406
wenzelm@4256
   407
wenzelm@4256
   408
(* access data *)
wenzelm@4256
   409
wenzelm@4256
   410
fun data_kinds (Data tab) = map fst (Symtab.dest tab);
wenzelm@4256
   411
wenzelm@4256
   412
fun lookup_data sg tab kind =
wenzelm@4998
   413
  let val name = Object.name_of_kind kind in
wenzelm@4998
   414
    (case Symtab.lookup (tab, name) of
wenzelm@4998
   415
      Some (k, x) =>
wenzelm@4998
   416
        if Object.eq_kind (kind, k) then x
wenzelm@4998
   417
        else err_access sg name
wenzelm@4998
   418
    | None => err_uninit sg name)
wenzelm@4998
   419
  end;
wenzelm@4256
   420
wenzelm@4998
   421
fun get_data kind f (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@4998
   422
  let val x = fst (lookup_data sg tab kind)
wenzelm@4998
   423
  in f x handle Match => Object.kind_error kind end;
wenzelm@4998
   424
wenzelm@4998
   425
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@12310
   426
  let val (e, (_, _, _, prt)) = lookup_data sg tab kind
wenzelm@6961
   427
  in prt sg e handle exn => err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn end;
wenzelm@4256
   428
wenzelm@4998
   429
fun put_data_sg sg (Data tab) kind f x =
wenzelm@4998
   430
  Data (Symtab.update ((Object.name_of_kind kind,
wenzelm@4998
   431
    (kind, (f x, snd (lookup_data sg tab kind)))), tab));
wenzelm@4256
   432
wenzelm@4256
   433
wenzelm@4256
   434
wenzelm@4256
   435
(** build signatures **)
wenzelm@3967
   436
wenzelm@3967
   437
fun ext_stamps stamps (id as ref name) =
wenzelm@3967
   438
  let val stmps = (case stamps of ref "#" :: ss => ss | ss => ss) in
wenzelm@3967
   439
    if exists (equal name o !) stmps then
wenzelm@3967
   440
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@3967
   441
    else id :: stmps
wenzelm@3967
   442
  end;
wenzelm@3967
   443
wenzelm@3967
   444
fun create_sign self stamps name (syn, tsig, ctab, (path, spaces), data) =
wenzelm@3967
   445
  let
wenzelm@3967
   446
    val id = ref name;
wenzelm@3967
   447
    val sign =
wenzelm@3967
   448
      make_sign (id, self, tsig, ctab, syn, path, spaces, data, ext_stamps stamps id);
wenzelm@3967
   449
  in
wenzelm@3967
   450
    (case self of
wenzelm@3967
   451
      SgRef (Some r) => r := sign
wenzelm@3967
   452
    | _ => sys_error "Sign.create_sign");
wenzelm@3967
   453
    sign
wenzelm@3967
   454
  end;
wenzelm@3967
   455
wenzelm@3975
   456
fun extend_sign keep extfun name decls
wenzelm@14645
   457
    (sg as Sg ({id = _, stamps, syn}, {self, tsig, const_tab, path, spaces, data})) =
wenzelm@3967
   458
  let
wenzelm@3967
   459
    val _ = check_stale sg;
wenzelm@3967
   460
    val (self', data') =
wenzelm@3975
   461
      if is_draft sg andalso keep then (self, data)
wenzelm@4256
   462
      else (SgRef (Some (ref sg)), prep_ext_data data);
wenzelm@3967
   463
  in
wenzelm@3967
   464
    create_sign self' stamps name
wenzelm@14645
   465
      (extfun sg (syn, tsig, const_tab, (path, spaces), data') decls)
wenzelm@3967
   466
  end;
wenzelm@3967
   467
wenzelm@3967
   468
wenzelm@3791
   469
wenzelm@3791
   470
(** name spaces **)
wenzelm@3791
   471
wenzelm@3791
   472
(* kinds *)
wenzelm@3791
   473
wenzelm@3791
   474
val classK = "class";
wenzelm@3791
   475
val typeK = "type";
wenzelm@3791
   476
val constK = "const";
wenzelm@3791
   477
wenzelm@3791
   478
wenzelm@8725
   479
(* declare and retrieve names *)
wenzelm@3791
   480
wenzelm@3791
   481
fun space_of spaces kind =
wenzelm@3791
   482
  if_none (assoc (spaces, kind)) NameSpace.empty;
wenzelm@3791
   483
wenzelm@3791
   484
(*input and output of qualified names*)
wenzelm@3995
   485
fun intrn spaces kind = NameSpace.intern (space_of spaces kind);
wenzelm@3995
   486
fun extrn spaces kind = NameSpace.extern (space_of spaces kind);
wenzelm@5175
   487
fun cond_extrn spaces kind = NameSpace.cond_extern (space_of spaces kind);
wenzelm@6845
   488
fun cond_extrn_table spaces kind tab = NameSpace.cond_extern_table (space_of spaces kind) tab;
wenzelm@3791
   489
wenzelm@8725
   490
(*add / hide names*)
wenzelm@8725
   491
fun change_space f spaces kind x = overwrite (spaces, (kind, f (space_of spaces kind, x)));
wenzelm@8730
   492
fun add_names x = change_space NameSpace.extend x;
wenzelm@12588
   493
fun hide_names b x = change_space (NameSpace.hide b) x;
wenzelm@3791
   494
wenzelm@3810
   495
(*make full names*)
wenzelm@11501
   496
fun full _ "" = error "Attempt to declare empty name \"\""
wenzelm@11501
   497
  | full None name = name
wenzelm@11501
   498
  | full (Some path) name =
wenzelm@11501
   499
      if NameSpace.is_qualified name then
wenzelm@11501
   500
        error ("Attempt to declare qualified name " ^ quote name)
wenzelm@14688
   501
      else
wenzelm@14688
   502
       (if not (Syntax.is_identifier name)
wenzelm@14688
   503
        then warning ("Declared non-identifier name " ^ quote name) else ();
wenzelm@14688
   504
        NameSpace.pack (path @ [name]));
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
wenzelm@3791
   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 =
wenzelm@11501
   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
wenzelm@14645
   582
val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0,
wenzelm@14645
   583
  Symtab.empty, pure_syn, Some [], [], empty_data, []);
wenzelm@14645
   584
wenzelm@14645
   585
val pre_pure =
wenzelm@14645
   586
  create_sign (SgRef (Some (ref dummy_sg))) [] "#"
wenzelm@14645
   587
    (pure_syn, Type.tsig0, 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@12068
   601
fun pretty_term sg = pretty_term' (syn_of sg) sg;
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@3937
   610
fun pretty_classrel sg (c1, c2) = Pretty.block
wenzelm@3937
   611
  [pretty_sort sg [c1], Pretty.str " <", Pretty.brk 1, pretty_sort sg [c2]];
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@3937
   624
fun string_of_term sg t = Pretty.string_of (pretty_term sg t);
wenzelm@3937
   625
fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T);
wenzelm@3937
   626
fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S);
wenzelm@3937
   627
wenzelm@3937
   628
fun str_of_sort sg S = Pretty.str_of (pretty_sort sg S);
wenzelm@3937
   629
fun str_of_classrel sg c1_c2 = Pretty.str_of (pretty_classrel sg c1_c2);
wenzelm@3937
   630
fun str_of_arity sg ar = Pretty.str_of (pretty_arity sg ar);
wenzelm@3937
   631
wenzelm@3937
   632
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@3937
   633
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
wenzelm@3937
   634
wenzelm@3937
   635
wenzelm@3937
   636
wenzelm@8898
   637
(** read sorts **)  (*exception ERROR*)
wenzelm@8898
   638
wenzelm@8898
   639
fun err_in_sort s =
wenzelm@8898
   640
  error ("The error(s) above occurred in sort " ^ quote s);
wenzelm@8898
   641
wenzelm@14645
   642
fun rd_sort sg syn tsig spaces str =
wenzelm@14645
   643
  let val S = intrn_sort spaces (Syntax.read_sort (make_syntax sg syn) str
wenzelm@14645
   644
    handle ERROR => err_in_sort str)
wenzelm@8898
   645
  in Type.cert_sort tsig S handle TYPE (msg, _, _) => (error_msg msg; err_in_sort str) end;
wenzelm@8898
   646
wenzelm@8898
   647
(*read and certify sort wrt a signature*)
wenzelm@14645
   648
fun read_sort (sg as Sg ({syn, ...}, {tsig, spaces, ...})) str =
wenzelm@14645
   649
  (check_stale sg; rd_sort sg syn tsig spaces str);
wenzelm@8898
   650
wenzelm@14645
   651
fun cert_sort _ _ tsig _ = Type.cert_sort tsig;
wenzelm@8898
   652
wenzelm@8898
   653
wenzelm@8898
   654
wenzelm@562
   655
(** read types **)  (*exception ERROR*)
wenzelm@562
   656
wenzelm@562
   657
fun err_in_type s =
wenzelm@562
   658
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   659
wenzelm@4227
   660
fun rd_raw_typ syn tsig spaces def_sort str =
wenzelm@3791
   661
  intrn_tycons spaces
wenzelm@12313
   662
    (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) (intrn_sort spaces) str
wenzelm@3791
   663
      handle ERROR => err_in_type str);
wenzelm@4227
   664
wenzelm@12068
   665
fun read_raw_typ' syn (sg as Sg (_, {tsig, spaces, ...}), def_sort) str =
wenzelm@4227
   666
  (check_stale sg; rd_raw_typ syn tsig spaces def_sort str);
wenzelm@4227
   667
wenzelm@12068
   668
fun read_raw_typ (sg, def_sort) = read_raw_typ' (syn_of sg) (sg, def_sort);
wenzelm@12068
   669
wenzelm@562
   670
(*read and certify typ wrt a signature*)
wenzelm@9504
   671
local
wenzelm@12068
   672
  fun read_typ_aux rd cert (sg, def_sort) str =
wenzelm@12068
   673
    (cert (tsig_of sg) (rd (sg, def_sort) str)
wenzelm@12068
   674
      handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
wenzelm@9504
   675
in
wenzelm@12068
   676
  val read_typ              = read_typ_aux read_raw_typ Type.cert_typ;
wenzelm@12068
   677
  val read_typ_no_norm      = read_typ_aux read_raw_typ Type.cert_typ_no_norm;
wenzelm@12068
   678
  fun read_typ' syn         = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
wenzelm@12068
   679
  fun read_typ_no_norm' syn = read_typ_aux (read_raw_typ' syn) Type.cert_typ_no_norm;
wenzelm@9504
   680
end;
wenzelm@562
   681
wenzelm@562
   682
wenzelm@562
   683
wenzelm@8898
   684
(** certify classes, sorts, types and terms **)   (*exception TYPE*)
wenzelm@251
   685
wenzelm@8898
   686
val certify_class = Type.cert_class o tsig_of;
wenzelm@8898
   687
val certify_sort = Type.cert_sort o tsig_of;
wenzelm@3975
   688
val certify_typ = Type.cert_typ o tsig_of;
wenzelm@9504
   689
val certify_typ_no_norm = Type.cert_typ_no_norm o tsig_of;
wenzelm@10443
   690
wenzelm@10443
   691
fun certify_tycon sg c =
wenzelm@10443
   692
  if is_some (Symtab.lookup (#tycons (Type.rep_tsig (tsig_of sg)), c)) then c
wenzelm@10443
   693
  else raise TYPE ("Undeclared type constructor " ^ quote c, [], []);
wenzelm@10443
   694
wenzelm@10443
   695
fun certify_tyabbr sg c =
wenzelm@10443
   696
  if is_some (Symtab.lookup (#abbrs (Type.rep_tsig (tsig_of sg)), c)) then c
wenzelm@10443
   697
  else raise TYPE ("Unknown type abbreviation " ^ quote c, [], []);
wenzelm@10443
   698
wenzelm@11720
   699
fun certify_tyname sg c =
wenzelm@11720
   700
  certify_tycon sg c handle TYPE _ => certify_tyabbr sg c handle TYPE _ =>
wenzelm@11720
   701
    raise TYPE ("Unknown type name " ^ quote c, [], []);
wenzelm@11720
   702
wenzelm@10443
   703
fun certify_const sg c =
wenzelm@10443
   704
  if is_some (const_type sg c) then c else raise TYPE ("Undeclared constant " ^ quote c, [], []);
wenzelm@251
   705
wenzelm@4961
   706
wenzelm@4961
   707
(* certify_term *)
wenzelm@4961
   708
wenzelm@8290
   709
(*check for duplicate occurrences of TFree/TVar with distinct sorts*)
wenzelm@8290
   710
fun nodup_tvars (env, Type (_, Ts)) = nodup_tvars_list (env, Ts)
wenzelm@8290
   711
  | nodup_tvars (env as (tfrees, tvars), T as TFree (v as (a, S))) =
wenzelm@8290
   712
      (case assoc_string (tfrees, a) of
wenzelm@8290
   713
        Some S' =>
wenzelm@8290
   714
          if S = S' then env
wenzelm@8290
   715
          else raise TYPE ("Type variable " ^ quote a ^
wenzelm@8290
   716
            " has two distinct sorts", [TFree (a, S'), T], [])
wenzelm@8290
   717
      | None => (v :: tfrees, tvars))
wenzelm@8290
   718
  | nodup_tvars (env as (tfrees, tvars), T as TVar (v as (a, S))) =
wenzelm@2979
   719
      (case assoc_string_int (tvars, a) of
wenzelm@2979
   720
        Some S' =>
wenzelm@8290
   721
          if S = S' then env
wenzelm@8290
   722
          else raise TYPE ("Type variable " ^ quote (Syntax.string_of_vname a) ^
wenzelm@3791
   723
            " has two distinct sorts", [TVar (a, S'), T], [])
wenzelm@8290
   724
      | None => (tfrees, v :: tvars))
wenzelm@8290
   725
(*equivalent to foldl nodup_tvars_list, but 3X faster under Poly/ML*)
wenzelm@8290
   726
and nodup_tvars_list (env, []) = env
wenzelm@8290
   727
  | nodup_tvars_list (env, T :: Ts) = nodup_tvars_list (nodup_tvars (env, T), Ts);
nipkow@1494
   728
wenzelm@8290
   729
(*check for duplicate occurrences of Free/Var with distinct types*)
wenzelm@8290
   730
fun nodup_vars tm =
wenzelm@2979
   731
  let
wenzelm@8290
   732
    fun nodups (envs as (env as (frees, vars), envT)) tm =
wenzelm@2979
   733
      (case tm of
wenzelm@8290
   734
        Const (c, T) => (env, nodup_tvars (envT, T))
wenzelm@8290
   735
      | Free (v as (a, T)) =>
wenzelm@8290
   736
          (case assoc_string (frees, a) of
wenzelm@8290
   737
            Some T' =>
wenzelm@8290
   738
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@8290
   739
              else raise TYPE ("Variable " ^ quote a ^
wenzelm@8290
   740
                " has two distinct types", [T', T], [])
wenzelm@8290
   741
          | None => ((v :: frees, vars), nodup_tvars (envT, T)))
wenzelm@2979
   742
      | Var (v as (ixn, T)) =>
wenzelm@2979
   743
          (case assoc_string_int (vars, ixn) of
wenzelm@2979
   744
            Some T' =>
wenzelm@8290
   745
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@8290
   746
              else raise TYPE ("Variable " ^ quote (Syntax.string_of_vname ixn) ^
wenzelm@3791
   747
                " has two distinct types", [T', T], [])
wenzelm@8290
   748
          | None => ((frees, v :: vars), nodup_tvars (envT, T)))
wenzelm@8290
   749
      | Bound _ => envs
wenzelm@8290
   750
      | Abs (_, T, t) => nodups (env, nodup_tvars (envT, T)) t
wenzelm@8290
   751
      | s $ t => nodups (nodups envs s) t)
wenzelm@8290
   752
  in nodups (([], []), ([], [])) tm; tm end;
wenzelm@2979
   753
wenzelm@4961
   754
(*compute and check type of the term*)
wenzelm@4961
   755
fun type_check sg tm =
wenzelm@4961
   756
  let
wenzelm@8725
   757
    val prt = setmp Syntax.show_brackets true (pretty_term sg);
wenzelm@8725
   758
    val prT = pretty_typ sg;
wenzelm@251
   759
wenzelm@4961
   760
    fun err_appl why bs t T u U =
wenzelm@4961
   761
      let
wenzelm@10404
   762
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   763
        val t' = subst_bounds (xs, t);
wenzelm@4961
   764
        val u' = subst_bounds (xs, u);
wenzelm@10404
   765
        val text = cat_lines (TypeInfer.appl_error prt prT why t' T u' U);
wenzelm@4961
   766
      in raise TYPE (text, [T, U], [t', u']) end;
wenzelm@4961
   767
wenzelm@4961
   768
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   769
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   770
      | typ_of (_, Var (_, T)) = T
wenzelm@4961
   771
      | typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ =>
wenzelm@4961
   772
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   773
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   774
      | typ_of (bs, t $ u) =
wenzelm@4961
   775
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   776
            (case T of
wenzelm@4961
   777
              Type ("fun", [T1, T2]) =>
wenzelm@4961
   778
                if T1 = U then T2 else err_appl "Incompatible operand type." bs t T u U
wenzelm@4961
   779
            | _ => err_appl "Operator not of function type." bs t T u U)
wenzelm@4961
   780
          end;
wenzelm@4961
   781
wenzelm@4961
   782
  in typ_of ([], tm) end;
clasohm@0
   783
wenzelm@2979
   784
wenzelm@3975
   785
fun certify_term sg tm =
wenzelm@251
   786
  let
wenzelm@3969
   787
    val _ = check_stale sg;
wenzelm@3975
   788
    val tsig = tsig_of sg;
wenzelm@3969
   789
wenzelm@3975
   790
    fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T);
wenzelm@169
   791
wenzelm@4961
   792
    fun atom_err (errs, Const (a, T)) =
wenzelm@3975
   793
        (case const_type sg a of
wenzelm@4961
   794
          None => ("Undeclared constant " ^ show_const a T) :: errs
wenzelm@3975
   795
        | Some U =>
wenzelm@10404
   796
            if typ_instance sg (T, U) then errs
wenzelm@4961
   797
            else ("Illegal type for constant " ^ show_const a T) :: errs)
wenzelm@4961
   798
      | atom_err (errs, Var ((x, i), _)) =
wenzelm@4961
   799
          if i < 0 then ("Negative index for Var " ^ quote x) :: errs else errs
wenzelm@4961
   800
      | atom_err (errs, _) = errs;
wenzelm@251
   801
wenzelm@251
   802
    val norm_tm =
wenzelm@2185
   803
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@7068
   804
        [] => Type.norm_term tsig tm
wenzelm@3791
   805
      | errs => raise TYPE (cat_lines errs, [], [tm]));
wenzelm@8290
   806
    val _ = nodup_vars norm_tm;
wenzelm@251
   807
  in
wenzelm@4961
   808
    (case foldl_aterms atom_err ([], norm_tm) of
wenzelm@4961
   809
      [] => (norm_tm, type_check sg norm_tm, maxidx_of_term norm_tm)
wenzelm@3791
   810
    | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
wenzelm@251
   811
  end;
wenzelm@251
   812
wenzelm@251
   813
wenzelm@583
   814
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   815
wenzelm@2979
   816
(*
wenzelm@2979
   817
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   818
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   819
  used: list of already used type variables
wenzelm@2979
   820
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   821
wenzelm@4249
   822
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   823
  typs: expected types
wenzelm@2979
   824
*)
wenzelm@2979
   825
wenzelm@4249
   826
fun infer_types_simult sg def_type def_sort used freeze args =
wenzelm@251
   827
  let
wenzelm@3975
   828
    val tsig = tsig_of sg;
wenzelm@8725
   829
    val prt = setmp Syntax.show_brackets true (pretty_term sg);
wenzelm@8725
   830
    val prT = pretty_typ sg;
wenzelm@4249
   831
wenzelm@4249
   832
    val termss = foldr multiply (map fst args, [[]]);
wenzelm@4249
   833
    val typs =
wenzelm@4249
   834
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   835
wenzelm@4249
   836
    fun infer ts = OK
wenzelm@4249
   837
      (Type.infer_types prt prT tsig (const_type sg) def_type def_sort
wenzelm@4249
   838
        (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze typs ts)
wenzelm@4249
   839
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   840
wenzelm@4249
   841
    val err_results = map infer termss;
wenzelm@4249
   842
    val errs = mapfilter get_error err_results;
wenzelm@4249
   843
    val results = mapfilter get_ok err_results;
wenzelm@4249
   844
wenzelm@14645
   845
    val ambiguity = length termss;
wenzelm@14645
   846
wenzelm@4249
   847
    fun ambig_msg () =
wenzelm@4249
   848
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   849
      then
wenzelm@4249
   850
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   851
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   852
      else ();
wenzelm@4249
   853
  in
wenzelm@4249
   854
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   855
    else if length results = 1 then
wenzelm@4249
   856
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   857
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   858
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   859
      else (); hd results)
wenzelm@4249
   860
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
wenzelm@4249
   861
      (cat_lines (map (Pretty.string_of o prt) (flat (map fst results))))))
wenzelm@4249
   862
  end;
clasohm@623
   863
wenzelm@4249
   864
wenzelm@4249
   865
fun infer_types sg def_type def_sort used freeze tsT =
wenzelm@4249
   866
  apfst hd (infer_types_simult sg def_type def_sort used freeze [tsT]);
wenzelm@251
   867
wenzelm@251
   868
wenzelm@8607
   869
(** read_def_terms **)
wenzelm@8607
   870
wenzelm@8607
   871
(*read terms, infer types*)
wenzelm@12068
   872
fun read_def_terms' syn (sign, types, sorts) used freeze sTs =
wenzelm@8607
   873
  let
wenzelm@8607
   874
    fun read (s, T) =
wenzelm@8607
   875
      let val T' = certify_typ sign T handle TYPE (msg, _, _) => error msg
wenzelm@8607
   876
      in (Syntax.read syn T' s, T') end;
wenzelm@8607
   877
    val tsTs = map read sTs;
wenzelm@8607
   878
  in infer_types_simult sign types sorts used freeze tsTs end;
wenzelm@8607
   879
wenzelm@14645
   880
fun read_def_terms (sign, types, sorts) =
wenzelm@14645
   881
  read_def_terms' (syn_of sign) (sign, types, sorts);
wenzelm@12068
   882
wenzelm@8802
   883
fun simple_read_term sign T s =
wenzelm@8802
   884
  (read_def_terms (sign, K None, K None) [] true [(s, T)]
wenzelm@8802
   885
    handle ERROR => error ("The error(s) above occurred for " ^ s)) |> #1 |> hd;
wenzelm@8802
   886
wenzelm@8607
   887
wenzelm@2979
   888
clasohm@623
   889
(** extend signature **)    (*exception ERROR*)
clasohm@623
   890
wenzelm@620
   891
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   892
wenzelm@3791
   893
fun decls_of path name_of mfixs =
wenzelm@3810
   894
  map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs;
wenzelm@3791
   895
wenzelm@14645
   896
fun no_read _ _ _ _ decl = decl;
wenzelm@386
   897
wenzelm@386
   898
wenzelm@386
   899
(* add default sort *)
wenzelm@386
   900
wenzelm@14645
   901
fun ext_defS prep_sort sg (syn, tsig, ctab, (path, spaces), data) S =
wenzelm@14645
   902
  (syn, Type.ext_tsig_defsort tsig (prep_sort sg syn tsig spaces S),
wenzelm@14645
   903
    ctab, (path, spaces), data);
wenzelm@8898
   904
wenzelm@14645
   905
fun ext_defsort arg = ext_defS rd_sort arg;
paulson@8927
   906
fun ext_defsort_i arg = ext_defS cert_sort arg;
wenzelm@386
   907
wenzelm@386
   908
wenzelm@386
   909
(* add type constructors *)
wenzelm@386
   910
wenzelm@14645
   911
fun ext_types _ (syn, tsig, ctab, (path, spaces), data) types =
wenzelm@3791
   912
  let val decls = decls_of path Syntax.type_name types in
wenzelm@14645
   913
    (map_syntax (Syntax.extend_type_gram types) syn,
wenzelm@3791
   914
      Type.ext_tsig_types tsig decls, ctab,
wenzelm@3967
   915
      (path, add_names spaces typeK (map fst decls)), data)
wenzelm@3791
   916
  end;
wenzelm@386
   917
wenzelm@14645
   918
fun ext_nonterminals sign sg nonterms =
wenzelm@14645
   919
  ext_types sign sg (map (fn n => (n, 0, Syntax.NoSyn)) nonterms);
wenzelm@4844
   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) =
wenzelm@14645
   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@14645
   931
    val syn' = map_syntax (Syntax.extend_type_gram (map mfix_of abbrs)) syn;
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@386
   938
  in
wenzelm@3967
   939
    (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data)
wenzelm@386
   940
  end;
wenzelm@386
   941
wenzelm@3791
   942
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   943
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   944
wenzelm@386
   945
wenzelm@386
   946
(* add type arities *)
wenzelm@386
   947
wenzelm@14645
   948
fun ext_ars int prep_sort sg (syn, tsig, ctab, (path, spaces), data) arities =
wenzelm@386
   949
  let
wenzelm@14645
   950
    val prepS = prep_sort sg syn tsig spaces;
wenzelm@8898
   951
    fun prep_arity (c, Ss, S) =
wenzelm@8898
   952
      (if int then intrn spaces typeK c else c, map prepS Ss, prepS S);
wenzelm@8898
   953
    val tsig' = Type.ext_tsig_arities tsig (map prep_arity arities);
wenzelm@3791
   954
    val log_types = Type.logical_types tsig';
wenzelm@386
   955
  in
wenzelm@14645
   956
    (map_syntax (Syntax.extend_log_types log_types) syn,
wenzelm@14645
   957
      tsig', ctab, (path, spaces), data)
wenzelm@386
   958
  end;
wenzelm@386
   959
wenzelm@14645
   960
fun ext_arities arg = ext_ars true rd_sort arg;
paulson@8927
   961
fun ext_arities_i arg = ext_ars false cert_sort arg;
wenzelm@8898
   962
wenzelm@386
   963
wenzelm@386
   964
(* add term constants and syntax *)
wenzelm@386
   965
wenzelm@3805
   966
fun const_name path c mx =
wenzelm@3810
   967
  full path (Syntax.const_name c mx);
wenzelm@3805
   968
wenzelm@386
   969
fun err_in_const c =
wenzelm@386
   970
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   971
wenzelm@386
   972
fun err_dup_consts cs =
wenzelm@386
   973
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   974
wenzelm@251
   975
wenzelm@14645
   976
fun read_const sg syn tsig (path, spaces) (c, ty_src, mx) =
wenzelm@14645
   977
  (c, rd_raw_typ (make_syntax sg syn) tsig spaces (K None) ty_src, mx)
wenzelm@3805
   978
    handle ERROR => err_in_const (const_name path c mx);
wenzelm@386
   979
wenzelm@14645
   980
fun ext_cnsts rd_const syn_only prmode sg (syn, tsig, ctab, (path, spaces), data)
wenzelm@14645
   981
    raw_consts =
wenzelm@386
   982
  let
wenzelm@2979
   983
    fun prep_const (c, ty, mx) =
wenzelm@3791
   984
      (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@3791
   985
        handle TYPE (msg, _, _) =>
wenzelm@3805
   986
          (error_msg msg; err_in_const (const_name path c mx));
wenzelm@386
   987
wenzelm@14645
   988
    val consts = map (prep_const o rd_const sg syn tsig (path, spaces)) raw_consts;
wenzelm@386
   989
    val decls =
wenzelm@386
   990
      if syn_only then []
wenzelm@3791
   991
      else decls_of path Syntax.const_name consts;
wenzelm@386
   992
  in
wenzelm@14645
   993
    (map_syntax (Syntax.extend_const_gram prmode consts) syn, tsig,
wenzelm@4489
   994
      Symtab.extend (ctab, decls)
wenzelm@3791
   995
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@3967
   996
      (path, add_names spaces constK (map fst decls)), data)
wenzelm@386
   997
  end;
wenzelm@386
   998
wenzelm@14645
   999
fun ext_consts_i x = ext_cnsts no_read false ("", true) x;
wenzelm@14645
  1000
fun ext_consts x = ext_cnsts read_const false ("", true) x;
wenzelm@14645
  1001
fun ext_syntax_i x = ext_cnsts no_read true ("", true) x;
wenzelm@14645
  1002
fun ext_syntax x = ext_cnsts read_const true ("", true) x;
wenzelm@14645
  1003
fun ext_modesyntax_i x y (prmode, consts) = ext_cnsts no_read true prmode x y consts;
wenzelm@14645
  1004
fun ext_modesyntax x y (prmode, consts) = ext_cnsts read_const true prmode x y consts;
wenzelm@386
  1005
wenzelm@386
  1006
wenzelm@386
  1007
(* add type classes *)
wenzelm@386
  1008
wenzelm@386
  1009
fun const_of_class c = c ^ "_class";
wenzelm@386
  1010
wenzelm@386
  1011
fun class_of_const c_class =
wenzelm@386
  1012
  let
wenzelm@3791
  1013
    val c = implode (take (size c_class - size "_class", explode c_class));
wenzelm@386
  1014
  in
wenzelm@386
  1015
    if const_of_class c = c_class then c
wenzelm@3791
  1016
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
  1017
  end;
wenzelm@386
  1018
wenzelm@386
  1019
wenzelm@14645
  1020
fun ext_classes int sg (syn, tsig, ctab, (path, spaces), data) classes =
wenzelm@386
  1021
  let
wenzelm@562
  1022
    val names = map fst classes;
wenzelm@386
  1023
    val consts =
wenzelm@386
  1024
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
  1025
wenzelm@3810
  1026
    val full_names = map (full path) names;
wenzelm@3791
  1027
    val spaces' = add_names spaces classK full_names;
wenzelm@3791
  1028
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
  1029
    val classes' =
wenzelm@3791
  1030
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
  1031
  in
wenzelm@14645
  1032
    ext_consts_i sg
wenzelm@14645
  1033
      (map_syntax (Syntax.extend_consts names) syn,
wenzelm@3967
  1034
        Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data)
wenzelm@3791
  1035
    consts
wenzelm@386
  1036
  end;
wenzelm@386
  1037
wenzelm@386
  1038
wenzelm@2963
  1039
(* add to classrel *)
wenzelm@421
  1040
wenzelm@14645
  1041
fun ext_classrel int _ (syn, tsig, ctab, (path, spaces), data) pairs =
wenzelm@3791
  1042
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@3967
  1043
    (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data)
wenzelm@3791
  1044
  end;
wenzelm@421
  1045
wenzelm@421
  1046
wenzelm@14645
  1047
(* add translation functions *)
wenzelm@14645
  1048
wenzelm@14645
  1049
fun ext_trfs ext non_typed sg (syn, tsig, ctab, names, data) (atrs, trs, tr's, atr's) =
wenzelm@14645
  1050
  let val syn' = syn |> ext (atrs, trs, map (apsnd non_typed) tr's, atr's)
wenzelm@14645
  1051
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1052
wenzelm@14645
  1053
fun ext_trfsT ext sg (syn, tsig, ctab, names, data) tr's =
wenzelm@14645
  1054
  let val syn' = syn |> ext ([], [], tr's, [])
wenzelm@14645
  1055
  in make_syntax sg syn'; (syn', tsig, ctab, names, data) end;
wenzelm@14645
  1056
wenzelm@14645
  1057
fun ext_trfuns sg = ext_trfs (map_syntax o Syntax.extend_trfuns) Syntax.non_typed_tr' sg;
wenzelm@14645
  1058
fun ext_trfunsT sg = ext_trfsT (map_syntax o Syntax.extend_trfuns) sg;
wenzelm@14645
  1059
fun ext_advanced_trfuns sg = ext_trfs extend_trfuns Syntax.non_typed_tr'' sg;
wenzelm@14645
  1060
fun ext_advanced_trfunsT sg = ext_trfsT extend_trfuns sg;
wenzelm@14645
  1061
wenzelm@14645
  1062
wenzelm@14645
  1063
(* add token translation functions *)
wenzelm@14645
  1064
wenzelm@14645
  1065
fun ext_tokentrfuns sg (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1066
  (map_syntax (Syntax.extend_tokentrfuns args) syn, tsig, ctab, names, data);
wenzelm@14645
  1067
wenzelm@14645
  1068
wenzelm@4619
  1069
(* add translation rules *)
wenzelm@4619
  1070
wenzelm@14645
  1071
fun ext_trrules sg (syn, tsig, ctab, (path, spaces), data) args =
wenzelm@14645
  1072
  (syn |> map_syntax (Syntax.extend_trrules (make_syntax sg syn)
wenzelm@14645
  1073
      (map (Syntax.map_trrule (fn (root, str) => (intrn spaces typeK root, str))) args)),
wenzelm@14645
  1074
    tsig, ctab, (path, spaces), data);
wenzelm@4619
  1075
wenzelm@14645
  1076
fun ext_trrules_i _ (syn, tsig, ctab, names, data) args =
wenzelm@14645
  1077
  (syn |> map_syntax (Syntax.extend_trrules_i args), tsig, ctab, names, data);
wenzelm@3791
  1078
wenzelm@3791
  1079
wenzelm@3791
  1080
(* add to path *)
wenzelm@3791
  1081
wenzelm@14645
  1082
fun ext_path _ (syn, tsig, ctab, (path, spaces), data) elems =
wenzelm@3791
  1083
  let
wenzelm@3791
  1084
    val path' =
wenzelm@11501
  1085
      if elems = "//" then None
wenzelm@11501
  1086
      else if elems = "/" then Some []
wenzelm@11501
  1087
      else if elems = ".." andalso is_some path andalso path <> Some [] then
wenzelm@11501
  1088
        Some (fst (split_last (the path)))
wenzelm@11501
  1089
      else Some (if_none path [] @ NameSpace.unpack elems);
wenzelm@3791
  1090
  in
wenzelm@3967
  1091
    (syn, tsig, ctab, (path', spaces), data)
wenzelm@10404
  1092
  end;
wenzelm@3791
  1093
wenzelm@3791
  1094
wenzelm@8725
  1095
(* change name space *)
wenzelm@8725
  1096
wenzelm@14645
  1097
fun ext_add_space _ (syn, tsig, ctab, (path, spaces), data) (kind, names) =
wenzelm@8725
  1098
  (syn, tsig, ctab, (path, add_names spaces kind names), data);
wenzelm@3791
  1099
wenzelm@14645
  1100
fun ext_hide_space _ (syn, tsig, ctab, (path, spaces), data) (b, (kind, xnames)) =
wenzelm@12588
  1101
  (syn, tsig, ctab, (path, hide_names b spaces kind (map (intrn spaces kind) xnames)), data);
wenzelm@8725
  1102
wenzelm@14645
  1103
fun ext_hide_space_i _ (syn, tsig, ctab, (path, spaces), data) (b, (kind, names)) =
wenzelm@12588
  1104
  (syn, tsig, ctab, (path, hide_names b spaces kind names), data);
wenzelm@386
  1105
wenzelm@386
  1106
wenzelm@3967
  1107
(* signature data *)
wenzelm@386
  1108
wenzelm@12310
  1109
fun ext_init_data sg (syn, tsig, ctab, names, data) (kind, (e, cp, ext, mrg, prt)) =
wenzelm@12310
  1110
  (syn, tsig, ctab, names, init_data_sg sg data kind e cp ext mrg prt);
wenzelm@386
  1111
wenzelm@4998
  1112
fun ext_put_data sg (syn, tsig, ctab, names, data) (kind, f, x) =
wenzelm@4998
  1113
  (syn, tsig, ctab, names, put_data_sg sg data kind f x);
wenzelm@386
  1114
wenzelm@386
  1115
wenzelm@12310
  1116
fun copy_data (k, (e, mths as (cp, _, _, _))) =
wenzelm@6961
  1117
  (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
wenzelm@6546
  1118
wenzelm@14645
  1119
fun copy (sg as Sg ({id = _, stamps, syn}, {self, tsig, const_tab, path, spaces, data})) =
wenzelm@6546
  1120
  let
wenzelm@6546
  1121
    val _ = check_stale sg;
wenzelm@6546
  1122
    val self' = SgRef (Some (ref sg));
wenzelm@6546
  1123
    val Data tab = data;
wenzelm@6546
  1124
    val data' = Data (Symtab.map copy_data tab);
wenzelm@6546
  1125
  in create_sign self' stamps "#" (syn, tsig, const_tab, (path, spaces), data') end;
wenzelm@6546
  1126
wenzelm@6546
  1127
wenzelm@386
  1128
(* the external interfaces *)
wenzelm@386
  1129
wenzelm@14645
  1130
val add_classes          = extend_sign true (ext_classes true) "#";
wenzelm@14645
  1131
val add_classes_i        = extend_sign true (ext_classes false) "#";
wenzelm@14645
  1132
val add_classrel         = extend_sign true (ext_classrel true) "#";
wenzelm@14645
  1133
val add_classrel_i       = extend_sign true (ext_classrel false) "#";
wenzelm@14645
  1134
val add_defsort          = extend_sign true ext_defsort "#";
wenzelm@14645
  1135
val add_defsort_i        = extend_sign true ext_defsort_i "#";
wenzelm@14645
  1136
val add_types            = extend_sign true ext_types "#";
wenzelm@14645
  1137
val add_nonterminals     = extend_sign true ext_nonterminals "#";
wenzelm@14645
  1138
val add_tyabbrs          = extend_sign true ext_tyabbrs "#";
wenzelm@14645
  1139
val add_tyabbrs_i        = extend_sign true ext_tyabbrs_i "#";
wenzelm@14645
  1140
val add_arities          = extend_sign true ext_arities "#";
wenzelm@14645
  1141
val add_arities_i        = extend_sign true ext_arities_i "#";
wenzelm@14645
  1142
val add_consts           = extend_sign true ext_consts "#";
wenzelm@14645
  1143
val add_consts_i         = extend_sign true ext_consts_i "#";
wenzelm@14645
  1144
val add_syntax           = extend_sign true ext_syntax "#";
wenzelm@14645
  1145
val add_syntax_i         = extend_sign true ext_syntax_i "#";
wenzelm@14645
  1146
val add_modesyntax       = extend_sign true ext_modesyntax "#";
wenzelm@14645
  1147
val add_modesyntax_i     = extend_sign true ext_modesyntax_i "#";
wenzelm@14645
  1148
val add_trfuns           = extend_sign true ext_trfuns "#";
wenzelm@14645
  1149
val add_trfunsT          = extend_sign true ext_trfunsT "#";
wenzelm@14645
  1150
val add_advanced_trfuns  = extend_sign true ext_advanced_trfuns "#";
wenzelm@14645
  1151
val add_advanced_trfunsT = extend_sign true ext_advanced_trfunsT "#";
wenzelm@14645
  1152
val add_tokentrfuns      = extend_sign true ext_tokentrfuns "#";
wenzelm@14645
  1153
val add_trrules          = extend_sign true ext_trrules "#";
wenzelm@14645
  1154
val add_trrules_i        = extend_sign true ext_trrules_i "#";
wenzelm@14645
  1155
val add_path             = extend_sign true ext_path "#";
wenzelm@14645
  1156
val add_space            = extend_sign true ext_add_space "#";
wenzelm@14645
  1157
val hide_space           = curry (extend_sign true ext_hide_space "#");
wenzelm@14645
  1158
val hide_space_i         = curry (extend_sign true ext_hide_space_i "#");
wenzelm@14645
  1159
val init_data            = extend_sign true ext_init_data "#";
wenzelm@14645
  1160
fun put_data k f x       = extend_sign true ext_put_data "#" (k, f, x);
wenzelm@14645
  1161
fun add_name name        = extend_sign true (K K) name ();
wenzelm@14645
  1162
val prep_ext             = extend_sign false (K K) "#" ();
wenzelm@386
  1163
wenzelm@386
  1164
wenzelm@3867
  1165
wenzelm@10404
  1166
(** merge signatures **)        (*exception TERM*)
wenzelm@3867
  1167
wenzelm@4228
  1168
(* merge_stamps *)
wenzelm@4228
  1169
wenzelm@4228
  1170
fun merge_stamps stamps1 stamps2 =
wenzelm@12296
  1171
  let val stamps = merge_lists' stamps1 stamps2 in
wenzelm@4228
  1172
    (case duplicates (map ! stamps) of
wenzelm@4228
  1173
      [] => stamps
wenzelm@4228
  1174
    | dups => raise TERM ("Attempt to merge different versions of theories "
wenzelm@4228
  1175
        ^ commas_quote dups, []))
wenzelm@4228
  1176
  end;
wenzelm@4228
  1177
wenzelm@4228
  1178
wenzelm@14645
  1179
(* trivial merge *)
wenzelm@3877
  1180
wenzelm@4228
  1181
fun merge_refs (sgr1 as SgRef (Some (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
wenzelm@4228
  1182
        sgr2 as SgRef (Some (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
wenzelm@3967
  1183
      if fast_subsig (sg2, sg1) then sgr1
wenzelm@3967
  1184
      else if fast_subsig (sg1, sg2) then sgr2
wenzelm@3967
  1185
      else if subsig (sg2, sg1) then sgr1
wenzelm@3967
  1186
      else if subsig (sg1, sg2) then sgr2
wenzelm@4228
  1187
      else (merge_stamps s1 s2; (*check for different versions*)
wenzelm@4228
  1188
        raise TERM ("Attempt to do non-trivial merge of signatures", []))
wenzelm@3967
  1189
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
  1190
wenzelm@4627
  1191
val merge = deref o merge_refs o pairself self_ref;
wenzelm@4627
  1192
wenzelm@3867
  1193
wenzelm@14645
  1194
(* non-trivial merge *)              (*exception TERM/ERROR*)
wenzelm@10932
  1195
wenzelm@6961
  1196
fun nontriv_merge (sg1, sg2) =
wenzelm@3967
  1197
  if subsig (sg2, sg1) then sg1
wenzelm@251
  1198
  else if subsig (sg1, sg2) then sg2
wenzelm@386
  1199
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@3967
  1200
    raise TERM ("Attempt to merge draft signatures", [])
wenzelm@10932
  1201
  else if exists_stamp PureN sg1 andalso exists_stamp CPureN sg2 orelse
wenzelm@10932
  1202
      exists_stamp CPureN sg1 andalso exists_stamp PureN sg2 then
wenzelm@10932
  1203
    raise TERM ("Cannot merge Pure and CPure signatures", [])
wenzelm@251
  1204
  else
wenzelm@251
  1205
    let
wenzelm@14645
  1206
      val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
wenzelm@14645
  1207
          {self = _, tsig = tsig1, const_tab = const_tab1,
wenzelm@14645
  1208
            path = _, spaces = spaces1, data = data1}) = sg1;
wenzelm@14645
  1209
      val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
wenzelm@14645
  1210
          {self = _, tsig = tsig2, const_tab = const_tab2,
wenzelm@14645
  1211
            path = _, spaces = spaces2, data = data2}) = sg2;
wenzelm@386
  1212
wenzelm@3967
  1213
      val id = ref "";
wenzelm@14645
  1214
      val self_ref = ref dummy_sg;
wenzelm@3967
  1215
      val self = SgRef (Some self_ref);
wenzelm@402
  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@2185
  1220
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
  1221
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
  1222
        handle Symtab.DUPS cs =>
wenzelm@3791
  1223
          raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
wenzelm@3791
  1224
wenzelm@11501
  1225
      val path = Some [];
wenzelm@3791
  1226
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
  1227
      val spaces =
wenzelm@3791
  1228
        kinds ~~
wenzelm@3791
  1229
          ListPair.map NameSpace.merge
wenzelm@3791
  1230
            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
wenzelm@3867
  1231
wenzelm@4256
  1232
      val data = merge_data (data1, data2);
wenzelm@3967
  1233
wenzelm@14645
  1234
      val sign = make_sign (id, self, tsig, const_tab, Syn (syntax, trfuns),
wenzelm@14645
  1235
        path, spaces, data, stamps);
wenzelm@14645
  1236
    in self_ref := sign; syn_of sign; sign end;
wenzelm@3791
  1237
clasohm@0
  1238
end;