src/Pure/Syntax/syntax.ML
author wenzelm
Sat Sep 29 21:39:53 2007 +0200 (2007-09-29)
changeset 24768 123e219b66c2
parent 24709 ecfb9dcb6c4c
child 24923 9e095546cdac
permissions -rw-r--r--
added unparse interfaces (still unused);
add_typ/term_check: added stage and name argument;
added print_checks;
wenzelm@18
     1
(*  Title:      Pure/Syntax/syntax.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@18
     4
wenzelm@24263
     5
Standard Isabelle syntax, based on arbitrary context-free grammars
wenzelm@24263
     6
(specified by mixfix declarations).
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@260
     9
signature BASIC_SYNTAX =
wenzelm@2383
    10
sig
wenzelm@260
    11
  include AST0
wenzelm@556
    12
  include SYN_TRANS0
wenzelm@556
    13
  include MIXFIX0
wenzelm@260
    14
  include PRINTER0
wenzelm@2383
    15
end;
wenzelm@260
    16
clasohm@0
    17
signature SYNTAX =
wenzelm@2383
    18
sig
wenzelm@260
    19
  include AST1
clasohm@0
    20
  include LEXICON0
wenzelm@237
    21
  include SYN_EXT0
clasohm@0
    22
  include TYPE_EXT0
wenzelm@556
    23
  include SYN_TRANS1
wenzelm@556
    24
  include MIXFIX1
clasohm@0
    25
  include PRINTER0
wenzelm@15755
    26
  val extend_trtab: string -> (string * ('a * stamp)) list ->
wenzelm@15755
    27
    ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table
wenzelm@15755
    28
  val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table ->
wenzelm@15755
    29
    ('a * stamp) Symtab.table
wenzelm@14648
    30
  val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
wenzelm@14648
    31
    -> ('a * stamp) list Symtab.table
wenzelm@15755
    32
  val extend_tr'tab: (string * ('a * stamp)) list ->
wenzelm@15755
    33
    ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
wenzelm@1158
    34
  datatype 'a trrule =
wenzelm@3526
    35
    ParseRule of 'a * 'a |
wenzelm@3526
    36
    PrintRule of 'a * 'a |
wenzelm@3526
    37
    ParsePrintRule of 'a * 'a
wenzelm@556
    38
  type syntax
wenzelm@17079
    39
  val eq_syntax: syntax * syntax -> bool
wenzelm@14687
    40
  val is_keyword: syntax -> string -> bool
wenzelm@20784
    41
  type mode
wenzelm@20784
    42
  val default_mode: mode
wenzelm@21702
    43
  val input_mode: mode
wenzelm@21826
    44
  val internalM: string
wenzelm@12073
    45
  val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
wenzelm@14904
    46
  val extend_const_gram: (string -> bool) ->
wenzelm@20784
    47
    mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@12073
    48
  val extend_consts: string list -> syntax -> syntax
wenzelm@12073
    49
  val extend_trfuns:
wenzelm@15755
    50
    (string * ((ast list -> ast) * stamp)) list *
wenzelm@15755
    51
    (string * ((term list -> term) * stamp)) list *
wenzelm@15755
    52
    (string * ((bool -> typ -> term list -> term) * stamp)) list *
wenzelm@15755
    53
    (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
wenzelm@16613
    54
  val extend_advanced_trfuns:
wenzelm@21772
    55
    (string * ((Proof.context -> ast list -> ast) * stamp)) list *
wenzelm@21772
    56
    (string * ((Proof.context -> term list -> term) * stamp)) list *
wenzelm@21772
    57
    (string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list *
wenzelm@21772
    58
    (string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax
wenzelm@23660
    59
  val extend_tokentrfuns: (string * string * (string -> output * int)) list -> syntax -> syntax
wenzelm@19262
    60
  val remove_const_gram: (string -> bool) ->
wenzelm@20784
    61
    mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@21772
    62
  val extend_trrules: Proof.context -> (string -> bool) -> syntax ->
wenzelm@14904
    63
    (string * string) trrule list -> syntax -> syntax
wenzelm@21772
    64
  val remove_trrules: Proof.context -> (string -> bool) -> syntax ->
wenzelm@19262
    65
    (string * string) trrule list -> syntax -> syntax
wenzelm@19262
    66
  val extend_trrules_i: ast trrule list -> syntax -> syntax
wenzelm@19262
    67
  val remove_trrules_i: ast trrule list -> syntax -> syntax
wenzelm@4618
    68
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
wenzelm@556
    69
  val merge_syntaxes: syntax -> syntax -> syntax
wenzelm@18720
    70
  val basic_syn: syntax
wenzelm@18720
    71
  val basic_nonterms: string list
wenzelm@556
    72
  val print_gram: syntax -> unit
wenzelm@556
    73
  val print_trans: syntax -> unit
wenzelm@556
    74
  val print_syntax: syntax -> unit
wenzelm@21772
    75
  val read: Proof.context -> (string -> bool) -> syntax -> typ -> string -> term list
wenzelm@24372
    76
  val standard_parse_term: Pretty.pp -> (term -> term Exn.result) ->
wenzelm@22703
    77
    (((string * int) * sort) list -> string * int -> Term.sort) ->
wenzelm@22703
    78
    (string -> string option) -> (string -> string option) ->
wenzelm@22703
    79
    (typ -> typ) -> (sort -> sort) -> Proof.context ->
wenzelm@24372
    80
    (string -> bool) -> syntax -> typ -> string -> term
wenzelm@24278
    81
  val standard_parse_typ: Proof.context -> syntax -> ((indexname * sort) list -> indexname -> sort) ->
wenzelm@16613
    82
    (sort -> sort) -> string -> typ
wenzelm@24278
    83
  val standard_parse_sort: Proof.context -> syntax -> (sort -> sort) -> string -> sort
wenzelm@21772
    84
  val pretty_term: (string -> xstring) -> Proof.context -> syntax -> bool -> term -> Pretty.T
wenzelm@21772
    85
  val pretty_typ: Proof.context -> syntax -> typ -> Pretty.T
wenzelm@21772
    86
  val pretty_sort: Proof.context -> syntax -> sort -> Pretty.T
clasohm@882
    87
  val ambiguity_level: int ref
skalberg@14177
    88
  val ambiguity_is_error: bool ref
wenzelm@24263
    89
  val parse_sort: Proof.context -> string -> sort
wenzelm@24263
    90
  val parse_typ: Proof.context -> string -> typ
wenzelm@24263
    91
  val parse_term: Proof.context -> string -> term
wenzelm@24263
    92
  val parse_prop: Proof.context -> string -> term
wenzelm@24768
    93
  val unparse_sort: Proof.context -> sort -> Pretty.T
wenzelm@24768
    94
  val unparse_typ: Proof.context -> typ -> Pretty.T
wenzelm@24768
    95
  val unparse_term: Proof.context -> term -> Pretty.T
wenzelm@24768
    96
  val install_operations:
wenzelm@24768
    97
   {parse_sort: Proof.context -> string -> sort,
wenzelm@24768
    98
    parse_typ: Proof.context -> string -> typ,
wenzelm@24768
    99
    parse_term: Proof.context -> string -> term,
wenzelm@24768
   100
    parse_prop: Proof.context -> string -> term,
wenzelm@24768
   101
    unparse_sort: Proof.context -> sort -> Pretty.T,
wenzelm@24768
   102
    unparse_typ: Proof.context -> typ -> Pretty.T,
wenzelm@24768
   103
    unparse_term: Proof.context -> term -> Pretty.T} -> unit
wenzelm@24768
   104
  val print_checks: Proof.context -> unit
wenzelm@24768
   105
  val add_typ_check: int -> string ->
wenzelm@24768
   106
    (typ list -> Proof.context -> typ list * Proof.context) ->
wenzelm@24768
   107
    Context.generic -> Context.generic
wenzelm@24768
   108
  val add_term_check: int -> string ->
wenzelm@24768
   109
    (term list -> Proof.context -> term list * Proof.context) ->
wenzelm@24768
   110
    Context.generic -> Context.generic
wenzelm@24768
   111
  val add_typ_uncheck: int -> string ->
wenzelm@24768
   112
    (typ list -> Proof.context -> typ list * Proof.context) ->
wenzelm@24768
   113
    Context.generic -> Context.generic
wenzelm@24768
   114
  val add_term_uncheck: int -> string ->
wenzelm@24768
   115
    (term list -> Proof.context -> term list * Proof.context) ->
wenzelm@24768
   116
    Context.generic -> Context.generic
wenzelm@24488
   117
  val check_sort: Proof.context -> sort -> sort
wenzelm@24512
   118
  val check_typ: Proof.context -> typ -> typ
wenzelm@24512
   119
  val check_term: Proof.context -> term -> term
wenzelm@24512
   120
  val check_prop: Proof.context -> term -> term
wenzelm@24488
   121
  val check_typs: Proof.context -> typ list -> typ list
wenzelm@24263
   122
  val check_terms: Proof.context -> term list -> term list
wenzelm@24263
   123
  val check_props: Proof.context -> term list -> term list
wenzelm@24768
   124
  val uncheck_typs: Proof.context -> typ list -> typ list
wenzelm@24768
   125
  val uncheck_terms: Proof.context -> term list -> term list
wenzelm@24263
   126
  val read_sort: Proof.context -> string -> sort
wenzelm@24263
   127
  val read_typ: Proof.context -> string -> typ
wenzelm@24488
   128
  val read_term: Proof.context -> string -> term
wenzelm@24488
   129
  val read_prop: Proof.context -> string -> term
wenzelm@24263
   130
  val read_terms: Proof.context -> string list -> term list
wenzelm@24263
   131
  val read_props: Proof.context -> string list -> term list
wenzelm@24709
   132
  val read_sort_global: theory -> string -> sort
wenzelm@24709
   133
  val read_typ_global: theory -> string -> typ
wenzelm@24709
   134
  val read_term_global: theory -> string -> term
wenzelm@24709
   135
  val read_prop_global: theory -> string -> term
wenzelm@2383
   136
end;
clasohm@0
   137
wenzelm@12094
   138
structure Syntax: SYNTAX =
clasohm@0
   139
struct
clasohm@0
   140
wenzelm@237
   141
(** tables of translation functions **)
wenzelm@237
   142
wenzelm@5692
   143
(* parse (ast) translations *)
wenzelm@5692
   144
wenzelm@17412
   145
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
wenzelm@237
   146
wenzelm@23655
   147
fun err_dup_trfun name c =
wenzelm@23655
   148
  error ("More than one " ^ name ^ " for " ^ quote c);
wenzelm@237
   149
wenzelm@21536
   150
fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns;
wenzelm@21536
   151
wenzelm@21536
   152
fun extend_trtab name trfuns tab = Symtab.extend (remove_trtab trfuns tab, trfuns)
wenzelm@23655
   153
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   154
wenzelm@15755
   155
fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
wenzelm@23655
   156
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   157
wenzelm@237
   158
wenzelm@5692
   159
(* print (ast) translations *)
wenzelm@5692
   160
wenzelm@18931
   161
fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c);
wenzelm@18931
   162
fun extend_tr'tab trfuns = fold_rev Symtab.update_list trfuns;
wenzelm@18931
   163
fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns;
wenzelm@18931
   164
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2);
wenzelm@5692
   165
wenzelm@5692
   166
wenzelm@237
   167
wenzelm@2700
   168
(** tables of token translation functions **)
wenzelm@2700
   169
wenzelm@2700
   170
fun lookup_tokentr tabs modes =
wenzelm@19482
   171
  let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""]))
haftmann@17314
   172
  in fn c => Option.map fst (AList.lookup (op =) trs c) end;
wenzelm@2700
   173
wenzelm@2700
   174
fun merge_tokentrtabs tabs1 tabs2 =
wenzelm@2700
   175
  let
wenzelm@2700
   176
    fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
wenzelm@2700
   177
wenzelm@4703
   178
    fun name (s, _) = implode (tl (Symbol.explode s));
wenzelm@2706
   179
wenzelm@2700
   180
    fun merge mode =
wenzelm@2700
   181
      let
wenzelm@17213
   182
        val trs1 = these (AList.lookup (op =) tabs1 mode);
wenzelm@17213
   183
        val trs2 = these (AList.lookup (op =) tabs2 mode);
wenzelm@19046
   184
        val trs = distinct eq_tr (trs1 @ trs2);
wenzelm@2700
   185
      in
wenzelm@18964
   186
        (case duplicates (eq_fst (op =)) trs of
wenzelm@2700
   187
          [] => (mode, trs)
wenzelm@2700
   188
        | dups => error ("More than one token translation function in mode " ^
wenzelm@2706
   189
            quote mode ^ " for " ^ commas_quote (map name dups)))
wenzelm@2700
   190
      end;
wenzelm@19046
   191
  in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end;
wenzelm@2700
   192
wenzelm@15755
   193
fun extend_tokentrtab tokentrs tabs =
wenzelm@2700
   194
  let
wenzelm@17213
   195
    fun ins_tokentr (m, c, f) =
wenzelm@17213
   196
      AList.default (op =) (m, [])
wenzelm@17213
   197
      #> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ())));
wenzelm@15759
   198
  in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
wenzelm@2700
   199
wenzelm@2700
   200
wenzelm@2700
   201
wenzelm@237
   202
(** tables of translation rules **)
wenzelm@237
   203
wenzelm@5692
   204
type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
wenzelm@237
   205
wenzelm@19482
   206
fun dest_ruletab tab = maps snd (Symtab.dest tab);
wenzelm@237
   207
wenzelm@237
   208
wenzelm@237
   209
(* empty, extend, merge ruletabs *)
wenzelm@237
   210
wenzelm@18931
   211
val extend_ruletab = fold_rev (fn r => Symtab.update_list (Ast.head_of_rule r, r));
wenzelm@18931
   212
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
wenzelm@18931
   213
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
clasohm@0
   214
clasohm@0
   215
clasohm@0
   216
clasohm@0
   217
(** datatype syntax **)
clasohm@0
   218
wenzelm@237
   219
datatype syntax =
wenzelm@237
   220
  Syntax of {
wenzelm@15755
   221
    input: SynExt.xprod list,
wenzelm@4703
   222
    lexicon: Scan.lexicon,
wenzelm@5692
   223
    gram: Parser.gram,
wenzelm@18
   224
    consts: string list,
wenzelm@2913
   225
    prmodes: string list,
wenzelm@21772
   226
    parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
wenzelm@237
   227
    parse_ruletab: ruletab,
wenzelm@21772
   228
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
wenzelm@21772
   229
    print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table,
wenzelm@237
   230
    print_ruletab: ruletab,
wenzelm@21772
   231
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
wenzelm@23660
   232
    tokentrtab: (string * (string * ((string -> output * int) * stamp)) list) list,
wenzelm@17079
   233
    prtabs: Printer.prtabs} * stamp;
clasohm@0
   234
wenzelm@17079
   235
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
wenzelm@17079
   236
wenzelm@17079
   237
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
wenzelm@14687
   238
wenzelm@20784
   239
type mode = string * bool;
wenzelm@15755
   240
val default_mode = ("", true);
wenzelm@21702
   241
val input_mode = ("input", true);
wenzelm@21826
   242
val internalM = "internal";
wenzelm@15755
   243
wenzelm@18
   244
wenzelm@237
   245
(* empty_syntax *)
wenzelm@18
   246
wenzelm@17079
   247
val empty_syntax = Syntax
wenzelm@17079
   248
  ({input = [],
wenzelm@4703
   249
    lexicon = Scan.empty_lexicon,
wenzelm@5692
   250
    gram = Parser.empty_gram,
wenzelm@167
   251
    consts = [],
wenzelm@2913
   252
    prmodes = [],
wenzelm@5692
   253
    parse_ast_trtab = Symtab.empty,
wenzelm@5692
   254
    parse_ruletab = Symtab.empty,
wenzelm@5692
   255
    parse_trtab = Symtab.empty,
wenzelm@5692
   256
    print_trtab = Symtab.empty,
wenzelm@5692
   257
    print_ruletab = Symtab.empty,
wenzelm@5692
   258
    print_ast_trtab = Symtab.empty,
wenzelm@2700
   259
    tokentrtab = [],
wenzelm@17079
   260
    prtabs = Printer.empty_prtabs}, stamp ());
wenzelm@167
   261
wenzelm@167
   262
wenzelm@237
   263
(* extend_syntax *)
wenzelm@167
   264
wenzelm@17079
   265
fun extend_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@167
   266
  let
wenzelm@15755
   267
    val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
wenzelm@2366
   268
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@2700
   269
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@14904
   270
    val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
wenzelm@2913
   271
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@2700
   272
      print_ast_translation, token_translation} = syn_ext;
