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