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