wenzelm@19546
   273
    fun if_inout xs = if inout then xs else [];
wenzelm@167
   274
  in
wenzelm@17079
   275
    Syntax
wenzelm@19546
   276
    ({input = if_inout xprods @ input,
wenzelm@22121
   277
      lexicon = Scan.extend_lexicon (if_inout (SynExt.delims_of xprods)) lexicon,
wenzelm@19546
   278
      gram = Parser.extend_gram gram (if_inout xprods),
wenzelm@19546
   279
      consts = Library.merge (op =) (consts1, filter_out NameSpace.is_qualified consts2),
wenzelm@18921
   280
      prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)),
wenzelm@167
   281
      parse_ast_trtab =
wenzelm@19546
   282
        extend_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@19546
   283
      parse_ruletab = extend_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@19546
   284
      parse_trtab = extend_trtab "parse translation" (if_inout parse_translation) parse_trtab,
wenzelm@15755
   285
      print_trtab = extend_tr'tab print_translation print_trtab,
wenzelm@15755
   286
      print_ruletab = extend_ruletab print_rules print_ruletab,
wenzelm@15755
   287
      print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
wenzelm@15755
   288
      tokentrtab = extend_tokentrtab token_translation tokentrtab,
wenzelm@17079
   289
      prtabs = Printer.extend_prtabs mode xprods prtabs}, stamp ())
