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