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