wenzelm@18
   290
  end;
wenzelm@18
   291
wenzelm@15755
   292
wenzelm@15755
   293
(* remove_syntax *)
wenzelm@15755
   294
wenzelm@17079
   295
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@15755
   296
  let
wenzelm@15755
   297
    val SynExt.SynExt {xprods, consts = _, prmodes = _,
wenzelm@15755
   298
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@15755
   299
      print_ast_translation, token_translation = _} = syn_ext;
wenzelm@15755
   300
    val {input, lexicon, gram, consts, prmodes,
wenzelm@15755
   301
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@15755
   302
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@19300
   303
    val input' = if inout then subtract (op =) xprods input else input;
wenzelm@19546
   304
    fun if_inout xs = if inout then xs else [];
wenzelm@15755
   305
  in
wenzelm@17079
   306
    Syntax
wenzelm@17079
   307
    ({input = input',
wenzelm@15755
   308
      lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
wenzelm@15755
   309
      gram = if inout then Parser.make_gram input' else gram,
wenzelm@15755
   310
      consts = consts,
wenzelm@15755
   311
      prmodes = prmodes,
wenzelm@19546
   312
      parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@19546
   313
      parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@19546
   314
      parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
wenzelm@15755
   315
      print_trtab = remove_tr'tab print_translation print_trtab,
wenzelm@15755
   316
      print_ruletab = remove_ruletab print_rules print_ruletab,
wenzelm@15755
   317
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
wenzelm@15755
   318
      tokentrtab = tokentrtab,
wenzelm@17079
   319
      prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
wenzelm@15755
   320
  end;
wenzelm@14904
   321
wenzelm@18
   322
wenzelm@237
   323
(* merge_syntaxes *)
clasohm@0
   324
wenzelm@17079
   325
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
clasohm@0
   326
  let
wenzelm@15755
   327
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
wenzelm@15755
   328
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
wenzelm@15755
   329
      parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
wenzelm@15755
   330
      print_trtab = print_trtab1, print_ruletab = print_ruletab1,
wenzelm@15755
   331
      print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
wenzelm@237
   332
wenzelm@15755
   333
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
wenzelm@15755
   334
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
wenzelm@15755
   335
      parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
wenzelm@15755
   336
      print_trtab = print_trtab2, print_ruletab = print_ruletab2,
wenzelm@15755
   337
      print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
clasohm@0
   338
  in
wenzelm@17079
   339
    Syntax
wenzelm@18921
   340
    ({input = Library.merge (op =) (input1, input2),
wenzelm@4703
   341
      lexicon = Scan.merge_lexicons lexicon1 lexicon2,
wenzelm@5692
   342
      gram = Parser.merge_grams gram1 gram2,
wenzelm@18428
   343
      consts = sort_distinct string_ord (consts1 @ consts2),
wenzelm@18921
   344
      prmodes = Library.merge (op =) (prmodes1, prmodes2),
wenzelm@237
   345
      parse_ast_trtab =
wenzelm@15755
   346
        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@237
   347
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
wenzelm@15755
   348
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@5692
   349
      print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@237
   350
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@5692
   351
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
wenzelm@2700
   352
      tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
wenzelm@17079
   353
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
clasohm@0
   354
  end;
clasohm@0
   355
clasohm@0
   356
wenzelm@18720
   357
(* basic syntax *)
wenzelm@260
   358
wenzelm@18720
   359
val basic_syn =
wenzelm@17168
   360
  empty_syntax
wenzelm@17168
   361
  |> extend_syntax default_mode TypeExt.type_ext
wenzelm@17168
   362
  |> extend_syntax default_mode SynExt.pure_ext;
wenzelm@260
   363
wenzelm@18720
   364
val basic_nonterms =
wenzelm@18720
   365
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
wenzelm@18720
   366
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
wenzelm@22573
   367
    "asms", SynExt.any_, SynExt.sprop, "num_const", "index", "struct"]);
wenzelm@18720
   368
clasohm@0
   369
wenzelm@4887
   370
wenzelm@15759
   371
(** print syntax **)
wenzelm@15759
   372
wenzelm@15759
   373
local
clasohm@0
   374
wenzelm@260
   375
fun pretty_strs_qs name strs =
berghofe@14598
   376
  Pretty.strs (name :: map Library.quote (sort_strings strs));
clasohm@0
   377
wenzelm@17079
   378
fun pretty_gram (Syntax (tabs, _)) =
clasohm@0
   379
  let
wenzelm@14904
   380
    val {lexicon, prmodes, gram, prtabs, ...} = tabs;
wenzelm@2913
   381
    val prmodes' = sort_strings (filter_out (equal "") prmodes);
clasohm@0
   382
  in
wenzelm@8720
   383
    [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
wenzelm@8720
   384
      Pretty.big_list "prods:" (Parser.pretty_gram gram),
wenzelm@8720
   385
      pretty_strs_qs "print modes:" prmodes']
clasohm@0
   386
  end;
clasohm@0
   387
wenzelm@17079
   388
fun pretty_trans (Syntax (tabs, _)) =
clasohm@0
   389
  let
wenzelm@260
   390
    fun pretty_trtab name tab =
wenzelm@5692
   391
      pretty_strs_qs name (Symtab.keys tab);
clasohm@0
   392
wenzelm@260
   393
    fun pretty_ruletab name tab =
wenzelm@5692
   394
      Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
clasohm@0
   395
berghofe@14598
   396
    fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs);
wenzelm@4703
   397
wenzelm@237
   398
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@4703
   399
      print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
clasohm@0
   400
  in
wenzelm@8720
   401
    [pretty_strs_qs "consts:" consts,
wenzelm@8720
   402
      pretty_trtab "parse_ast_translation:" parse_ast_trtab,
wenzelm@8720
   403
      pretty_ruletab "parse_rules:" parse_ruletab,
wenzelm@8720
   404
      pretty_trtab "parse_translation:" parse_trtab,
wenzelm@8720
   405
      pretty_trtab "print_translation:" print_trtab,
wenzelm@8720
   406
      pretty_ruletab "print_rules:" print_ruletab,
wenzelm@8720
   407
      pretty_trtab "print_ast_translation:" print_ast_trtab,
wenzelm@8720
   408
      Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
clasohm@0
   409
  end;
clasohm@0
   410
wenzelm@15759
   411
in
clasohm@0
   412
wenzelm@15759
   413
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
wenzelm@15759
   414
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
wenzelm@15759
   415
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
clasohm@0
   416
wenzelm@15759
   417
end;
clasohm@0
   418
clasohm@0
   419
clasohm@0
   420
wenzelm@237
   421
(** read **)
wenzelm@18
   422
wenzelm@260
   423
(* read_ast *)
wenzelm@260
   424
clasohm@882
   425
val ambiguity_level = ref 1;
wenzelm@24263
   426
val ambiguity_is_error = ref false;
clasohm@882
   427
wenzelm@21772
   428
fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root str =
wenzelm@260
   429
  let
wenzelm@14904
   430
    val {lexicon, gram, parse_ast_trtab, ...} = tabs;
wenzelm@14904
   431
    val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
wenzelm@4703
   432
    val chars = Symbol.explode str;
wenzelm@5692
   433
    val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
clasohm@330
   434
wenzelm@12292
   435
    fun show_pt pt =
wenzelm@21772
   436
      Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt])));
