src/Pure/sign.ML
author wenzelm
Thu Jun 09 16:56:42 2005 +0200 (2005-06-09)
changeset 16354 6f0ca9628840
parent 16337 5734de2f7ace
child 16368 a06868ebeb0f
permissions -rw-r--r--
simplified is_stale, check_stale;
fixed map_sg -- proper treatment of non-drafts;
wenzelm@19
     1
(*  Title:      Pure/sign.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@251
     3
    Author:     Lawrence C Paulson and Markus Wenzel
clasohm@0
     4
wenzelm@251
     5
The abstract type "sg" of signatures.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@19
     8
signature SIGN =
wenzelm@3791
     9
sig
paulson@1501
    10
  type sg
wenzelm@3967
    11
  type sg_ref
wenzelm@4256
    12
  type data
wenzelm@2197
    13
  val rep_sg: sg ->
wenzelm@3975
    14
   {self: sg_ref,
wenzelm@14784
    15
    tsig: Type.tsig,
wenzelm@14784
    16
    consts: (typ * stamp) Symtab.table,
wenzelm@16133
    17
    naming: NameSpace.naming,
wenzelm@16337
    18
    spaces: {class: NameSpace.T, tycon: NameSpace.T, const: NameSpace.T},
wenzelm@4256
    19
    data: data}
wenzelm@3975
    20
  val stamp_names_of: sg -> string list
wenzelm@10932
    21
  val exists_stamp: string -> sg -> bool
wenzelm@16337
    22
  val pretty_sg: sg -> Pretty.T
wenzelm@16337
    23
  val pprint_sg: sg -> pprint_args -> unit
wenzelm@16337
    24
  val pretty_abbrev_sg: sg -> Pretty.T
wenzelm@16337
    25
  val str_of_sg: sg -> string
wenzelm@16337
    26
  val is_stale: sg -> bool
wenzelm@3967
    27
  val self_ref: sg -> sg_ref
wenzelm@16337
    28
  val deref: sg_ref -> sg
wenzelm@16337
    29
  val name_of: sg -> string
wenzelm@16337
    30
  val is_draft: sg -> bool
wenzelm@16337
    31
  val eq_sg: sg * sg -> bool
paulson@1501
    32
  val subsig: sg * sg -> bool
paulson@1501
    33
  val same_sg: sg * sg -> bool
wenzelm@16337
    34
  val joinable: sg * sg -> bool
wenzelm@16337
    35
  val prep_ext: sg -> sg
wenzelm@16337
    36
  val copy: sg -> sg
wenzelm@16337
    37
  val add_name: string -> sg -> sg
wenzelm@16337
    38
  val data_kinds: data -> string list
wenzelm@16337
    39
  val print_all_data: sg -> unit
wenzelm@16337
    40
  val pre_pure: sg
wenzelm@16337
    41
  val PureN: string
wenzelm@16337
    42
  val CPureN: string
wenzelm@16288
    43
  val naming_of: sg -> NameSpace.naming
wenzelm@16337
    44
  val base_name: string -> bstring
wenzelm@16337
    45
  val full_name: sg -> bstring -> string
wenzelm@16337
    46
  val full_name_path: sg -> string -> bstring -> string
wenzelm@16337
    47
  val declare_name: sg -> string -> NameSpace.T -> NameSpace.T
wenzelm@16337
    48
  val syn_of: sg -> Syntax.syntax
wenzelm@16337
    49
  val tsig_of: sg -> Type.tsig
paulson@1501
    50
  val classes: sg -> class list
wenzelm@4844
    51
  val defaultS: sg -> sort
paulson@1501
    52
  val subsort: sg -> sort * sort -> bool
wenzelm@4568
    53
  val of_sort: sg -> typ * sort -> bool
wenzelm@7640
    54
  val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
wenzelm@14784
    55
  val universal_witness: sg -> (typ * sort) option
wenzelm@10443
    56
  val typ_instance: sg -> typ * typ -> bool
wenzelm@16337
    57
  val is_logtype: sg -> string -> bool
wenzelm@16337
    58
  val const_type: sg -> string -> typ option
wenzelm@16337
    59
  val the_const_type: sg -> string -> typ
wenzelm@16337
    60
  val declared_tyname: sg -> string -> bool
wenzelm@16337
    61
  val declared_const: sg -> string -> bool
wenzelm@3810
    62
  val classK: string
wenzelm@3810
    63
  val typeK: string
wenzelm@3810
    64
  val constK: string
wenzelm@3810
    65
  val intern: sg -> string -> xstring -> string
wenzelm@3810
    66
  val extern: sg -> string -> string -> xstring
wenzelm@16133
    67
  val extern_table: sg -> string -> 'a Symtab.table -> (xstring * 'a) list
wenzelm@16337
    68
  val intern_class: sg -> xstring -> string
wenzelm@16337
    69
  val extern_class: sg -> string -> xstring
wenzelm@3805
    70
  val intern_tycon: sg -> xstring -> string
wenzelm@16337
    71
  val extern_tycon: sg -> string -> xstring
wenzelm@3805
    72
  val intern_const: sg -> xstring -> string
wenzelm@16337
    73
  val extern_const: sg -> string -> xstring
wenzelm@16337
    74
  val intern_sort: sg -> sort -> sort
wenzelm@16337
    75
  val extern_sort: sg -> sort -> sort
wenzelm@16337
    76
  val intern_typ: sg -> typ -> typ
wenzelm@16337
    77
  val extern_typ: sg -> typ -> typ
wenzelm@16337
    78
  val intern_term: sg -> term -> term
wenzelm@16337
    79
  val extern_term: sg -> term -> term
wenzelm@16337
    80
  val intern_tycons: sg -> typ -> typ
wenzelm@16337
    81
  val pretty_term': Syntax.syntax -> sg -> term -> Pretty.T
paulson@1501
    82
  val pretty_term: sg -> term -> Pretty.T
paulson@1501
    83
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    84
  val pretty_sort: sg -> sort -> Pretty.T
wenzelm@14828
    85
  val pretty_classrel: sg -> class list -> Pretty.T
wenzelm@14828
    86
  val pretty_arity: sg -> arity -> Pretty.T
paulson@1501
    87
  val string_of_term: sg -> term -> string
paulson@1501
    88
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    89
  val string_of_sort: sg -> sort -> string
wenzelm@14828
    90
  val string_of_classrel: sg -> class list -> string
wenzelm@14828
    91
  val string_of_arity: sg -> arity -> string
paulson@1501
    92
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    93
  val pprint_typ: sg -> typ -> pprint_args -> unit
wenzelm@14828
    94
  val pp: sg -> Pretty.pp
wenzelm@8898
    95
  val certify_class: sg -> class -> class
wenzelm@8898
    96
  val certify_sort: sg -> sort -> sort
paulson@1501
    97
  val certify_typ: sg -> typ -> typ
wenzelm@16337
    98
  val certify_typ_syntax: sg -> typ -> typ
wenzelm@16337
    99
  val certify_typ_abbrev: sg -> typ -> typ
wenzelm@14828
   100
  val certify_term: Pretty.pp -> sg -> term -> term * typ * int
wenzelm@16337
   101
  val read_sort': Syntax.syntax -> sg -> string -> sort
wenzelm@8898
   102
  val read_sort: sg -> string -> sort
wenzelm@16337
   103
  val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   104
  val read_typ_syntax': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   105
  val read_typ_abbrev': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
paulson@1501
   106
  val read_typ: sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   107
  val read_typ_syntax: sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   108
  val read_typ_abbrev: sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   109
  val read_tyname: sg -> string -> typ        (*exception TYPE*)
wenzelm@16288
   110
  val read_const: sg -> string -> term        (*exception TYPE*)
wenzelm@16337
   111
  val infer_types_simult: Pretty.pp -> sg -> (indexname -> typ option) ->
wenzelm@16337
   112
    (indexname -> sort option) -> string list -> bool
wenzelm@16337
   113
    -> (term list * typ) list -> term list * (indexname * typ) list
wenzelm@14828
   114
  val infer_types: Pretty.pp -> sg -> (indexname -> typ option) ->
paulson@1501
   115
    (indexname -> sort option) -> string list -> bool
wenzelm@16337
   116
    -> term list * typ -> term * (indexname * typ) list
wenzelm@14905
   117
  val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
wenzelm@14828
   118
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@12068
   119
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8607
   120
  val read_def_terms:
wenzelm@8607
   121
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@8607
   122
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8802
   123
  val simple_read_term: sg -> typ -> string -> term
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_syntax: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   133
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   134
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   135
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
wenzelm@15746
   136
  val del_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@15746
   137
  val del_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
wenzelm@16337
   138
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
wenzelm@16337
   139
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@16337
   140
  val const_of_class: class -> string
wenzelm@16337
   141
  val class_of_const: string -> class