wenzelm@260
   437
  in
wenzelm@21962
   438
    if length pts > ! ambiguity_level then
wenzelm@16613
   439
      if ! ambiguity_is_error then error ("Ambiguous input " ^ quote str)
wenzelm@16661
   440
      else (warning ("Ambiguous input " ^ quote str ^ "\n" ^
wenzelm@16716
   441
          "produces " ^ string_of_int (length pts) ^ " parse trees.");
wenzelm@21962
   442
         List.app (warning o show_pt) pts)
wenzelm@21962
   443
    else ();
wenzelm@21772
   444
    SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts
wenzelm@260
   445
  end;
wenzelm@260
   446
wenzelm@260
   447
wenzelm@237
   448
(* read *)
clasohm@0
   449
wenzelm@21772
   450
fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty str =
clasohm@0
   451
  let
wenzelm@237
   452
    val {parse_ruletab, parse_trtab, ...} = tabs;
wenzelm@21772
   453
    val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) str;
clasohm@0
   454
  in
wenzelm@21772
   455
    SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab)
wenzelm@18931
   456
      (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
clasohm@0
   457
  end;
clasohm@0
   458
clasohm@0
   459
wenzelm@22703
   460
(* read terms *)
wenzelm@22703
   461
wenzelm@24372
   462
(*brute-force disambiguation via type-inference*)
wenzelm@24372
   463
fun disambig _ _ [t] = t
wenzelm@24372
   464
  | disambig pp check ts =
wenzelm@24372
   465
      let
wenzelm@24372
   466
        val err_results = map check ts;
wenzelm@24372
   467
        val errs = map_filter (fn Exn.Exn (ERROR msg) => SOME msg | _ => NONE) err_results;
wenzelm@24372
   468
        val results = map_filter Exn.get_result err_results;
wenzelm@24372
   469
wenzelm@24372
   470
        val ambiguity = length ts;
wenzelm@24372
   471
        fun ambig_msg () =
wenzelm@24372
   472
          if ambiguity > 1 andalso ambiguity <= ! ambiguity_level then
wenzelm@24372
   473
            "Got more than one parse tree.\n\
wenzelm@24372
   474
            \Retry with smaller ambiguity_level for more information."
wenzelm@24372
   475
          else "";
wenzelm@24372
   476
      in
wenzelm@24372
   477
        if null results then cat_error (ambig_msg ()) (cat_lines errs)
wenzelm@24372
   478
        else if length results = 1 then
wenzelm@24372
   479
          (if ambiguity > ! ambiguity_level then
wenzelm@24372
   480
            warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@24372
   481
              \You may still want to disambiguate your grammar or your input."
wenzelm@24372
   482
          else (); hd results)
wenzelm@24372
   483
        else cat_error (ambig_msg ()) ("More than one term is type correct:\n" ^
wenzelm@24372
   484
          cat_lines (map (Pretty.string_of_term pp) ts))
wenzelm@24372
   485
      end;
wenzelm@24372
   486
wenzelm@24372
   487
fun standard_parse_term pp check get_sort map_const map_free map_type map_sort
wenzelm@24372
   488
    ctxt is_logtype syn ty str =
wenzelm@24372
   489
  read ctxt is_logtype syn ty str
wenzelm@24372
   490
  |> map (TypeExt.decode_term get_sort map_const map_free map_type map_sort)
wenzelm@24372
   491
  |> disambig (Printer.pp_show_brackets pp) check;
wenzelm@22703
   492
wenzelm@22703
   493
wenzelm@237
   494
(* read types *)
clasohm@0
   495
wenzelm@24278
   496
fun standard_parse_typ ctxt syn get_sort map_sort str =
wenzelm@21772
   497
  (case read ctxt (K false) syn SynExt.typeT str of
wenzelm@22703
   498
    [t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t
wenzelm@8894
   499
  | _ => error "read_typ: ambiguous syntax");
wenzelm@144
   500
wenzelm@144
   501
wenzelm@8894
   502
(* read sorts *)
wenzelm@8894
   503
wenzelm@24278
   504
fun standard_parse_sort ctxt syn map_sort str =
wenzelm@21772
   505
  (case read ctxt (K false) syn TypeExt.sortT str of
wenzelm@22703
   506
    [t] => TypeExt.sort_of_term map_sort t
wenzelm@8894
   507
  | _ => error "read_sort: ambiguous syntax");
wenzelm@8894
   508
wenzelm@8894
   509
wenzelm@18
   510
wenzelm@1158
   511
(** prepare translation rules **)
wenzelm@1158
   512
wenzelm@1158
   513
datatype 'a trrule =
wenzelm@3526
   514
  ParseRule of 'a * 'a |
wenzelm@3526
   515
  PrintRule of 'a * 'a |
wenzelm@3526
   516
  ParsePrintRule of 'a * 'a;
wenzelm@888
   517
wenzelm@4618
   518
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
wenzelm@4618
   519
  | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
wenzelm@4618
   520
  | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
wenzelm@1158
   521
skalberg@15531
   522
fun parse_rule (ParseRule pats) = SOME pats
skalberg@15531
   523
  | parse_rule (PrintRule _) = NONE
skalberg@15531
   524
  | parse_rule (ParsePrintRule pats) = SOME pats;
wenzelm@1158
   525
skalberg@15531
   526
fun print_rule (ParseRule _) = NONE
skalberg@15531
   527
  | print_rule (PrintRule pats) = SOME (swap pats)
skalberg@15531
   528
  | print_rule (ParsePrintRule pats) = SOME (swap pats);
wenzelm@1158
   529
wenzelm@1158
   530
wenzelm@19262
   531
local
wenzelm@19262
   532
wenzelm@1158
   533
fun check_rule (rule as (lhs, rhs)) =
wenzelm@5692
   534
  (case Ast.rule_error rule of
skalberg@15531
   535
    SOME msg =>
wenzelm@1158
   536
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
wenzelm@5692
   537
        Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
skalberg@15531
   538
  | NONE => rule);
wenzelm@888
   539
wenzelm@888
   540
wenzelm@21772
   541
fun read_pattern ctxt is_logtype syn (root, str) =
wenzelm@18
   542
  let
wenzelm@17079
   543
    val Syntax ({consts, ...}, _) = syn;
wenzelm@18
   544
wenzelm@5692
   545
    fun constify (ast as Ast.Constant _) = ast
wenzelm@5692
   546
      | constify (ast as Ast.Variable x) =
wenzelm@20664
   547
          if member (op =) consts x orelse NameSpace.is_qualified x then Ast.Constant x
wenzelm@3830
   548
          else ast
wenzelm@5692
   549
      | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
wenzelm@18
   550
  in
wenzelm@21772
   551
    (case read_asts ctxt is_logtype syn true root str of
wenzelm@888
   552
      [ast] => constify ast
wenzelm@888
   553
    | _ => error ("Syntactically ambiguous input: " ^ quote str))
wenzelm@18678
   554
  end handle ERROR msg =>
wenzelm@18678
   555
    cat_error msg ("The error(s) above occurred in translation pattern " ^
wenzelm@888
   556
      quote str);
clasohm@0
   557
wenzelm@1158
   558
fun prep_rules rd_pat raw_rules =
wenzelm@4618
   559
  let val rules = map (map_trrule rd_pat) raw_rules in
wenzelm@19482
   560
    (map check_rule (map_filter parse_rule rules),
wenzelm@19482
   561
      map check_rule (map_filter print_rule rules))
wenzelm@1158
   562
  end
wenzelm@18
   563
wenzelm@19262
   564
in
wenzelm@19262
   565
wenzelm@19262
   566
val cert_rules = prep_rules I;
wenzelm@19262
   567
wenzelm@21772
   568
fun read_rules ctxt is_logtype syn =
wenzelm@21772
   569
  prep_rules (read_pattern ctxt is_logtype syn);
wenzelm@19262
   570
wenzelm@19262
   571
end;
wenzelm@19262
   572
wenzelm@18
   573
wenzelm@18
   574
wenzelm@3779
   575
(** pretty terms, typs, sorts **)
wenzelm@18
   576
wenzelm@23631
   577
fun pretty_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t =
clasohm@0
   578
  let
wenzelm@2700
   579
    val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
wenzelm@21772
   580
    val ast = t_to_ast ctxt (lookup_tr' print_trtab) t;
clasohm@0
   581
  in
wenzelm@23631
   582
    Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab)
wenzelm@24613
   583
      (lookup_tokentr tokentrtab (print_mode_value ()))
wenzelm@23631
   584
      (Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast))
clasohm@0
   585
  end;
clasohm@0
   586
wenzelm@23631
   587
fun pretty_term extern =
wenzelm@23631
   588
  pretty_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term;
wenzelm@23631
   589
fun pretty_typ ctxt syn =
wenzelm@23631
   590
  pretty_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false;
wenzelm@23631
   591
fun pretty_sort ctxt syn =
wenzelm@23631
   592
  pretty_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false;
clasohm@0
   593
clasohm@0
   594
clasohm@0
   595
wenzelm@19262
   596
(** modify syntax **)
wenzelm@383
   597
wenzelm@14904
   598
fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls);
wenzelm@14904
   599
fun ext_syntax f = ext_syntax' (K f) (K false) default_mode;
wenzelm@383
   600
wenzelm@16613
   601
val extend_type_gram       = ext_syntax Mixfix.syn_ext_types;
wenzelm@16613
   602
val extend_const_gram      = ext_syntax' Mixfix.syn_ext_consts;
wenzelm@16613
   603
val extend_consts          = ext_syntax SynExt.syn_ext_const_names;
wenzelm@16613
   604
val extend_trfuns          = ext_syntax SynExt.syn_ext_trfuns;
wenzelm@16613
   605
val extend_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns;
wenzelm@16613
   606
val extend_tokentrfuns     = ext_syntax SynExt.syn_ext_tokentrfuns;
wenzelm@5692
   607
wenzelm@15755
   608
fun remove_const_gram is_logtype prmode decls =
wenzelm@15755
   609
  remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
wenzelm@15755
   610
wenzelm@21772
   611
fun extend_trrules ctxt is_logtype syn =
wenzelm@21772
   612
  ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
wenzelm@19262
   613
wenzelm@21772
   614
fun remove_trrules ctxt is_logtype syn =
wenzelm@21772
   615
  remove_syntax default_mode o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
wenzelm@19262
   616
wenzelm@19262
   617
val extend_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules;
wenzelm@19262
   618