wenzelm@16337
   142
  val add_classes: (bstring * xstring list) list -> sg -> sg
wenzelm@16337
   143
  val add_classes_i: (bstring * class list) list -> sg -> sg
wenzelm@16337
   144
  val add_classrel: (xstring * xstring) list -> sg -> sg
wenzelm@16337
   145
  val add_classrel_i: (class * class) list -> sg -> sg
paulson@1501
   146
  val add_trfuns:
wenzelm@4344
   147
    (string * (ast list -> ast)) list *
wenzelm@4344
   148
    (string * (term list -> term)) list *
wenzelm@4344
   149
    (string * (term list -> term)) list *
wenzelm@4344
   150
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
   151
  val add_trfunsT:
wenzelm@4344
   152
    (string * (bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@14645
   153
  val add_advanced_trfuns:
wenzelm@14645
   154
    (string * (sg -> ast list -> ast)) list *
wenzelm@14645
   155
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   156
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   157
    (string * (sg -> ast list -> ast)) list -> sg -> sg
wenzelm@14645
   158
  val add_advanced_trfunsT:
wenzelm@14645
   159
    (string * (sg -> bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
   160
  val add_tokentrfuns:
wenzelm@6311
   161
    (string * string * (string -> string * real)) list -> sg -> sg
wenzelm@4619
   162
  val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
paulson@1810
   163
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
   164
  val add_path: string -> sg -> sg
wenzelm@16133
   165
  val qualified_names: sg -> sg
wenzelm@16133
   166
  val no_base_names: sg -> sg
wenzelm@16133
   167
  val custom_accesses: (string list -> string list list) -> sg -> sg
wenzelm@16133
   168
  val set_policy: (string -> bstring -> string) * (string list -> string list list) -> sg -> sg
wenzelm@16189
   169
  val restore_naming: sg -> sg -> sg
wenzelm@12588
   170
  val hide_space: bool -> string * string list -> sg -> sg
wenzelm@12588
   171
  val hide_space_i: bool -> string * string list -> sg -> sg
wenzelm@3967
   172
  val merge_refs: sg_ref * sg_ref -> sg_ref
wenzelm@4627
   173
  val merge: sg * sg -> sg
wenzelm@15703
   174
  val prep_ext_merge: sg list -> sg
wenzelm@3791
   175
end;
clasohm@0
   176
wenzelm@6191
   177
signature PRIVATE_SIGN =
wenzelm@5642
   178
sig
wenzelm@5642
   179
  include SIGN
wenzelm@6546
   180
  val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
wenzelm@12310
   181
    (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
wenzelm@5642
   182
  val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
wenzelm@5642
   183
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
wenzelm@5642
   184
  val print_data: Object.kind -> sg -> unit
wenzelm@5642
   185
end;
wenzelm@5642
   186
wenzelm@6191
   187
structure Sign: PRIVATE_SIGN =
wenzelm@143
   188
struct
clasohm@0
   189
wenzelm@251
   190
(** datatype sg **)
wenzelm@251
   191
wenzelm@14645
   192
(* types sg, data, syn, sg_ref *)
wenzelm@4256
   193
wenzelm@19
   194
datatype sg =
wenzelm@3975
   195
  Sg of
wenzelm@16337
   196
   {id: string ref,
wenzelm@16133
   197
    stamps: string ref list,                      (*unique theory indentifier*)
wenzelm@16133
   198
    syn: syn} *                                   (*syntax for parsing and printing*)
wenzelm@16133
   199
   {self: sg_ref,                                 (*mutable self reference*)
wenzelm@16133
   200
    tsig: Type.tsig,                              (*order-sorted signature of types*)
wenzelm@16133
   201
    consts: (typ * stamp) Symtab.table,           (*type schemes of constants*)
wenzelm@16337
   202
    naming: NameSpace.naming,
wenzelm@16337
   203
    spaces: {class: NameSpace.T, tycon: NameSpace.T, const: NameSpace.T},
wenzelm@16337
   204
    data: data}
wenzelm@4256
   205
and data =
wenzelm@4256
   206
  Data of
wenzelm@16133
   207
    (Object.kind *                                (*kind (for authorization)*)
wenzelm@16133
   208
      (Object.T *                                 (*value*)
wenzelm@16133
   209
        ((Object.T -> Object.T) *                 (*copy method*)
wenzelm@16133
   210
          (Object.T -> Object.T) *                (*prepare extend method*)
wenzelm@16133
   211
          (Object.T * Object.T -> Object.T) *     (*merge and prepare extend method*)
wenzelm@16133
   212
          (sg -> Object.T -> unit))))             (*print method*)
wenzelm@4256
   213
    Symtab.table
wenzelm@14645
   214
and syn =
wenzelm@14645
   215
  Syn of
wenzelm@14645
   216
    Syntax.syntax *
wenzelm@14645
   217
     (((sg -> ast list -> ast) * stamp) Symtab.table *
wenzelm@14645
   218
      ((sg -> term list -> term) * stamp) Symtab.table *
wenzelm@14645
   219
      ((sg -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
wenzelm@14645
   220
      ((sg -> ast list -> ast) * stamp) list Symtab.table)
wenzelm@3967
   221
and sg_ref =
wenzelm@4998
   222
  SgRef of sg ref option;
clasohm@0
   223
wenzelm@16337
   224
(* FIXME assign!??? *)
wenzelm@16337
   225
fun make_sg (id, self, stamps) naming data (syn, tsig, consts, spaces) =
wenzelm@14645
   226
  Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
wenzelm@16133
   227
    consts = consts, naming = naming, spaces = spaces, data = data});
wenzelm@3975
   228
wenzelm@16337
   229
fun rep_sg (Sg (_, args)) = args;
wenzelm@3975
   230
wenzelm@3975
   231
wenzelm@16337
   232
(* stamps *)
wenzelm@3967
   233
wenzelm@15801
   234
fun stamps_of (Sg ({stamps, ...}, _)) = stamps;
wenzelm@15801
   235
val stamp_names_of = rev o map ! o stamps_of;
wenzelm@15801
   236
fun exists_stamp name = exists (equal name o !) o stamps_of;
wenzelm@15801
   237
wenzelm@16354
   238
fun is_stale (Sg ({id, ...}, {self = SgRef (SOME (ref (Sg ({id = id', ...}, _)))), ...})) =
wenzelm@16354
   239
      id <> id'
wenzelm@16354
   240
  | is_stale _ = false;
wenzelm@16354
   241
wenzelm@16354
   242
fun pretty_sg sg =
wenzelm@16354
   243
  Pretty.str_list "{" "}" (stamp_names_of sg @ (if is_stale sg then ["!"] else []));
wenzelm@16354
   244
wenzelm@3975
   245
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@3975
   246
wenzelm@16337
   247
fun pretty_abbrev_sg sg =
wenzelm@16337
   248
  let
wenzelm@16337
   249
    val stamps = map ! (stamps_of sg);
wenzelm@16337
   250
    val abbrev = if length stamps > 5 then "..." :: rev (List.take (stamps, 5)) else rev stamps;
wenzelm@16337
   251
  in Pretty.str_list "{" "}" abbrev end;
wenzelm@16288
   252
wenzelm@16337
   253
val str_of_sg = Pretty.str_of o pretty_abbrev_sg;
wenzelm@15703
   254
wenzelm@3967
   255
wenzelm@3995
   256
(* id and self *)
wenzelm@3967
   257
wenzelm@16354
   258
fun check_stale pos sg =
wenzelm@16354
   259
  if is_stale sg then raise TERM ("Stale signature (in " ^ pos ^ "): " ^ str_of_sg sg, [])
wenzelm@16354
   260
  else sg;
wenzelm@4951
   261
wenzelm@16337
   262
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale "Sign.self_ref" sg; self);
wenzelm@3995
   263
skalberg@15531
   264
fun deref (SgRef (SOME (ref sg))) = sg
skalberg@15531
   265
  | deref (SgRef NONE) = sys_error "Sign.deref";
wenzelm@3995
   266
wenzelm@16337
   267
fun assign (SgRef (SOME r)) sg = r := sg
wenzelm@16337
   268
  | assign (SgRef NONE) _ = sys_error "Sign.assign";
wenzelm@16337
   269
wenzelm@3975
   270
fun name_of (sg as Sg ({id = ref name, ...}, _)) =
wenzelm@4844
   271
  if name = "" orelse ord name = ord "#" then
wenzelm@3975
   272
    raise TERM ("Nameless signature " ^ str_of_sg sg, [])
wenzelm@3975
   273
  else name;
wenzelm@3975
   274
wenzelm@16337
   275
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) =
wenzelm@16337
   276
      name = "" orelse ord name = ord "#"
wenzelm@16337
   277
  | is_draft _ = sys_error "Sign.is_draft";
wenzelm@16337
   278
nipkow@206
   279
wenzelm@2979
   280
(* inclusion and equality *)
paulson@2180
   281
wenzelm@2185
   282
local
wenzelm@2185
   283
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@16337
   284
  fun mem_stamp (_: string ref, []) = false
wenzelm@2185
   285
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   286
wenzelm@2185
   287
  fun subset_stamp ([], ys) = true
wenzelm@2185
   288
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   289
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
wenzelm@2185
   290
in
wenzelm@3975
   291
  fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
wenzelm@16337
   292
    (check_stale "Sign.eq_sg" sg1; check_stale "Sign.eq_sg" sg2; id1 = id2);
wenzelm@2185
   293
wenzelm@15801
   294
  fun subsig (sg1, sg2) =
wenzelm@15801
   295
    eq_sg (sg1, sg2) orelse mem_stamp (hd (stamps_of sg1), stamps_of sg2);
paulson@2180
   296
wenzelm@15801
   297
  fun subsig_internal (sg1, sg2) =
wenzelm@15801
   298
    eq_sg (sg1, sg2) orelse subset_stamp (stamps_of sg1, stamps_of sg2);
wenzelm@2185
   299
end;
wenzelm@2185
   300
wenzelm@402
   301
wenzelm@9031
   302
fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
wenzelm@9031
   303
wenzelm@2185
   304
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   305
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   306
  same version of it*)
wenzelm@15801
   307
fun same_sg (sg1, sg2) =
wenzelm@15801
   308
  eq_sg (sg1, sg2) orelse eq_set_string (pairself (map ! o stamps_of) (sg1, sg2));
wenzelm@2185
   309
wenzelm@16337
   310
wenzelm@16337
   311
(* data operations *)
wenzelm@16337
   312
wenzelm@16337
   313
fun err_method name kind e =    (* FIXME wrap exn msg!? *)
wenzelm@16337
   314
  (warning ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
wenzelm@16337
   315
wenzelm@16337
   316
fun err_inconsistent kinds =
wenzelm@16337
   317
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
wenzelm@16337
   318
wenzelm@16337
   319
val empty_data = Data Symtab.empty;
wenzelm@16337
   320
wenzelm@16337
   321
fun merge_data (Data tab1, Data tab2) =
wenzelm@16337
   322
  let
wenzelm@16337
   323
    val data1 = map snd (Symtab.dest tab1);
wenzelm@16337
   324
    val data2 = map snd (Symtab.dest tab2);
wenzelm@16337
   325
    val all_data = data1 @ data2;
wenzelm@16337
   326
    val kinds = gen_distinct Object.eq_kind (map fst all_data);
wenzelm@16337
   327
wenzelm@16337
   328
   fun entry data kind =
wenzelm@16337
   329
     (case gen_assoc Object.eq_kind (data, kind) of
wenzelm@16337
   330
       NONE => []
wenzelm@16337
   331
     | SOME x => [(kind, x)]);
wenzelm@16337
   332
wenzelm@16337
   333
    fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
wenzelm@16337
   334
          (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
wenzelm@16337
   335
      | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
wenzelm@16337
   336
          (kind, (mrg (e1, e2)
wenzelm@16337
   337
            handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
wenzelm@16337
   338
      | merge_entries _ = sys_error "merge_entries";
wenzelm@16337
   339
wenzelm@16337
   340
    val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
wenzelm@16337
   341
    val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
wenzelm@16337
   342
  in
wenzelm@16337
   343
    Data (Symtab.make data_idx)
wenzelm@16337
   344
      handle Symtab.DUPS dups => err_inconsistent dups
wenzelm@16337
   345
  end;
wenzelm@16337
   346
wenzelm@16337
   347
fun prep_ext_data data = merge_data (data, empty_data);
wenzelm@16337
   348
wenzelm@16337
   349
fun copy_data (Data tab) =
wenzelm@16337
   350
  let
wenzelm@16337
   351
    fun cp_data (k, (e, mths as (cp, _, _, _))) =
wenzelm@16337
   352
      (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
wenzelm@16337
   353
  in Data (Symtab.map cp_data tab) end;
wenzelm@16337
   354
wenzelm@16337
   355
wenzelm@16337
   356
wenzelm@16337
   357
(** build signatures **)
wenzelm@16337
   358
wenzelm@16337
   359
fun create_sg name self stamps naming data sign =
wenzelm@16337
   360
  let
wenzelm@16337
   361
    val id = ref name;
wenzelm@16337
   362
    val stamps' = (case stamps of ref "#" :: ss => ss | ss => ss);
wenzelm@16337
   363
    val _ = conditional (exists (equal name o !) stamps')
wenzelm@16337
   364
      (fn () => error ("Theory already contains a " ^ quote name ^ " component"));
wenzelm@16337
   365
    val sg = make_sg (id, self, id :: stamps') naming data sign;
wenzelm@16337
   366
  in assign self sg; sg end;
wenzelm@16337
   367
wenzelm@16337
   368
fun new_sg f (sg as Sg ({stamps, syn, ...}, {self = _, tsig, consts, naming, spaces, data})) =
wenzelm@16337
   369
  let
wenzelm@16337
   370
    val _ = check_stale "Sign.new_sg" sg;
wenzelm@16337
   371
    val self' = SgRef (SOME (ref sg));
wenzelm@16337
   372
    val data' = f data;
wenzelm@16337
   373
  in create_sg "#" self' stamps naming data' (syn, tsig, consts, spaces) end;
wenzelm@16337
   374
wenzelm@16337
   375
val prep_ext = new_sg prep_ext_data;
wenzelm@16337
   376
val copy = new_sg copy_data;
wenzelm@16337
   377
wenzelm@16337
   378
fun add_name name (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, spaces, data})) =
wenzelm@16354
   379
  let
wenzelm@16354
   380
    val _ = check_stale "Sign.add_name" sg;
wenzelm@16354
   381
    val (self', data') =
wenzelm@16354
   382
      if is_draft sg then (self, data)
wenzelm@16354
   383
      else (SgRef (SOME (ref sg)), prep_ext_data data);
wenzelm@16354
   384
  in create_sg name self' stamps naming data' (syn, tsig, consts, spaces) end;
wenzelm@16337
   385
wenzelm@16354
   386
fun map_sg keep f (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, spaces, data})) =
wenzelm@16337
   387
  let
wenzelm@16337
   388
    val _ = check_stale "Sign.map_sg" sg;
wenzelm@16354
   389
    val (self', data') =
wenzelm@16354
   390
      if is_draft sg andalso keep then (self, data)
wenzelm@16354
   391
      else (SgRef (SOME (ref sg)), prep_ext_data data);
wenzelm@16354
   392
    val (naming', data', sign') = f (naming, data', (syn, tsig, consts, spaces));
wenzelm@16354
   393
  in create_sg "#" self' stamps naming' data' sign' end;
wenzelm@16337
   394
wenzelm@16354
   395
fun map_naming f = map_sg true (fn (naming, data, sign) => (f naming, data, sign));
wenzelm@16354
   396
fun map_data f = map_sg true (fn (naming, data, sign) => (naming, f data, sign));
wenzelm@16354
   397
fun map_sign f = map_sg true (fn (naming, data, sign) => (naming, data, f sign));
wenzelm@16337
   398
fun map_syn f = map_sign (fn (syn, tsig, consts, spaces) => (f syn, tsig, consts, spaces));
wenzelm@16337
   399
wenzelm@16337
   400
wenzelm@16337
   401
wenzelm@16337
   402
(** signature data **)
wenzelm@16337
   403
wenzelm@16337
   404
(* errors *)
wenzelm@16337
   405
wenzelm@16337
   406
fun of_theory sg = "\nof theory " ^ str_of_sg sg;
wenzelm@16337
   407
wenzelm@16337
   408
fun err_dup_init sg kind =
wenzelm@16337
   409
  error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@16337
   410
wenzelm@16337
   411
fun err_uninit sg kind =
wenzelm@16337
   412
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@16337
   413
wenzelm@16337
   414
fun err_access sg kind =
wenzelm@16337
   415
  error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@16337
   416
wenzelm@16337
   417
wenzelm@16337
   418
(* access data *)
wenzelm@16337
   419
wenzelm@16337
   420
fun data_kinds (Data tab) = Symtab.keys tab;
wenzelm@16337
   421
wenzelm@16337
   422
fun lookup_data sg tab kind =
wenzelm@16337
   423
  let val name = Object.name_of_kind kind in
wenzelm@16337
   424
    (case Symtab.lookup (tab, name) of
wenzelm@16337
   425
      SOME (k, x) =>
wenzelm@16337
   426
        if Object.eq_kind (kind, k) then x
wenzelm@16337
   427
        else err_access sg name
wenzelm@16337
   428
    | NONE => err_uninit sg name)
wenzelm@16337
   429
  end;
wenzelm@16337
   430
wenzelm@16337
   431
fun init_data (kind, (e, cp, ext, mrg, prt)) sg = sg |> map_data (fn Data tab =>
wenzelm@16337
   432
  Data (Symtab.update_new ((Object.name_of_kind kind, (kind, (e, (cp, ext, mrg, prt)))), tab))
wenzelm@16337
   433
    handle Symtab.DUP name => err_dup_init sg name);
wenzelm@16337
   434
wenzelm@16337
   435
fun get_data kind dest (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@16337
   436
  let val x = fst (lookup_data sg tab kind)
wenzelm@16337
   437
  in dest x handle Match => Object.kind_error kind end;
wenzelm@16337
   438
wenzelm@16337
   439
fun put_data kind mk x sg = sg |> map_data (fn Data tab =>
wenzelm@16337
   440
  Data (Symtab.update ((Object.name_of_kind kind,
wenzelm@16337
   441
    (kind, (mk x, snd (lookup_data sg tab kind)))), tab)));
wenzelm@16337
   442
wenzelm@16337
   443
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@16337
   444
  let val (e, (_, _, _, prt)) = lookup_data sg tab kind in
wenzelm@16337
   445
    prt sg e handle exn =>
wenzelm@16337
   446
      err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn
wenzelm@16337
   447
  end;
wenzelm@16337
   448
wenzelm@16337
   449
fun print_all_data (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@16337
   450
  List.app (fn kind => print_data kind sg) (map (#1 o #2) (Symtab.dest tab));
wenzelm@16337
   451
wenzelm@16337
   452
wenzelm@16337
   453
wenzelm@16337
   454
(** primitive signatures **)
wenzelm@16337
   455
wenzelm@16337
   456
val empty_spaces =
wenzelm@16337
   457
  {class = NameSpace.empty, tycon = NameSpace.empty, const = NameSpace.empty};
wenzelm@16337
   458
wenzelm@16337
   459
fun merge_spaces
wenzelm@16337
   460
  ({class = class1, tycon = tycon1, const = const1},
wenzelm@16337
   461
    {class = class2, tycon = tycon2, const = const2}) =
wenzelm@16337
   462
  {class = NameSpace.merge (class1, class2),
wenzelm@16337
   463
   tycon = NameSpace.merge (tycon1, tycon2),
wenzelm@16337
   464
   const = NameSpace.merge (const1, const2)};
wenzelm@16337
   465
wenzelm@16337
   466
val pure_syn =
wenzelm@16337
   467
  Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
wenzelm@16337
   468
wenzelm@16337
   469
val dummy_sg = make_sg (ref "", SgRef NONE, []) NameSpace.default_naming empty_data
wenzelm@16337
   470
  (pure_syn, Type.empty_tsig, Symtab.empty, empty_spaces);
wenzelm@16337
   471
wenzelm@16337
   472
val pre_pure =
wenzelm@16337
   473
  create_sg "#" (SgRef (SOME (ref dummy_sg))) [] NameSpace.default_naming empty_data
wenzelm@16337
   474
    (pure_syn, Type.empty_tsig, Symtab.empty, empty_spaces);
wenzelm@16337
   475
wenzelm@16337
   476
val PureN = "Pure";
wenzelm@16337
   477
val CPureN = "CPure";
wenzelm@16337
   478
wenzelm@16337
   479
wenzelm@16337
   480
wenzelm@16337
   481
(** signature content **)
wenzelm@16337
   482
wenzelm@16337
   483
(* naming *)
wenzelm@16337
   484
wenzelm@16337
   485
fun naming_of (Sg (_, {naming, ...})) = naming;
wenzelm@16337
   486
wenzelm@16337
   487
val base_name = NameSpace.base;
wenzelm@16337
   488
val full_name = NameSpace.full o naming_of;
wenzelm@16337
   489
fun full_name_path sg elems = NameSpace.full (NameSpace.add_path elems (naming_of sg));
wenzelm@16337
   490
val declare_name = NameSpace.declare o naming_of;
wenzelm@14645
   491
wenzelm@14645
   492
wenzelm@14645
   493
(* syntax *)
wenzelm@14645
   494
wenzelm@14645
   495
fun map_syntax f (Syn (syntax, trfuns)) = Syn (f syntax, trfuns);
wenzelm@14645
   496
wenzelm@14645
   497
fun make_syntax sg (Syn (syntax, (atrs, trs, tr's, atr's))) =
wenzelm@14645
   498
  let
wenzelm@15746
   499
    fun apply (c, (f, s)) = (c, (f sg, s));
wenzelm@14645
   500
    fun prep tab = map apply (Symtab.dest tab);
wenzelm@14645
   501
    fun prep' tab = map apply (Symtab.dest_multi tab);
wenzelm@14645
   502
  in syntax |> Syntax.extend_trfuns (prep atrs, prep trs, prep' tr's, prep' atr's) end;
wenzelm@14645
   503
wenzelm@14645
   504
fun syn_of (sg as Sg ({syn, ...}, _)) = make_syntax sg syn;
wenzelm@14645
   505
wenzelm@14645
   506
wenzelm@14645
   507
(* advanced translation functions *)
wenzelm@14645
   508
wenzelm@14645
   509
fun extend_trfuns (atrs, trs, tr's, atr's)
wenzelm@14645
   510
    (Syn (syn, (parse_ast_trtab, parse_trtab, print_trtab, print_ast_trtab))) =
wenzelm@15746
   511
  Syn (syn, (Syntax.extend_trtab "parse ast translation" atrs parse_ast_trtab,
wenzelm@15746
   512
    Syntax.extend_trtab "parse translation" trs parse_trtab,
wenzelm@15746
   513
    Syntax.extend_tr'tab tr's print_trtab,
wenzelm@15746
   514
    Syntax.extend_tr'tab atr's print_ast_trtab));
wenzelm@14645
   515
wenzelm@14645
   516
fun merge_trfuns
wenzelm@14645
   517
    (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
wenzelm@14645
   518
    (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
wenzelm@15746
   519
  (Syntax.merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@15746
   520
    Syntax.merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@14645
   521
    Syntax.merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@14645
   522
    Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
wenzelm@386
   523
clasohm@0
   524
wenzelm@16337
   525
(* type signature *)
wenzelm@16337
   526
wenzelm@16337
   527
val tsig_of = #tsig o rep_sg;
wenzelm@4568
   528
wenzelm@7640
   529
val classes = Type.classes o tsig_of;
wenzelm@4844
   530
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   531
val subsort = Type.subsort o tsig_of;
wenzelm@7640
   532
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   533
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@14784
   534
val universal_witness = Type.universal_witness o tsig_of;
wenzelm@14784
   535
val typ_instance = Type.typ_instance o tsig_of;
wenzelm@16337
   536
fun is_logtype sg c = c mem_string Type.logical_types (tsig_of sg);
wenzelm@4256
   537
wenzelm@4256
   538
wenzelm@16337
   539
(* consts signature *)
wenzelm@4256
   540
wenzelm@16337
   541
fun const_type (Sg (_, {consts, ...})) c = Option.map #1 (Symtab.lookup (consts, c));
wenzelm@4256
   542
wenzelm@16337
   543
fun the_const_type sg c =
wenzelm@16337
   544
  (case const_type sg c of SOME T => T
wenzelm@16337
   545
  | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
wenzelm@4256
   546
wenzelm@16337
   547
fun declared_tyname sg c = is_some (Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c));
wenzelm@16337
   548
fun declared_const sg c = is_some (const_type sg c);
wenzelm@4256
   549
wenzelm@4256
   550
wenzelm@16337
   551
(* name spaces *)
wenzelm@3791
   552
wenzelm@3791
   553
val classK = "class";
wenzelm@3791
   554
val typeK = "type";
wenzelm@3791
   555
val constK = "const";
wenzelm@3791
   556
wenzelm@16337
   557
fun illegal_space kind = "Illegal signature name space: " ^ quote kind;
wenzelm@3791
   558
wenzelm@16337
   559
fun space_of sg kind = #spaces (rep_sg sg) |>
wenzelm@16337
   560
 (if kind = classK then #class
wenzelm@16337
   561
  else if kind = typeK then #tycon
wenzelm@16337
   562
  else if kind = constK then #const
wenzelm@16337
   563
  else raise TERM (illegal_space kind, []));
wenzelm@3791
   564
wenzelm@16337
   565
fun map_space kind f {class, tycon, const} =
wenzelm@16337
   566
  let
wenzelm@16337
   567
    val (class', tycon', const') =
wenzelm@16337
   568
      if kind = classK then (f class, tycon, const)
wenzelm@16337
   569
      else if kind = typeK then (class, f tycon, const)
wenzelm@16337
   570
      else if kind = constK then (class, tycon, f const)
wenzelm@16337
   571
      else raise TERM (illegal_space kind, []);
wenzelm@16337
   572
  in {class = class', tycon = tycon', const = const'} end;
wenzelm@3791
   573
wenzelm@16337
   574
fun declare_names sg kind = map_space kind o fold (declare_name sg);
wenzelm@16337
   575
fun hide_names kind = map_space kind oo (fold o NameSpace.hide);
wenzelm@620
   576
wenzelm@402
   577
clasohm@0
   578
wenzelm@16337
   579
(** intern / extern names **)
wenzelm@16337
   580
wenzelm@16337
   581
val intern = NameSpace.intern oo space_of;
wenzelm@16337
   582
val extern = NameSpace.extern oo space_of;
wenzelm@16337
   583
fun extern_table sg = curry NameSpace.extern_table o space_of sg;
wenzelm@14645
   584
wenzelm@16337
   585
fun intern_class sg = intern sg classK;
wenzelm@16337
   586
fun extern_class sg = extern sg classK;
wenzelm@16337
   587
fun intern_tycon sg = intern sg typeK;
wenzelm@16337
   588
fun extern_tycon sg = extern sg typeK;
wenzelm@16337
   589
fun intern_const sg = intern sg constK;
wenzelm@16337
   590
fun extern_const sg = extern sg constK;
wenzelm@16337
   591
wenzelm@16337
   592
val intern_sort = map o intern_class;
wenzelm@16337
   593
val extern_sort = map o extern_class;
wenzelm@16337
   594
wenzelm@16337
   595
local
wenzelm@14645
   596
wenzelm@16337
   597
fun mapping add_names f t =
wenzelm@16337
   598
  let
wenzelm@16337
   599
    fun f' x = let val y = f x in if x = y then NONE else SOME (x, y) end;
wenzelm@16337
   600
    val tab = List.mapPartial f' (add_names (t, []));
wenzelm@16337
   601
    fun get x = if_none (assoc_string (tab, x)) x;
wenzelm@16337
   602
  in get end;
wenzelm@16337
   603
wenzelm@16337
   604
fun typ_mapping f g sg T =
wenzelm@16337
   605
  T |> Term.map_typ
wenzelm@16337
   606
    (mapping add_typ_classes (f sg) T)
wenzelm@16337
   607
    (mapping add_typ_tycons (g sg) T);
wenzelm@14645
   608
wenzelm@16337
   609
fun term_mapping f g h sg t =
wenzelm@16337
   610
  t |> Term.map_term
wenzelm@16337
   611
    (mapping add_term_classes (f sg) t)
wenzelm@16337
   612
    (mapping add_term_tycons (g sg) t)
wenzelm@16337
   613
    (mapping add_term_consts (h sg) t);
wenzelm@16337
   614
wenzelm@16337
   615
in
wenzelm@14645
   616
wenzelm@16337
   617
val intern_typ = typ_mapping intern_class intern_tycon;
wenzelm@16337
   618
val extern_typ = typ_mapping extern_class extern_tycon;
wenzelm@16337
   619
val intern_term = term_mapping intern_class intern_tycon intern_const;
wenzelm@16337
   620
val extern_term = term_mapping extern_class extern_tycon extern_const;
wenzelm@16337
   621
val intern_tycons = typ_mapping (K I) intern_tycon;
wenzelm@16337
   622
wenzelm@16337
   623
end;
wenzelm@14645
   624
wenzelm@14645
   625
wenzelm@14645
   626
wenzelm@4249
   627
(** pretty printing of terms, types etc. **)
wenzelm@3937
   628
wenzelm@16337
   629
fun pretty_term' syn sg t = Syntax.pretty_term syn (exists_stamp CPureN sg) (extern_term sg t);
wenzelm@14974
   630
fun pretty_term sg t = pretty_term' (syn_of sg) sg t;
wenzelm@16337
   631
fun pretty_typ sg T = Syntax.pretty_typ (syn_of sg) (extern_typ sg T);
wenzelm@16337
   632
fun pretty_sort sg S = Syntax.pretty_sort (syn_of sg) (extern_sort sg S);
wenzelm@3937
   633
skalberg@15570
   634
fun pretty_classrel sg cs = Pretty.block (List.concat
wenzelm@14828
   635
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort sg o single) cs)));
wenzelm@3937
   636
wenzelm@16337
   637
fun pretty_arity sg (a, Ss, S) =
wenzelm@3937
   638
  let
wenzelm@16337
   639
    val a' = extern_tycon sg a;
wenzelm@3937
   640
    val dom =
wenzelm@3937
   641
      if null Ss then []
wenzelm@3937
   642
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@16337
   643
  in Pretty.block ([Pretty.str (a' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S]) end;
wenzelm@3937
   644
wenzelm@14828
   645
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@14828
   646
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@14828
   647
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@14828
   648
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@14828
   649
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@3937
   650
wenzelm@16337
   651
val pprint_term = (Pretty.pprint o Pretty.quote) oo pretty_term;
wenzelm@16337
   652
val pprint_typ = (Pretty.pprint o Pretty.quote) oo pretty_typ;
wenzelm@3937
   653
wenzelm@14974
   654
fun pp sg = Pretty.pp (pretty_term sg, pretty_typ sg, pretty_sort sg,
wenzelm@14974
   655
  pretty_classrel sg, pretty_arity sg);
wenzelm@14828
   656
wenzelm@3937
   657
wenzelm@3937
   658
wenzelm@16337
   659
(** certify entities **)    (*exception TYPE*)
wenzelm@8898
   660
wenzelm@16337
   661
(* certify wrt. type signature *)
wenzelm@8898
   662
wenzelm@16337
   663
fun certify cert = cert o tsig_of o check_stale "Sign.certify";
wenzelm@562
   664
wenzelm@16337
   665
val certify_class      = certify Type.cert_class;
wenzelm@16337
   666
val certify_sort       = certify Type.cert_sort;
wenzelm@16337
   667
val certify_typ        = certify Type.cert_typ;
wenzelm@16337
   668
val certify_typ_syntax = certify Type.cert_typ_syntax;
wenzelm@16337
   669
val certify_typ_abbrev = certify Type.cert_typ_abbrev;
wenzelm@10443
   670
wenzelm@4961
   671
wenzelm@4961
   672
(* certify_term *)
wenzelm@4961
   673
wenzelm@14987
   674
local
nipkow@1494
   675
wenzelm@16337
   676
(*determine and check the type of a term*)
wenzelm@16337
   677
fun type_check pp tm =
wenzelm@4961
   678
  let
wenzelm@4961
   679
    fun err_appl why bs t T u U =
wenzelm@4961
   680
      let
wenzelm@10404
   681
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   682
        val t' = subst_bounds (xs, t);
wenzelm@4961
   683
        val u' = subst_bounds (xs, u);
wenzelm@16337
   684
        val msg = cat_lines
wenzelm@14828
   685
          (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
wenzelm@16337
   686
      in raise TYPE (msg, [T, U], [t', u']) end;
wenzelm@4961
   687
wenzelm@4961
   688
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   689
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   690
      | typ_of (_, Var (_, T)) = T
skalberg@15570
   691
      | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
wenzelm@4961
   692
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   693
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   694
      | typ_of (bs, t $ u) =
wenzelm@4961
   695
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   696
            (case T of
wenzelm@4961
   697
              Type ("fun", [T1, T2]) =>
wenzelm@14828
   698
                if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
wenzelm@14828
   699
            | _ => err_appl "Operator not of function type" bs t T u U)
wenzelm@4961
   700
          end;
wenzelm@4961
   701
wenzelm@4961
   702
  in typ_of ([], tm) end;
clasohm@0
   703
wenzelm@14987
   704
in
wenzelm@14987
   705
wenzelm@14828
   706
fun certify_term pp sg tm =
wenzelm@251
   707
  let
wenzelm@16337
   708
    val _ = check_stale "Sign.certify_term" sg;
wenzelm@14784
   709
wenzelm@14993
   710
    val tm' = map_term_types (Type.cert_typ (tsig_of sg)) tm;
wenzelm@14784
   711
    val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
wenzelm@14784
   712
wenzelm@14784
   713
    fun err msg = raise TYPE (msg, [], [tm']);
wenzelm@3969
   714
wenzelm@14828
   715
    fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
wenzelm@169
   716
wenzelm@14784
   717
    fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
wenzelm@14784
   718
      | check_atoms (Abs (_, _, t)) = check_atoms t
wenzelm@14784
   719
      | check_atoms (Const (a, T)) =
wenzelm@14784
   720
          (case const_type sg a of
skalberg@15531
   721
            NONE => err ("Undeclared constant " ^ show_const a T)
skalberg@15531
   722
          | SOME U =>
wenzelm@14784
   723
              if typ_instance sg (T, U) then ()
wenzelm@14784
   724
              else err ("Illegal type for constant " ^ show_const a T))
wenzelm@14784
   725
      | check_atoms (Var ((x, i), _)) =
wenzelm@14784
   726
          if i < 0 then err ("Malformed variable: " ^ quote x) else ()
wenzelm@14784
   727
      | check_atoms _ = ();
wenzelm@14784
   728
  in
wenzelm@14784
   729
    check_atoms tm';
wenzelm@16337
   730
    (tm', type_check pp tm', maxidx_of_term tm')
wenzelm@14784
   731
  end;
wenzelm@251
   732
wenzelm@14987
   733
end;
wenzelm@14987
   734
wenzelm@251
   735
wenzelm@16337
   736
wenzelm@16337
   737
(** read and certify entities **)    (*exception ERROR*)
wenzelm@16337
   738
wenzelm@16337
   739
(* sorts *)
wenzelm@16337
   740
wenzelm@16337
   741
fun read_sort' syn sg str =
wenzelm@16337
   742
  let
wenzelm@16337
   743
    val _ = check_stale "Sign.read_sort'" sg;
wenzelm@16337
   744
    val S = intern_sort sg (Syntax.read_sort syn str);
wenzelm@16337
   745
  in Type.cert_sort (tsig_of sg) S handle TYPE (msg, _, _) => error msg end;
wenzelm@16337
   746
wenzelm@16337
   747
fun read_sort sg str = read_sort' (syn_of sg) sg str;
wenzelm@16337
   748
wenzelm@16337
   749
wenzelm@16337
   750
(* types *)
wenzelm@16337
   751
wenzelm@16337
   752
local
wenzelm@16337
   753
wenzelm@16337
   754
fun gen_read_typ' cert syn (sg, def_sort) str =
wenzelm@16337
   755
  let
wenzelm@16337
   756
    val _ = check_stale "Sign.gen_read_typ'" sg;
wenzelm@16337
   757
    val get_sort = TypeInfer.get_sort (tsig_of sg) def_sort (intern_sort sg);
wenzelm@16337
   758
    val T = intern_tycons sg (Syntax.read_typ syn get_sort (intern_sort sg) str);
wenzelm@16337
   759
  in cert (tsig_of sg) T handle TYPE (msg, _, _) => error msg end
wenzelm@16337
   760
  handle ERROR => error ("The error(s) above occurred in type " ^ quote str);
wenzelm@16337
   761
wenzelm@16337
   762
fun gen_read_typ cert (sg, def_sort) str = gen_read_typ' cert (syn_of sg) (sg, def_sort) str;
wenzelm@16337
   763
wenzelm@16337
   764
in
wenzelm@16337
   765
wenzelm@16337
   766
fun no_def_sort sg = (sg, K NONE);
wenzelm@16337
   767
wenzelm@16337
   768
val read_typ'        = gen_read_typ' Type.cert_typ;
wenzelm@16337
   769
val read_typ_syntax' = gen_read_typ' Type.cert_typ_syntax;
wenzelm@16337
   770
val read_typ_abbrev' = gen_read_typ' Type.cert_typ_abbrev;
wenzelm@16337
   771
val read_typ         = gen_read_typ Type.cert_typ;
wenzelm@16337
   772
val read_typ_syntax  = gen_read_typ Type.cert_typ_syntax;
wenzelm@16337
   773
val read_typ_abbrev  = gen_read_typ Type.cert_typ_abbrev;
wenzelm@16337
   774
wenzelm@16337
   775
end;
wenzelm@16337
   776
wenzelm@16337
   777
wenzelm@16337
   778
(* type and constant names *)    (*exception TYPE*)   (* FIXME really!? *)
wenzelm@15703
   779
wenzelm@15703
   780
fun read_tyname sg raw_c =
wenzelm@15703
   781
  let val c = intern_tycon sg raw_c in
wenzelm@15703
   782
    (case Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c) of
wenzelm@15703
   783
      SOME (Type.LogicalType n, _) => Type (c, replicate n dummyT)
wenzelm@15826
   784
    | _ => raise TYPE ("Undeclared type constructor: " ^ quote c, [], []))
wenzelm@15703
   785
  end;
wenzelm@15703
   786
wenzelm@15703
   787
fun read_const sg raw_c =
wenzelm@16288
   788
  let val c = intern_const sg raw_c
wenzelm@16288
   789
  in the_const_type sg c; Const (c, dummyT) end;
wenzelm@15703
   790
wenzelm@15703
   791
wenzelm@251
   792
wenzelm@583
   793
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   794
wenzelm@2979
   795
(*
wenzelm@16337
   796
  def_type: partial map from indexnames to types (constrains Frees and Vars)
wenzelm@16337
   797
  def_sort: partial map from indexnames to sorts (constrains TFrees and TVars)
wenzelm@2979
   798
  used: list of already used type variables
wenzelm@2979
   799
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   800
wenzelm@4249
   801
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   802
  typs: expected types
wenzelm@2979
   803
*)
wenzelm@2979
   804
wenzelm@14828
   805
fun infer_types_simult pp sg def_type def_sort used freeze args =
wenzelm@251
   806
  let
wenzelm@3975
   807
    val tsig = tsig_of sg;
wenzelm@4249
   808
skalberg@15574
   809
    val termss = foldr multiply [[]] (map fst args);
wenzelm@4249
   810
    val typs =
wenzelm@4249
   811
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   812
wenzelm@14828
   813
    fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) tsig
wenzelm@14828
   814
        (const_type sg) def_type def_sort (intern_const sg) (intern_tycons sg)
wenzelm@14828
   815
        (intern_sort sg) used freeze typs ts)
wenzelm@4249
   816
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   817
wenzelm@4249
   818
    val err_results = map infer termss;
skalberg@15570
   819
    val errs = List.mapPartial get_error err_results;
skalberg@15570
   820
    val results = List.mapPartial get_ok err_results;
wenzelm@4249
   821
wenzelm@14645
   822
    val ambiguity = length termss;
wenzelm@14645
   823
wenzelm@4249
   824
    fun ambig_msg () =
wenzelm@4249
   825
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   826
      then
wenzelm@4249
   827
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   828
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   829
      else ();
wenzelm@4249
   830
  in
wenzelm@4249
   831
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   832
    else if length results = 1 then
wenzelm@4249
   833
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   834
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   835
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   836
      else (); hd results)
wenzelm@4249
   837
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
skalberg@15570
   838
      cat_lines (map (Pretty.string_of_term pp) (List.concat (map fst results)))))
wenzelm@4249
   839
  end;
clasohm@623
   840
wenzelm@4249
   841
wenzelm@14828
   842
fun infer_types pp sg def_type def_sort used freeze tsT =
wenzelm@14828
   843
  apfst hd (infer_types_simult pp sg def_type def_sort used freeze [tsT]);
wenzelm@251
   844
wenzelm@251
   845
wenzelm@8607
   846
wenzelm@16337
   847
(** read_def_terms -- read terms and infer types **)
wenzelm@16337
   848
wenzelm@16337
   849
fun read_def_terms' pp is_logtype syn (sg, types, sorts) used freeze sTs =
wenzelm@8607
   850
  let
wenzelm@8607
   851
    fun read (s, T) =
wenzelm@16337
   852
      let val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg
wenzelm@14905
   853
      in (Syntax.read is_logtype syn T' s, T') end;
wenzelm@16337
   854
  in infer_types_simult pp sg types sorts used freeze (map read sTs) end;
wenzelm@8607
   855
wenzelm@16337
   856
fun read_def_terms (sg, types, sorts) =
wenzelm@16337
   857
  read_def_terms' (pp sg) (is_logtype sg) (syn_of sg) (sg, types, sorts);
wenzelm@12068
   858
wenzelm@16337
   859
fun simple_read_term sg T s =
wenzelm@16337
   860
  let val ([t], _) = read_def_terms (sg, K NONE, K NONE) [] true [(s, T)]
wenzelm@16337
   861
  in t end
wenzelm@16337
   862
  handle ERROR => error ("The error(s) above occurred for term " ^ s);
wenzelm@8802
   863
wenzelm@8607
   864
wenzelm@2979
   865
wenzelm@16337
   866
(** signature extension functions **)  (*exception ERROR/TYPE*)
wenzelm@386
   867
wenzelm@386
   868
(* add default sort *)
wenzelm@386
   869
wenzelm@16337
   870
fun gen_add_defsort prep_sort s sg = sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
   871
  (syn, Type.set_defsort (prep_sort sg s) tsig, consts, spaces));
wenzelm@8898
   872
wenzelm@16337
   873
val add_defsort = gen_add_defsort read_sort;
wenzelm@16337
   874
val add_defsort_i = gen_add_defsort certify_sort;
wenzelm@386
   875
wenzelm@386
   876
wenzelm@386
   877
(* add type constructors *)
wenzelm@386
   878
wenzelm@16337
   879
fun add_types types sg = sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@14856
   880
  let
wenzelm@14905
   881
    val syn' = map_syntax (Syntax.extend_type_gram types) syn;
wenzelm@16337
   882
    val decls = map (fn (a, n, mx) => (full_name sg (Syntax.type_name a mx), n)) types;
wenzelm@14856
   883
    val tsig' = Type.add_types decls tsig;
wenzelm@16337
   884
    val spaces' = declare_names sg typeK (map #1 decls) spaces;
wenzelm@16337
   885
  in (syn', tsig', consts, spaces') end);
wenzelm@16337
   886
wenzelm@16337
   887
wenzelm@16337
   888
(* add nonterminals *)
wenzelm@16337
   889
wenzelm@16337
   890
fun add_nonterminals bnames sg = sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
   891
  let
wenzelm@16337
   892
    val syn' = map_syntax (Syntax.extend_consts bnames) syn;
wenzelm@16337
   893
    val names = map (full_name sg) bnames;
wenzelm@16337
   894
    val tsig' = Type.add_nonterminals names tsig;
wenzelm@16337
   895
    val spaces' = declare_names sg typeK names spaces;
wenzelm@16337
   896
  in (syn', tsig', consts, spaces') end);
wenzelm@386
   897
wenzelm@386
   898
wenzelm@386
   899
(* add type abbreviations *)
wenzelm@386
   900
wenzelm@16337
   901
fun gen_add_tyabbr prep_typ (a, vs, rhs, mx) sg =
wenzelm@16337
   902
  sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
   903
    let
wenzelm@16337
   904
      val syn' = map_syntax (Syntax.extend_type_gram [(a, length vs, mx)]) syn;
wenzelm@16337
   905
      val a' = full_name sg (Syntax.type_name a mx);
wenzelm@16337
   906
      val abbr = (a', vs, prep_typ sg rhs) handle ERROR =>
wenzelm@16337
   907
        error ("in type abbreviation " ^ quote (Syntax.type_name a' mx));
wenzelm@16337
   908
      val tsig' = Type.add_abbrevs [abbr] tsig;
wenzelm@16337
   909
      val spaces' = declare_names sg typeK [a'] spaces;
wenzelm@16337
   910
    in (syn', tsig', consts, spaces') end);
wenzelm@386
   911
wenzelm@16337
   912
val add_tyabbrs = fold (gen_add_tyabbr (read_typ_syntax o no_def_sort));
wenzelm@16337
   913
val add_tyabbrs_i = fold (gen_add_tyabbr certify_typ_syntax);
wenzelm@14784
   914
wenzelm@14784
   915
wenzelm@386
   916
(* add type arities *)
wenzelm@386
   917
wenzelm@16337
   918
fun gen_add_arities int_tycon prep_sort arities sg =
wenzelm@16337
   919
  sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
   920
    let
wenzelm@16337
   921
      fun prep_arity (a, Ss, S) = (int_tycon sg a, map (prep_sort sg) Ss, prep_sort sg S)
wenzelm@16337
   922
        handle ERROR => error ("in arity for type " ^ quote a);
wenzelm@16337
   923
      val tsig' = Type.add_arities (pp sg) (map prep_arity arities) tsig;
wenzelm@16337
   924
    in (syn, tsig', consts, spaces) end);
wenzelm@386
   925
wenzelm@16337
   926
val add_arities = gen_add_arities intern_tycon read_sort;
wenzelm@16337
   927
val add_arities_i = gen_add_arities (K I) certify_sort;
wenzelm@8898
   928
wenzelm@386
   929
wenzelm@16337
   930
(* modify syntax *)
wenzelm@16337
   931
wenzelm@16337
   932
fun gen_syntax change_gram prep_typ (prmode, args) sg =
wenzelm@16337
   933
  let
wenzelm@16337
   934
    fun prep (c, T, mx) = (c, prep_typ sg T, mx) handle ERROR =>
wenzelm@16337
   935
      error ("in syntax declaration " ^ quote (Syntax.const_name c mx));
wenzelm@16337
   936
  in sg |> map_syn (map_syntax (change_gram (is_logtype sg) prmode (map prep args))) end;
wenzelm@16337
   937
wenzelm@16337
   938
fun gen_add_syntax x = gen_syntax Syntax.extend_const_gram x;
wenzelm@386
   939
wenzelm@16337
   940
val add_modesyntax = gen_add_syntax (read_typ_syntax o no_def_sort);
wenzelm@16337
   941
val add_modesyntax_i = gen_add_syntax certify_typ_syntax;
wenzelm@16337
   942
val add_syntax = curry add_modesyntax Syntax.default_mode;
wenzelm@16337
   943
val add_syntax_i = curry add_modesyntax_i Syntax.default_mode;
wenzelm@16337
   944
val del_modesyntax = gen_syntax Syntax.remove_const_gram (read_typ_syntax o no_def_sort);
wenzelm@16337
   945
val del_modesyntax_i = gen_syntax Syntax.remove_const_gram certify_typ_syntax;
wenzelm@3805
   946
wenzelm@16337
   947
wenzelm@16337
   948
(* add constants *)
wenzelm@386
   949
wenzelm@386
   950
fun err_dup_consts cs =
wenzelm@386
   951
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   952
wenzelm@16337
   953
fun gen_add_consts prep_typ raw_args sg =
wenzelm@386
   954
  let
wenzelm@16337
   955
    val prepT = compress_type o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ sg;
wenzelm@16337
   956
    fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
wenzelm@16337
   957
      handle ERROR => error ("in declaration of constant " ^ quote (Syntax.const_name c mx));
wenzelm@16337
   958
    val args = map prep raw_args;
wenzelm@16337
   959
    val decls = args |> map (fn (c, T, mx) =>
wenzelm@16337
   960
      (full_name sg (Syntax.const_name c mx), (T, stamp ())));
wenzelm@386
   961
  in
wenzelm@16337
   962
    sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
   963
      (syn, tsig,
wenzelm@16337
   964
        Symtab.extend (consts, decls) handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@16337
   965
        declare_names sg constK (map #1 decls) spaces))
wenzelm@16337
   966
    |> add_syntax_i args
wenzelm@386
   967
  end;
wenzelm@386
   968
wenzelm@16337
   969
val add_consts = gen_add_consts (read_typ o no_def_sort);
wenzelm@16337
   970
val add_consts_i = gen_add_consts certify_typ;
wenzelm@386
   971
wenzelm@386
   972
wenzelm@386
   973
(* add type classes *)
wenzelm@386
   974
wenzelm@16337
   975
val classN = "_class";
wenzelm@386
   976
wenzelm@16337
   977
val const_of_class = suffix classN;
wenzelm@16337
   978
fun class_of_const c = unsuffix classN c
wenzelm@16337
   979
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
wenzelm@386
   980
wenzelm@16337
   981
fun gen_add_class int_class (bclass, raw_classes) sg =
wenzelm@16337
   982
  sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
   983
    let
wenzelm@16337
   984
      val class = full_name sg bclass;
wenzelm@16337
   985
      val classes = map (int_class sg) raw_classes;
wenzelm@16337
   986
      val syn' = map_syntax (Syntax.extend_consts [bclass]) syn;
wenzelm@16337
   987
      val tsig' = Type.add_classes (pp sg) [(class, classes)] tsig;
wenzelm@16337
   988
      val spaces' = declare_names sg classK [class] spaces;
wenzelm@16337
   989
    in (syn', tsig', consts, spaces') end)
wenzelm@16337
   990
  |> add_consts_i [(const_of_class bclass, a_itselfT --> propT, Syntax.NoSyn)];
wenzelm@3791
   991
wenzelm@16337
   992
val add_classes = fold (gen_add_class intern_class);
wenzelm@16337
   993
val add_classes_i = fold (gen_add_class (K I));
wenzelm@386
   994
wenzelm@386
   995
wenzelm@2963
   996
(* add to classrel *)
wenzelm@421
   997
wenzelm@16337
   998
fun gen_add_classrel int_class raw_pairs sg =
wenzelm@16337
   999
  sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
  1000
    let
wenzelm@16337
  1001
      val pairs = map (pairself (int_class sg)) raw_pairs;
wenzelm@16337
  1002
      val tsig' = Type.add_classrel (pp sg) pairs tsig;
wenzelm@16337
  1003
    in (syn, tsig', consts, spaces) end);
wenzelm@16337
  1004
wenzelm@16337
  1005
val add_classrel = gen_add_classrel intern_class;
wenzelm@16337
  1006
val add_classrel_i = gen_add_classrel (K I);
wenzelm@421
  1007
wenzelm@421
  1008
wenzelm@14645
  1009
(* add translation functions *)
wenzelm@14645
  1010
wenzelm@15746
  1011
local
wenzelm@15746
  1012
wenzelm@15746
  1013
fun mk trs = map Syntax.mk_trfun trs;
wenzelm@15746
  1014
wenzelm@16337
  1015
fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) sg = sg |> map_syn (fn syn =>
wenzelm@15746
  1016
  let val syn' = syn |> ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's)
wenzelm@16337
  1017
  in make_syntax sg syn'; syn' end);
wenzelm@14645
  1018
wenzelm@16337
  1019
fun gen_add_trfunsT ext tr's sg = sg |> map_syn (fn syn =>
wenzelm@15746
  1020
  let val syn' = syn |> ext ([], [], mk tr's, [])
wenzelm@16337
  1021
  in make_syntax sg syn'; syn' end);
wenzelm@14645
  1022
wenzelm@15746
  1023
in
wenzelm@15746
  1024
wenzelm@16337
  1025
val add_trfuns = gen_add_trfuns (map_syntax o Syntax.extend_trfuns) Syntax.non_typed_tr';
wenzelm@16337
  1026
val add_trfunsT = gen_add_trfunsT (map_syntax o Syntax.extend_trfuns);
wenzelm@16337
  1027
val add_advanced_trfuns = gen_add_trfuns extend_trfuns Syntax.non_typed_tr'';
wenzelm@16337
  1028
val add_advanced_trfunsT = gen_add_trfunsT extend_trfuns;
wenzelm@14645
  1029
wenzelm@15746
  1030
end;
wenzelm@15746
  1031
wenzelm@16337
  1032
val add_tokentrfuns = map_syn o map_syntax o Syntax.extend_tokentrfuns;
wenzelm@14645
  1033
wenzelm@14645
  1034
wenzelm@4619
  1035
(* add translation rules *)
wenzelm@4619
  1036
wenzelm@16337
  1037
fun add_trrules args sg = sg |> map_syn (fn syn =>
wenzelm@16337
  1038
  let val rules = map (Syntax.map_trrule (apfst (intern_tycon sg))) args
wenzelm@16337
  1039
  in map_syntax (Syntax.extend_trrules (is_logtype sg) (make_syntax sg syn) rules) syn end);
wenzelm@8725
  1040
wenzelm@16337
  1041
val add_trrules_i = map_syn o map_syntax o Syntax.extend_trrules_i;
wenzelm@386
  1042
wenzelm@386
  1043
wenzelm@16337
  1044
(* modify naming *)
wenzelm@6546
  1045
wenzelm@16337
  1046
val add_path        = map_naming o NameSpace.add_path;
wenzelm@16337
  1047
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@16337
  1048
val no_base_names   = map_naming NameSpace.no_base_names;
wenzelm@16337
  1049
val custom_accesses = map_naming o NameSpace.custom_accesses;
wenzelm@16337
  1050
val set_policy      = map_naming o NameSpace.set_policy;
wenzelm@16337
  1051
val restore_naming  = map_naming o K o naming_of;
wenzelm@6546
  1052
wenzelm@6546
  1053
wenzelm@16337
  1054
(* hide names *)
wenzelm@386
  1055
wenzelm@16337
  1056
fun hide_space fully (kind, xnames) sg = sg |> map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
  1057
  let
wenzelm@16337
  1058
    val names = map (intern sg kind) xnames;
wenzelm@16337
  1059
    val spaces' = hide_names kind fully names spaces;
wenzelm@16337
  1060
  in (syn, tsig, consts, spaces') end);
wenzelm@16337
  1061
wenzelm@16337
  1062
fun hide_space_i fully (kind, names) = map_sign (fn (syn, tsig, consts, spaces) =>
wenzelm@16337
  1063
  (syn, tsig, consts, hide_names kind fully names spaces));
wenzelm@386
  1064
wenzelm@386
  1065
wenzelm@3867
  1066
wenzelm@16337
  1067
(** merge signatures **)
wenzelm@4228
  1068
wenzelm@4228
  1069
fun merge_stamps stamps1 stamps2 =
wenzelm@12296
  1070
  let val stamps = merge_lists' stamps1 stamps2 in
wenzelm@4228
  1071
    (case duplicates (map ! stamps) of
wenzelm@4228
  1072
      [] => stamps
wenzelm@15826
  1073
    | dups => error ("Attempt to merge different versions of theories " ^ commas_quote dups))
wenzelm@4228
  1074
  end;
wenzelm@4228
  1075
wenzelm@4228
  1076
wenzelm@16337
  1077
(* trivial merge *)
wenzelm@3877
  1078
skalberg@15531
  1079
fun merge_refs (sgr1 as SgRef (SOME (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
skalberg@15531
  1080
        sgr2 as SgRef (SOME (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
wenzelm@15703
  1081
      if subsig (sg2, sg1) then sgr1
wenzelm@3967
  1082
      else if subsig (sg1, sg2) then sgr2
wenzelm@16337
  1083
      else (merge_stamps s1 s2;  (*check for different versions*)
wenzelm@16337
  1084
        error ("Attempt to do non-trivial merge of signature\n" ^
wenzelm@16337
  1085
          str_of_sg sg1 ^ " and " ^ str_of_sg sg2))
wenzelm@3967
  1086
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
  1087
wenzelm@4627
  1088
val merge = deref o merge_refs o pairself self_ref;
wenzelm@4627
  1089
wenzelm@3867
  1090
wenzelm@16337
  1091
(* non-trivial merge *)              (*exception TERM/ERROR*)
wenzelm@15703
  1092
wenzelm@15703
  1093
local
wenzelm@10932
  1094
wenzelm@6961
  1095
fun nontriv_merge (sg1, sg2) =
wenzelm@15703
  1096
  if subsig_internal (sg2, sg1) then sg1
wenzelm@15703
  1097
  else if subsig_internal (sg1, sg2) then sg2
wenzelm@15703
  1098
  else
wenzelm@15801
  1099
    if exists_stamp CPureN sg1 <> exists_stamp CPureN sg2
wenzelm@15801
  1100
    then error "Cannot merge Pure and CPure developments"
wenzelm@251
  1101
  else
wenzelm@251
  1102
    let
wenzelm@14645
  1103
      val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
wenzelm@14784
  1104
          {self = _, tsig = tsig1, consts = consts1,
wenzelm@16133
  1105
            naming = _, spaces = spaces1, data = data1}) = sg1;
wenzelm@14645
  1106
      val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
wenzelm@14784
  1107
          {self = _, tsig = tsig2, consts = consts2,
wenzelm@16133
  1108
            naming = _, spaces = spaces2, data = data2}) = sg2;
wenzelm@386
  1109
wenzelm@4228
  1110
      val stamps = merge_stamps stamps1 stamps2;
wenzelm@14645
  1111
      val syntax = Syntax.merge_syntaxes syntax1 syntax2;
wenzelm@14645
  1112
      val trfuns = merge_trfuns trfuns1 trfuns2;
wenzelm@16337
  1113
      val syn = Syn (syntax, trfuns);
wenzelm@14784
  1114
      val consts = Symtab.merge eq_snd (consts1, consts2)
wenzelm@14784
  1115
        handle Symtab.DUPS cs => err_dup_consts cs;
wenzelm@16133
  1116
      val naming = NameSpace.default_naming;
wenzelm@16337
  1117
      val spaces = merge_spaces (spaces1, spaces2);
wenzelm@4256
  1118
      val data = merge_data (data1, data2);
wenzelm@3967
  1119
wenzelm@16337
  1120
      val pre_sg = make_sg (ref "", SgRef (SOME (ref dummy_sg)), ref "#" :: stamps1)
wenzelm@16337
  1121
        naming data (syn, tsig1, consts, spaces);
wenzelm@16337
  1122
      val tsig = Type.merge_tsigs (pp pre_sg) (tsig1, tsig2);
wenzelm@14905
  1123
wenzelm@16337
  1124
      val self = SgRef (SOME (ref dummy_sg));
wenzelm@16337
  1125
      val sg = make_sg (ref "", self, stamps) naming data (syn, tsig, consts, spaces);
wenzelm@16337
  1126
    in assign self sg; syn_of sg; sg end;
wenzelm@3791
  1127
wenzelm@15703
  1128
in
wenzelm@15703
  1129
wenzelm@15703
  1130
fun prep_ext_merge sgs =
wenzelm@15703
  1131
  if null sgs then
wenzelm@15703
  1132
    error "Merge: no parent theories"
wenzelm@15703
  1133
  else if exists is_draft sgs then
wenzelm@15703
  1134
    error "Attempt to merge draft theories"
wenzelm@15703
  1135
  else
wenzelm@15703
  1136
    Library.foldl nontriv_merge (hd sgs, tl sgs)
wenzelm@15703
  1137
    |> prep_ext
wenzelm@15703
  1138
    |> add_path "/";
wenzelm@15703
  1139
clasohm@0
  1140
end;
wenzelm@15703
  1141
wenzelm@15703
  1142
end;