val remove_trrules_i = remove_syntax default_mode o SynExt.syn_ext_rules o cert_rules;
wenzelm@5692
   619
wenzelm@5692
   620
wenzelm@24263
   621
wenzelm@24768
   622
(** inner syntax operations **)
wenzelm@24263
   623
wenzelm@24768
   624
(* (un)parsing *)
wenzelm@24263
   625
wenzelm@24263
   626
local
wenzelm@24768
   627
  type operations =
wenzelm@24768
   628
   {parse_sort: Proof.context -> string -> sort,
wenzelm@24768
   629
    parse_typ: Proof.context -> string -> typ,
wenzelm@24768
   630
    parse_term: Proof.context -> string -> term,
wenzelm@24768
   631
    parse_prop: Proof.context -> string -> term,
wenzelm@24768
   632
    unparse_sort: Proof.context -> sort -> Pretty.T,
wenzelm@24768
   633
    unparse_typ: Proof.context -> typ -> Pretty.T,
wenzelm@24768
   634
    unparse_term: Proof.context -> term -> Pretty.T};
wenzelm@24768
   635
wenzelm@24768
   636
  val operations = ref (NONE: operations option);
wenzelm@24768
   637
wenzelm@24768
   638
  fun operation which ctxt x =
wenzelm@24768
   639
    (case ! operations of
wenzelm@24768
   640
      NONE => error "Inner syntax operations not yet installed"
wenzelm@24768
   641
    | SOME ops => which ops ctxt x);
wenzelm@24263
   642
in
wenzelm@24263
   643
wenzelm@24768
   644
val parse_sort = operation #parse_sort;
wenzelm@24768
   645
val parse_typ = operation #parse_typ;
wenzelm@24768
   646
val parse_term = operation #parse_term;
wenzelm@24768
   647
val parse_prop = operation #parse_prop;
wenzelm@24768
   648
val unparse_sort = operation #unparse_sort;
wenzelm@24768
   649
val unparse_typ = operation #unparse_typ;
wenzelm@24768
   650
val unparse_term = operation #unparse_term;
wenzelm@24768
   651
wenzelm@24768
   652
fun install_operations ops = CRITICAL (fn () =>
wenzelm@24768
   653
  if is_some (! operations) then error "Inner syntax operations already installed"
wenzelm@24768
   654
  else operations := SOME ops);
wenzelm@24768
   655
wenzelm@24263
   656
end;
wenzelm@24263
   657
wenzelm@24263
   658
wenzelm@24768
   659
(* context-sensitive (un)checking *)
wenzelm@24263
   660
wenzelm@24488
   661
local
wenzelm@24488
   662
wenzelm@24768
   663
type key = int * bool;
wenzelm@24768
   664
type 'a check = 'a list -> Proof.context -> 'a list * Proof.context;
wenzelm@24768
   665
wenzelm@24488
   666
structure Checks = GenericDataFun
wenzelm@24263
   667
(
wenzelm@24488
   668
  type T =
wenzelm@24768
   669
    ((key * ((string * typ check) * stamp) list) list *
wenzelm@24768
   670
     (key * ((string * term check) * stamp) list) list);
wenzelm@24488
   671
  val empty = ([], []);
wenzelm@24263
   672
  val extend = I;
wenzelm@24488
   673
  fun merge _ ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
wenzelm@24768
   674
    (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
wenzelm@24768
   675
     AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
wenzelm@24263
   676
);
wenzelm@24263
   677
wenzelm@24768
   678
fun gen_add which (key: key) name f =
wenzelm@24768
   679
  Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
wenzelm@24768
   680
wenzelm@24768
   681
fun gen_check which uncheck eq ctxt0 xs0 =
wenzelm@24263
   682
  let
wenzelm@24768
   683
    val funs = which (Checks.get (Context.Proof ctxt0))
wenzelm@24768
   684
      |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
wenzelm@24768
   685
      |> Library.sort (int_ord o pairself fst)
wenzelm@24768
   686
      |> not uncheck ? map (apsnd rev);
wenzelm@24263
   687
wenzelm@24768
   688
    fun check_fixpoint fs (xs, ctxt) =
wenzelm@24768
   689
      let val (xs', ctxt') = fold uncurry fs (xs, ctxt) in
wenzelm@24488
   690
        if eq_list eq (xs, xs') then (xs, ctxt)
wenzelm@24768
   691
        else check_fixpoint fs (xs', ctxt')
wenzelm@24263
   692
      end;
wenzelm@24768
   693
  in #1 (fold (check_fixpoint o snd) funs (xs0, ctxt0)) end;
wenzelm@24768
   694
wenzelm@24768
   695
fun map_sort f S =
wenzelm@24768
   696
  (case f (TFree ("", S)) of
wenzelm@24768
   697
    TFree ("", S') => S'
wenzelm@24768
   698
  | T => raise TYPE ("map_sort", [TFree ("", S), T], []));
wenzelm@24488
   699
wenzelm@24488
   700
in
wenzelm@24263
   701
wenzelm@24768
   702
fun print_checks ctxt =
wenzelm@24768
   703
  let
wenzelm@24768
   704
    fun split_checks checks =
wenzelm@24768
   705
      List.partition (fn ((_, un), _) => not un) checks
wenzelm@24768
   706
      |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
wenzelm@24768
   707
          #> sort (int_ord o pairself fst));
wenzelm@24768
   708
    fun pretty_checks kind checks =
wenzelm@24768
   709
      checks |> map (fn (i, names) => Pretty.block
wenzelm@24768
   710
        [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
wenzelm@24768
   711
          Pretty.brk 1, Pretty.strs names]);
wenzelm@24768
   712
wenzelm@24768
   713
    val (typs, terms) = Checks.get (Context.Proof ctxt);
wenzelm@24768
   714
    val (typ_checks, typ_unchecks) = split_checks typs;
wenzelm@24768
   715
    val (term_checks, term_unchecks) = split_checks terms;
wenzelm@24768
   716
  in
wenzelm@24768
   717
    pretty_checks "typ_checks" typ_checks @
wenzelm@24768
   718
    pretty_checks "term_checks" term_checks @
wenzelm@24768
   719
    pretty_checks "typ_unchecks" typ_unchecks @
wenzelm@24768
   720
    pretty_checks "term_unchecks" term_unchecks
wenzelm@24768
   721
  end |> Pretty.chunks |> Pretty.writeln;
wenzelm@24768
   722
wenzelm@24768
   723
fun add_typ_check stage = gen_add apfst (stage, false);
wenzelm@24768
   724
fun add_term_check stage = gen_add apsnd (stage, false);
wenzelm@24768
   725
fun add_typ_uncheck stage = gen_add apfst (stage, true);
wenzelm@24768
   726
fun add_term_uncheck stage = gen_add apsnd (stage, true);
wenzelm@24768
   727
wenzelm@24768
   728
val check_typs = gen_check fst false (op =);
wenzelm@24768
   729
val check_terms = gen_check snd false (op aconv);
wenzelm@24680
   730
fun check_props ctxt = map (TypeInfer.constrain propT) #> check_terms ctxt;
wenzelm@24488
   731
wenzelm@24512
   732
val check_typ = singleton o check_typs;
wenzelm@24512
   733
val check_term = singleton o check_terms;
wenzelm@24512
   734
val check_prop = singleton o check_props;
wenzelm@24768
   735
val check_sort = map_sort o check_typ;
wenzelm@24263
   736
wenzelm@24768
   737
val uncheck_typs = gen_check fst true (op =);
wenzelm@24768
   738
val uncheck_terms = gen_check snd true (op aconv);
wenzelm@24768
   739
val uncheck_sort = map_sort o singleton o uncheck_typs;
wenzelm@24488
   740
wenzelm@24488
   741
end;
wenzelm@24488
   742
wenzelm@24263
   743
wenzelm@24263
   744
(* combined operations *)
wenzelm@24263
   745
wenzelm@24263
   746
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
wenzelm@24263
   747
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
wenzelm@24263
   748
wenzelm@24263
   749
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
wenzelm@24263
   750
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
wenzelm@24263
   751
wenzelm@24263
   752
val read_term = singleton o read_terms;
wenzelm@24263
   753
val read_prop = singleton o read_props;
wenzelm@24263
   754
wenzelm@24709
   755
val read_sort_global = read_sort o ProofContext.init;
wenzelm@24709
   756
val read_typ_global = read_typ o ProofContext.init;
wenzelm@24709
   757
val read_term_global = read_term o ProofContext.init;
wenzelm@24709
   758
val read_prop_global = read_prop o ProofContext.init;
wenzelm@24278
   759
wenzelm@24263
   760
wenzelm@15833
   761
(*export parts of internal Syntax structures*)
wenzelm@15833
   762
open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
wenzelm@2366
   763
clasohm@0
   764
end;
wenzelm@5692
   765
wenzelm@5692
   766
structure BasicSyntax: BASIC_SYNTAX = Syntax;
wenzelm@5692
   767
open BasicSyntax;
wenzelm@23923
   768
wenzelm@23923
   769
structure Hidden = struct end;
wenzelm@23923
   770
structure Ast = Hidden;
wenzelm@23923
   771
structure SynExt = Hidden;
wenzelm@23923
   772
structure Parser = Hidden;
wenzelm@23923
   773
structure TypeExt = Hidden;
wenzelm@23923
   774
structure SynTrans = Hidden;
wenzelm@23923
   775
structure Mixfix = Hidden;
wenzelm@23923
   776
structure Printer = Hidden;