src/Pure/Syntax/syntax.ML
author wenzelm
Wed, 18 Mar 2009 21:55:38 +0100
changeset 30573 49899f26fbd1
parent 30364 577edc39b501
child 32738 15bb09ca0378
permissions -rw-r--r--
de-camelized Symbol_Pos;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/Syntax/syntax.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     3
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
     4
Standard Isabelle syntax, based on arbitrary context-free grammars
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
     5
(specified by mixfix declarations).
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
     8
signature BASIC_SYNTAX =
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
     9
sig
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    10
  include AST0
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    11
  include SYN_TRANS0
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    12
  include MIXFIX0
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    13
  include PRINTER0
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
    14
end;
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    15
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
signature SYNTAX =
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
    17
sig
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    18
  include AST1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  include LEXICON0
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    20
  include SYN_EXT0
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  include TYPE_EXT0
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    22
  include SYN_TRANS1
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    23
  include MIXFIX1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
  include PRINTER0
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    25
  type syntax
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
    26
  val eq_syntax: syntax * syntax -> bool
14687
e089757b952a added is_keyword;
wenzelm
parents: 14648
diff changeset
    27
  val is_keyword: syntax -> string -> bool
20784
eece9aaaf352 Syntax.mode;
wenzelm
parents: 20664
diff changeset
    28
  type mode
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
    29
  val mode_default: mode
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
    30
  val mode_input: mode
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    31
  val merge_syntaxes: syntax -> syntax -> syntax
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    32
  val basic_syn: syntax
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    33
  val basic_nonterms: string list
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    34
  val print_gram: syntax -> unit
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    35
  val print_trans: syntax -> unit
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    36
  val print_syntax: syntax -> unit
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    37
  val guess_infix: syntax -> string -> mixfix option
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30364
diff changeset
    38
  val read_token: string -> Symbol_Pos.T list * Position.T
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    39
  val ambiguity_is_error: bool ref
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    40
  val ambiguity_level: int ref
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    41
  val ambiguity_limit: int ref
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    42
  val standard_parse_term: Pretty.pp -> (term -> string option) ->
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    43
    (((string * int) * sort) list -> string * int -> Term.sort) ->
27195
bbf4cbc69243 map_const: soft version, no failure here;
wenzelm
parents: 26951
diff changeset
    44
    (string -> bool * string) -> (string -> string option) ->
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    45
    (typ -> typ) -> (sort -> sort) -> Proof.context ->
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30364
diff changeset
    46
    (string -> bool) -> syntax -> typ -> Symbol_Pos.T list * Position.T -> term
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    47
  val standard_parse_typ: Proof.context -> syntax ->
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
    48
    ((indexname * sort) list -> indexname -> sort) -> (sort -> sort) ->
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30364
diff changeset
    49
    Symbol_Pos.T list * Position.T -> typ
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
    50
  val standard_parse_sort: Proof.context -> syntax -> (sort -> sort) ->
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30364
diff changeset
    51
    Symbol_Pos.T list * Position.T -> sort
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    52
  datatype 'a trrule =
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    53
    ParseRule of 'a * 'a |
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    54
    PrintRule of 'a * 'a |
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    55
    ParsePrintRule of 'a * 'a
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    56
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    57
  val standard_unparse_term: (string -> xstring) ->
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    58
    Proof.context -> syntax -> bool -> term -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    59
  val standard_unparse_typ: Proof.context -> syntax -> typ -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    60
  val standard_unparse_sort: Proof.context -> syntax -> sort -> Pretty.T
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
    61
  val update_type_gram: (string * int * mixfix) list -> syntax -> syntax
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
    62
  val update_consts: string list -> syntax -> syntax
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
    63
  val update_trfuns:
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
    64
    (string * ((ast list -> ast) * stamp)) list *
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
    65
    (string * ((term list -> term) * stamp)) list *
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
    66
    (string * ((bool -> typ -> term list -> term) * stamp)) list *
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
    67
    (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
    68
  val update_advanced_trfuns:
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
    69
    (string * ((Proof.context -> ast list -> ast) * stamp)) list *
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
    70
    (string * ((Proof.context -> term list -> term) * stamp)) list *
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
    71
    (string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list *
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
    72
    (string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax
26704
51ee753cc2e3 token translations: context dependent, result Pretty.T;
wenzelm
parents: 26684
diff changeset
    73
  val extend_tokentrfuns: (string * string * (Proof.context -> string -> Pretty.T)) list ->
51ee753cc2e3 token translations: context dependent, result Pretty.T;
wenzelm
parents: 26684
diff changeset
    74
    syntax -> syntax
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
    75
  val update_const_gram: (string -> bool) ->
25387
d9ab1e3a8acb added update_const_gram (avoids duplicates);
wenzelm
parents: 25122
diff changeset
    76
    mode -> (string * typ * mixfix) list -> syntax -> syntax
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
    77
  val remove_const_gram: (string -> bool) ->
20784
eece9aaaf352 Syntax.mode;
wenzelm
parents: 20664
diff changeset
    78
    mode -> (string * typ * mixfix) list -> syntax -> syntax
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
    79
  val update_trrules: Proof.context -> (string -> bool) -> syntax ->
14904
7d8dc92fcb7f removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents: 14798
diff changeset
    80
    (string * string) trrule list -> syntax -> syntax
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
    81
  val remove_trrules: Proof.context -> (string -> bool) -> syntax ->
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
    82
    (string * string) trrule list -> syntax -> syntax
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
    83
  val update_trrules_i: ast trrule list -> syntax -> syntax
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
    84
  val remove_trrules_i: ast trrule list -> syntax -> syntax
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30364
diff changeset
    85
  val parse_token: Markup.T -> string -> Symbol_Pos.T list * Position.T
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    86
  val parse_sort: Proof.context -> string -> sort
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    87
  val parse_typ: Proof.context -> string -> typ
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    88
  val parse_term: Proof.context -> string -> term
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    89
  val parse_prop: Proof.context -> string -> term
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    90
  val unparse_sort: Proof.context -> sort -> Pretty.T
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    91
  val unparse_classrel: Proof.context -> class list -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    92
  val unparse_arity: Proof.context -> arity -> Pretty.T
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    93
  val unparse_typ: Proof.context -> typ -> Pretty.T
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    94
  val unparse_term: Proof.context -> term -> Pretty.T
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    95
  val install_operations:
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    96
   {parse_sort: Proof.context -> string -> sort,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    97
    parse_typ: Proof.context -> string -> typ,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    98
    parse_term: Proof.context -> string -> term,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    99
    parse_prop: Proof.context -> string -> term,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   100
    unparse_sort: Proof.context -> sort -> Pretty.T,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   101
    unparse_typ: Proof.context -> typ -> Pretty.T,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   102
    unparse_term: Proof.context -> term -> Pretty.T} -> unit
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   103
  val print_checks: Proof.context -> unit
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   104
  val add_typ_check: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   105
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   106
    Context.generic -> Context.generic
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   107
  val add_term_check: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   108
    (term list -> Proof.context -> (term list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   109
    Context.generic -> Context.generic
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   110
  val add_typ_uncheck: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   111
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   112
    Context.generic -> Context.generic
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   113
  val add_term_uncheck: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   114
    (term list -> Proof.context -> (term list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   115
    Context.generic -> Context.generic
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   116
  val check_sort: Proof.context -> sort -> sort
24512
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
   117
  val check_typ: Proof.context -> typ -> typ
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
   118
  val check_term: Proof.context -> term -> term
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
   119
  val check_prop: Proof.context -> term -> term
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   120
  val check_typs: Proof.context -> typ list -> typ list
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   121
  val check_terms: Proof.context -> term list -> term list
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   122
  val check_props: Proof.context -> term list -> term list
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   123
  val uncheck_sort: Proof.context -> sort -> sort
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   124
  val uncheck_arity: Proof.context -> arity -> arity
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   125
  val uncheck_classrel: Proof.context -> class list -> class list
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   126
  val uncheck_typs: Proof.context -> typ list -> typ list
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   127
  val uncheck_terms: Proof.context -> term list -> term list
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   128
  val read_sort: Proof.context -> string -> sort
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   129
  val read_typ: Proof.context -> string -> typ
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   130
  val read_term: Proof.context -> string -> term
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   131
  val read_prop: Proof.context -> string -> term
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   132
  val read_terms: Proof.context -> string list -> term list
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   133
  val read_props: Proof.context -> string list -> term list
24709
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   134
  val read_sort_global: theory -> string -> sort
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   135
  val read_typ_global: theory -> string -> typ
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   136
  val read_term_global: theory -> string -> term
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   137
  val read_prop_global: theory -> string -> term
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   138
  val pretty_term: Proof.context -> term -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   139
  val pretty_typ: Proof.context -> typ -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   140
  val pretty_sort: Proof.context -> sort -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   141
  val pretty_classrel: Proof.context -> class list -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   142
  val pretty_arity: Proof.context -> arity -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   143
  val string_of_term: Proof.context -> term -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   144
  val string_of_typ: Proof.context -> typ -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   145
  val string_of_sort: Proof.context -> sort -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   146
  val string_of_classrel: Proof.context -> class list -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   147
  val string_of_arity: Proof.context -> arity -> string
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   148
  val is_pretty_global: Proof.context -> bool
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   149
  val set_pretty_global: bool -> Proof.context -> Proof.context
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   150
  val init_pretty_global: theory -> Proof.context
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   151
  val pretty_term_global: theory -> term -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   152
  val pretty_typ_global: theory -> typ -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   153
  val pretty_sort_global: theory -> sort -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   154
  val string_of_term_global: theory -> term -> string
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   155
  val string_of_typ_global: theory -> typ -> string
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   156
  val string_of_sort_global: theory -> sort -> string
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   157
  val pp: Proof.context -> Pretty.pp
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   158
  val pp_global: theory -> Pretty.pp
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
   159
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
12094
wenzelm
parents: 12073
diff changeset
   161
structure Syntax: SYNTAX =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   164
(** tables of translation functions **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   165
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   166
(* parse (ast) translations *)
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   167
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17314
diff changeset
   168
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   169
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   170
fun err_dup_trfun name c =
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   171
  error ("More than one " ^ name ^ " for " ^ quote c);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   172
21536
f119c730f509 extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents: 20784
diff changeset
   173
fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns;
f119c730f509 extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents: 20784
diff changeset
   174
29004
a5a91f387791 removed Table.extend, NameSpace.extend_table
haftmann
parents: 28904
diff changeset
   175
fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab)
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   176
  handle Symtab.DUP c => err_dup_trfun name c;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   177
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   178
fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   179
  handle Symtab.DUP c => err_dup_trfun name c;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   180
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   181
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   182
(* print (ast) translations *)
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   183
18931
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   184
fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c);
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   185
fun update_tr'tab trfuns = fold_rev (Symtab.update_list SynExt.eq_trfun) trfuns;
18931
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   186
fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns;
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   187
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2);
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   188
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   189
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   190
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   191
(** tables of token translation functions **)
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   192
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   193
fun lookup_tokentr tabs modes =
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19375
diff changeset
   194
  let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""]))
17314
04e21a27c0ad introduces some modern-style AList operations
haftmann
parents: 17221
diff changeset
   195
  in fn c => Option.map fst (AList.lookup (op =) trs c) end;
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   196
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   197
fun merge_tokentrtabs tabs1 tabs2 =
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   198
  let
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   199
    fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   200
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   201
    fun name (s, _) = implode (tl (Symbol.explode s));
2706
91a640a91c6e improved err msg;
wenzelm
parents: 2700
diff changeset
   202
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   203
    fun merge mode =
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   204
      let
17213
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   205
        val trs1 = these (AList.lookup (op =) tabs1 mode);
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   206
        val trs2 = these (AList.lookup (op =) tabs2 mode);
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18977
diff changeset
   207
        val trs = distinct eq_tr (trs1 @ trs2);
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   208
      in
18964
67f572e03236 renamed gen_duplicates to duplicates;
wenzelm
parents: 18931
diff changeset
   209
        (case duplicates (eq_fst (op =)) trs of
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   210
          [] => (mode, trs)
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   211
        | dups => error ("More than one token translation function in mode " ^
2706
91a640a91c6e improved err msg;
wenzelm
parents: 2700
diff changeset
   212
            quote mode ^ " for " ^ commas_quote (map name dups)))
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   213
      end;
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18977
diff changeset
   214
  in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end;
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   215
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   216
fun extend_tokentrtab tokentrs tabs =
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   217
  let
17213
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   218
    fun ins_tokentr (m, c, f) =
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   219
      AList.default (op =) (m, [])
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   220
      #> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ())));
15759
wenzelm
parents: 15755
diff changeset
   221
  in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   222
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   223
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   224
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   225
(** tables of translation rules **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   226
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   227
type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   228
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19375
diff changeset
   229
fun dest_ruletab tab = maps snd (Symtab.dest tab);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   230
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   231
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   232
(* empty, update, merge ruletabs *)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   233
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   234
val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r));
18931
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   235
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   236
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
(** datatype syntax **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   242
datatype syntax =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   243
  Syntax of {
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   244
    input: SynExt.xprod list,
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   245
    lexicon: Scan.lexicon,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   246
    gram: Parser.gram,
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   247
    consts: string list,
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   248
    prmodes: string list,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   249
    parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   250
    parse_ruletab: ruletab,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   251
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   252
    print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   253
    print_ruletab: ruletab,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   254
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
26704
51ee753cc2e3 token translations: context dependent, result Pretty.T;
wenzelm
parents: 26684
diff changeset
   255
    tokentrtab: (string * (string * ((Proof.context -> string -> Pretty.T) * stamp)) list) list,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   256
    prtabs: Printer.prtabs} * stamp;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   258
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   259
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   260
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
14687
e089757b952a added is_keyword;
wenzelm
parents: 14648
diff changeset
   261
20784
eece9aaaf352 Syntax.mode;
wenzelm
parents: 20664
diff changeset
   262
type mode = string * bool;
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   263
val mode_default = ("", true);
25122
f37d7dd25c88 moved internalM to PrintMode.internal;
wenzelm
parents: 25060
diff changeset
   264
val mode_input = (PrintMode.input, true);
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   265
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   266
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   267
(* empty_syntax *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   268
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   269
val empty_syntax = Syntax
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   270
  ({input = [],
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   271
    lexicon = Scan.empty_lexicon,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   272
    gram = Parser.empty_gram,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   273
    consts = [],
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   274
    prmodes = [],
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   275
    parse_ast_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   276
    parse_ruletab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   277
    parse_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   278
    print_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   279
    print_ruletab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   280
    print_ast_trtab = Symtab.empty,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   281
    tokentrtab = [],
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   282
    prtabs = Printer.empty_prtabs}, stamp ());
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   283
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   284
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   285
(* update_syntax *)
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   286
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   287
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   288
  let
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   289
    val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
2366
a163d2be1bb5 added chartrans;
wenzelm
parents: 2287
diff changeset
   290
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   291
      print_ast_trtab, tokentrtab, prtabs} = tabs;
14904
7d8dc92fcb7f removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents: 14798
diff changeset
   292
    val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   293
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   294
      print_ast_translation, token_translation} = syn_ext;
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   295
    val input' = if inout then fold (insert (op =)) xprods input else input;
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   296
    val changed = length input <> length input';
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   297
    fun if_inout xs = if inout then xs else [];
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   298
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   299
    Syntax
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   300
    ({input = input',
27768
398c64b2acef adapted Scan.extend_lexicon/merge_lexicons;
wenzelm
parents: 27265
diff changeset
   301
      lexicon =
398c64b2acef adapted Scan.extend_lexicon/merge_lexicons;
wenzelm
parents: 27265
diff changeset
   302
        if changed then fold Scan.extend_lexicon (SynExt.delims_of xprods) lexicon else lexicon,
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   303
      gram = if changed then Parser.extend_gram gram xprods else gram,
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30189
diff changeset
   304
      consts = Library.merge (op =) (consts1, filter_out Long_Name.is_qualified consts2),
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18857
diff changeset
   305
      prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)),
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   306
      parse_ast_trtab =
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   307
        update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   308
      parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   309
      parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   310
      print_trtab = update_tr'tab print_translation print_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   311
      print_ruletab = update_ruletab print_rules print_ruletab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   312
      print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   313
      tokentrtab = extend_tokentrtab token_translation tokentrtab,
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   314
      prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ())
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   315
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   316
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   317
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   318
(* remove_syntax *)
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   319
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   320
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   321
  let
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   322
    val SynExt.SynExt {xprods, consts = _, prmodes = _,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   323
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   324
      print_ast_translation, token_translation = _} = syn_ext;
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   325
    val {input, lexicon, gram, consts, prmodes,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   326
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   327
      print_ast_trtab, tokentrtab, prtabs} = tabs;
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19262
diff changeset
   328
    val input' = if inout then subtract (op =) xprods input else input;
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   329
    val changed = length input <> length input';
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   330
    fun if_inout xs = if inout then xs else [];
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   331
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   332
    Syntax
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   333
    ({input = input',
25387
d9ab1e3a8acb added update_const_gram (avoids duplicates);
wenzelm
parents: 25122
diff changeset
   334
      lexicon = if changed then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
d9ab1e3a8acb added update_const_gram (avoids duplicates);
wenzelm
parents: 25122
diff changeset
   335
      gram = if changed then Parser.make_gram input' else gram,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   336
      consts = consts,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   337
      prmodes = prmodes,
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   338
      parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   339
      parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   340
      parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   341
      print_trtab = remove_tr'tab print_translation print_trtab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   342
      print_ruletab = remove_ruletab print_rules print_ruletab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   343
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   344
      tokentrtab = tokentrtab,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   345
      prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   346
  end;
14904
7d8dc92fcb7f removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents: 14798
diff changeset
   347
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   348
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   349
(* merge_syntaxes *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   350
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   351
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   352
  let
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   353
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   354
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   355
      parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   356
      print_trtab = print_trtab1, print_ruletab = print_ruletab1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   357
      print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   358
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   359
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   360
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   361
      parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   362
      print_trtab = print_trtab2, print_ruletab = print_ruletab2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   363
      print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   364
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   365
    Syntax
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18857
diff changeset
   366
    ({input = Library.merge (op =) (input1, input2),
27768
398c64b2acef adapted Scan.extend_lexicon/merge_lexicons;
wenzelm
parents: 27265
diff changeset
   367
      lexicon = Scan.merge_lexicons (lexicon1, lexicon2),
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   368
      gram = Parser.merge_grams gram1 gram2,
18428
4059413acbc1 sort_distinct;
wenzelm
parents: 17496
diff changeset
   369
      consts = sort_distinct string_ord (consts1 @ consts2),
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18857
diff changeset
   370
      prmodes = Library.merge (op =) (prmodes1, prmodes2),
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   371
      parse_ast_trtab =
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   372
        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   373
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   374
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   375
      print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   376
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   377
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   378
      tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   379
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   380
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   381
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   382
18720
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   383
(* basic syntax *)
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   384
18720
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   385
val basic_syn =
17168
e7951b191048 removed obsolete type_syn;
wenzelm
parents: 17079
diff changeset
   386
  empty_syntax
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   387
  |> update_syntax mode_default TypeExt.type_ext
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   388
  |> update_syntax mode_default SynExt.pure_ext;
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   389
18720
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   390
val basic_nonterms =
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   391
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   392
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
30189
3633f560f4c3 discontinued experimental support for Alice -- too hard to maintain its many language incompatibilities, never really worked anyway;
wenzelm
parents: 29134
diff changeset
   393
    "asms", SynExt.any, SynExt.sprop, "num_const", "float_const",
28904
3ef9489eeef5 New lexical item "float".
nipkow
parents: 28840
diff changeset
   394
    "index", "struct"]);
18720
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   395
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   396
4887
bbc13af86c16 added trfun_names;
wenzelm
parents: 4703
diff changeset
   397
15759
wenzelm
parents: 15755
diff changeset
   398
(** print syntax **)
wenzelm
parents: 15755
diff changeset
   399
wenzelm
parents: 15755
diff changeset
   400
local
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   401
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   402
fun pretty_strs_qs name strs =
28840
wenzelm
parents: 28413
diff changeset
   403
  Pretty.strs (name :: map quote (sort_strings strs));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   404
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   405
fun pretty_gram (Syntax (tabs, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   406
  let
14904
7d8dc92fcb7f removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents: 14798
diff changeset
   407
    val {lexicon, prmodes, gram, prtabs, ...} = tabs;
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 27889
diff changeset
   408
    val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   409
  in
8720
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   410
    [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   411
      Pretty.big_list "prods:" (Parser.pretty_gram gram),
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   412
      pretty_strs_qs "print modes:" prmodes']
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   413
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   414
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   415
fun pretty_trans (Syntax (tabs, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   416
  let
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   417
    fun pretty_trtab name tab =
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   418
      pretty_strs_qs name (Symtab.keys tab);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   419
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   420
    fun pretty_ruletab name tab =
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   421
      Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   422
28840
wenzelm
parents: 28413
diff changeset
   423
    fun pretty_tokentr (mode, trs) = Pretty.strs (quote mode ^ ":" :: map fst trs);
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   424
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   425
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   426
      print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   427
  in
8720
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   428
    [pretty_strs_qs "consts:" consts,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   429
      pretty_trtab "parse_ast_translation:" parse_ast_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   430
      pretty_ruletab "parse_rules:" parse_ruletab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   431
      pretty_trtab "parse_translation:" parse_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   432
      pretty_trtab "print_translation:" print_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   433
      pretty_ruletab "print_rules:" print_ruletab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   434
      pretty_trtab "print_ast_translation:" print_ast_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   435
      Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   436
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   437
15759
wenzelm
parents: 15755
diff changeset
   438
in
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   439
15759
wenzelm
parents: 15755
diff changeset
   440
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
wenzelm
parents: 15755
diff changeset
   441
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
wenzelm
parents: 15755
diff changeset
   442
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   443
15759
wenzelm
parents: 15755
diff changeset
   444
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   445
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   446
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   447
(* reconstructing infixes -- educated guessing *)
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   448
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   449
fun guess_infix (Syntax ({gram, ...}, _)) c =
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   450
  (case Parser.guess_infix_lr gram c of
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   451
    SOME (s, l, r, j) => SOME
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   452
     (if l then Mixfix.InfixlName (s, j)
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   453
      else if r then Mixfix.InfixrName (s, j)
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   454
      else Mixfix.InfixName (s, j))
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   455
  | NONE => NONE);
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   456
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   457
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   458
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   459
(** read **)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   460
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   461
(* read token -- with optional YXML encoding of position *)
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   462
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   463
fun read_token str =
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   464
  let val (text, pos) =
27803
c08f4ea29b83 read_token: more robust handling of empty text;
wenzelm
parents: 27786
diff changeset
   465
    (case YXML.parse str handle Fail msg => error msg of
c08f4ea29b83 read_token: more robust handling of empty text;
wenzelm
parents: 27786
diff changeset
   466
      XML.Elem ("token", props, [XML.Text text]) => (text, Position.of_properties props)
c08f4ea29b83 read_token: more robust handling of empty text;
wenzelm
parents: 27786
diff changeset
   467
    | XML.Elem ("token", props, []) => ("", Position.of_properties props)
c08f4ea29b83 read_token: more robust handling of empty text;
wenzelm
parents: 27786
diff changeset
   468
    | XML.Text text => (text, Position.none)
c08f4ea29b83 read_token: more robust handling of empty text;
wenzelm
parents: 27786
diff changeset
   469
    | _ => (str, Position.none))
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30364
diff changeset
   470
  in (Symbol_Pos.explode (text, pos), pos) end;
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   471
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   472
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   473
(* read_ast *)
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   474
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   475
val ambiguity_is_error = ref false;
882
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
   476
val ambiguity_level = ref 1;
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   477
val ambiguity_limit = ref 10;
882
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
   478
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   479
fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos;
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   480
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   481
fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root (syms, pos) =
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   482
  let
14904
7d8dc92fcb7f removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents: 14798
diff changeset
   483
    val {lexicon, gram, parse_ast_trtab, ...} = tabs;
27889
c9e8a5bda32b read_asts: Lexicon.report_token, filter Lexicon.is_proper;
wenzelm
parents: 27822
diff changeset
   484
    val toks = Lexicon.tokenize lexicon xids syms;
28413
ee73353fb87c report_token/parse_token: back to context-less version;
wenzelm
parents: 28407
diff changeset
   485
    val _ = List.app Lexicon.report_token toks;
27889
c9e8a5bda32b read_asts: Lexicon.report_token, filter Lexicon.is_proper;
wenzelm
parents: 27822
diff changeset
   486
14904
7d8dc92fcb7f removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents: 14798
diff changeset
   487
    val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
27889
c9e8a5bda32b read_asts: Lexicon.report_token, filter Lexicon.is_proper;
wenzelm
parents: 27822
diff changeset
   488
    val pts = Parser.parse gram root' (filter Lexicon.is_proper toks);
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   489
    val len = length pts;
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 260
diff changeset
   490
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   491
    val limit = ! ambiguity_limit;
12292
c4090cc2aa15 Symtab.merge_multi';
wenzelm
parents: 12094
diff changeset
   492
    fun show_pt pt =
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   493
      Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt])));
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   494
  in
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   495
    if len <= ! ambiguity_level then ()
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   496
    else if ! ambiguity_is_error then error (ambiguity_msg pos)
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   497
    else
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   498
      (warning (cat_lines
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   499
        (("Ambiguous input " ^ Position.str_of pos ^
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   500
          "\nproduces " ^ string_of_int len ^ " parse trees" ^
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   501
          (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   502
          map show_pt (Library.take (limit, pts)))));
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   503
    SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   504
  end;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   505
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   506
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   507
(* read *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   508
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   509
fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty inp =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   510
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   511
    val {parse_ruletab, parse_trtab, ...} = tabs;
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   512
    val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) inp;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   513
  in
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   514
    SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab)
18931
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   515
      (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   516
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   517
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   518
22703
d9fbdfe22543 added read_term;
wenzelm
parents: 22573
diff changeset
   519
(* read terms *)
d9fbdfe22543 added read_term;
wenzelm
parents: 22573
diff changeset
   520
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   521
(*brute-force disambiguation via type-inference*)
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   522
fun disambig _ _ [t] = t
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   523
  | disambig pp check ts =
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   524
      let
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   525
        val level = ! ambiguity_level;
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   526
        val limit = ! ambiguity_limit;
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   527
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   528
        val ambiguity = length ts;
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   529
        fun ambig_msg () =
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   530
          if ambiguity > 1 andalso ambiguity <= level then
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   531
            "Got more than one parse tree.\n\
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   532
            \Retry with smaller Syntax.ambiguity_level for more information."
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   533
          else "";
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   534
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   535
        val errs = map check ts;
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   536
        val results = map_filter (fn (t, NONE) => SOME t | _ => NONE) (ts ~~ errs);
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   537
        val len = length results;
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   538
      in
25476
03da46cfab9e standard_parse_term: check ambiguous results without changing the result yet;
wenzelm
parents: 25394
diff changeset
   539
        if null results then cat_error (ambig_msg ()) (cat_lines (map_filter I errs))
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   540
        else if len = 1 then
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   541
          (if ambiguity > level then
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   542
            warning "Fortunately, only one parse tree is type correct.\n\
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   543
              \You may still want to disambiguate your grammar or your input."
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   544
          else (); hd results)
25993
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   545
        else cat_error (ambig_msg ()) (cat_lines
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   546
          (("Ambiguous input, " ^ string_of_int len ^ " terms are type correct" ^
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   547
            (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
d542486e39e7 added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents: 25476
diff changeset
   548
            map (Pretty.string_of_term pp) (Library.take (limit, results))))
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   549
      end;
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   550
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   551
fun standard_parse_term pp check get_sort map_const map_free map_type map_sort
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   552
    ctxt is_logtype syn ty (syms, pos) =
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   553
  read ctxt is_logtype syn ty (syms, pos)
24372
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   554
  |> map (TypeExt.decode_term get_sort map_const map_free map_type map_sort)
743575ccfec8 standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents: 24341
diff changeset
   555
  |> disambig (Printer.pp_show_brackets pp) check;
22703
d9fbdfe22543 added read_term;
wenzelm
parents: 22573
diff changeset
   556
d9fbdfe22543 added read_term;
wenzelm
parents: 22573
diff changeset
   557
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   558
(* read types *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   559
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   560
fun standard_parse_typ ctxt syn get_sort map_sort (syms, pos) =
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   561
  (case read ctxt (K false) syn SynExt.typeT (syms, pos) of
22703
d9fbdfe22543 added read_term;
wenzelm
parents: 22573
diff changeset
   562
    [t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   563
  | _ => error (ambiguity_msg pos));
144
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   564
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   565
8894
0281bde335ca added read_sort;
wenzelm
parents: 8720
diff changeset
   566
(* read sorts *)
0281bde335ca added read_sort;
wenzelm
parents: 8720
diff changeset
   567
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   568
fun standard_parse_sort ctxt syn map_sort (syms, pos) =
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   569
  (case read ctxt (K false) syn TypeExt.sortT (syms, pos) of
22703
d9fbdfe22543 added read_term;
wenzelm
parents: 22573
diff changeset
   570
    [t] => TypeExt.sort_of_term map_sort t
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   571
  | _ => error (ambiguity_msg pos));
8894
0281bde335ca added read_sort;
wenzelm
parents: 8720
diff changeset
   572
0281bde335ca added read_sort;
wenzelm
parents: 8720
diff changeset
   573
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   574
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   575
(** prepare translation rules **)
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   576
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   577
datatype 'a trrule =
3526
df4d0dad2b4e tuned warning;
wenzelm
parents: 2913
diff changeset
   578
  ParseRule of 'a * 'a |
df4d0dad2b4e tuned warning;
wenzelm
parents: 2913
diff changeset
   579
  PrintRule of 'a * 'a |
df4d0dad2b4e tuned warning;
wenzelm
parents: 2913
diff changeset
   580
  ParsePrintRule of 'a * 'a;
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   581
4618
731bed12f762 export map_trrule;
wenzelm
parents: 4496
diff changeset
   582
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
731bed12f762 export map_trrule;
wenzelm
parents: 4496
diff changeset
   583
  | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
731bed12f762 export map_trrule;
wenzelm
parents: 4496
diff changeset
   584
  | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   585
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   586
fun parse_rule (ParseRule pats) = SOME pats
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   587
  | parse_rule (PrintRule _) = NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   588
  | parse_rule (ParsePrintRule pats) = SOME pats;
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   589
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   590
fun print_rule (ParseRule _) = NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   591
  | print_rule (PrintRule pats) = SOME (swap pats)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   592
  | print_rule (ParsePrintRule pats) = SOME (swap pats);
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   593
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   594
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   595
local
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   596
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   597
fun check_rule (rule as (lhs, rhs)) =
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   598
  (case Ast.rule_error rule of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   599
    SOME msg =>
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   600
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   601
        Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   602
  | NONE => rule);
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   603
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   604
fun read_pattern ctxt is_logtype syn (root, str) =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   605
  let
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   606
    val Syntax ({consts, ...}, _) = syn;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   607
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   608
    fun constify (ast as Ast.Constant _) = ast
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   609
      | constify (ast as Ast.Variable x) =
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30189
diff changeset
   610
          if member (op =) consts x orelse Long_Name.is_qualified x then Ast.Constant x
3830
7797327eca1d constify: qualified is const;
wenzelm
parents: 3782
diff changeset
   611
          else ast
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   612
      | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   613
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   614
    val (syms, pos) = read_token str;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   615
  in
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   616
    (case read_asts ctxt is_logtype syn true root (syms, pos) of
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   617
      [ast] => constify ast
27786
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   618
    | _ => error (ambiguity_msg pos))
4525c6f5d753 added read_token -- with optional YXML encoding of position;
wenzelm
parents: 27768
diff changeset
   619
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   620
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   621
fun prep_rules rd_pat raw_rules =
4618
731bed12f762 export map_trrule;
wenzelm
parents: 4496
diff changeset
   622
  let val rules = map (map_trrule rd_pat) raw_rules in
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19375
diff changeset
   623
    (map check_rule (map_filter parse_rule rules),
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19375
diff changeset
   624
      map check_rule (map_filter print_rule rules))
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   625
  end
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   626
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   627
in
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   628
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   629
val cert_rules = prep_rules I;
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   630
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   631
fun read_rules ctxt is_logtype syn =
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   632
  prep_rules (read_pattern ctxt is_logtype syn);
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   633
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   634
end;
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   635
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   636
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   637
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   638
(** unparse terms, typs, sorts **)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   639
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   640
local
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   641
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   642
fun unparse_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   643
  let
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   644
    val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   645
    val ast = t_to_ast ctxt (lookup_tr' print_trtab) t;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   646
  in
23631
2a9e918653cc pretty_sort/typ/term: markup;
wenzelm
parents: 23615
diff changeset
   647
    Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab)
24613
bc889c3d55a3 added print_mode_value (CRITICAL);
wenzelm
parents: 24512
diff changeset
   648
      (lookup_tokentr tokentrtab (print_mode_value ()))
23631
2a9e918653cc pretty_sort/typ/term: markup;
wenzelm
parents: 23615
diff changeset
   649
      (Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   650
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   651
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   652
in
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   653
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   654
fun standard_unparse_term extern =
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   655
  unparse_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   656
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   657
fun standard_unparse_typ ctxt syn =
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   658
  unparse_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   659
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   660
fun standard_unparse_sort ctxt syn =
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   661
  unparse_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   662
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   663
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   664
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   665
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   666
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   667
(** modify syntax **)
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   668
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   669
fun ext_syntax f decls = update_syntax mode_default (f decls);
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   670
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   671
val update_type_gram       = ext_syntax Mixfix.syn_ext_types;
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   672
val update_consts          = ext_syntax SynExt.syn_ext_const_names;
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   673
val update_trfuns          = ext_syntax SynExt.syn_ext_trfuns;
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   674
val update_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns;
16613
76e57e08dcb5 proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents: 15833
diff changeset
   675
val extend_tokentrfuns     = ext_syntax SynExt.syn_ext_tokentrfuns;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   676
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   677
fun update_const_gram is_logtype prmode decls =
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   678
  update_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
25387
d9ab1e3a8acb added update_const_gram (avoids duplicates);
wenzelm
parents: 25122
diff changeset
   679
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   680
fun remove_const_gram is_logtype prmode decls =
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   681
  remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   682
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   683
fun update_trrules ctxt is_logtype syn =
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   684
  ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   685
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   686
fun remove_trrules ctxt is_logtype syn =
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   687
  remove_syntax mode_default o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   688
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   689
val update_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules;
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   690
val remove_trrules_i = remove_syntax mode_default o SynExt.syn_ext_rules o cert_rules;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   691
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   692
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   693
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   694
(** inner syntax operations **)
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   695
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   696
(* (un)parsing *)
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   697
28413
ee73353fb87c report_token/parse_token: back to context-less version;
wenzelm
parents: 28407
diff changeset
   698
fun parse_token markup str =
27822
a6319699e517 added parse_token (from proof_context.ML);
wenzelm
parents: 27808
diff changeset
   699
  let
a6319699e517 added parse_token (from proof_context.ML);
wenzelm
parents: 27808
diff changeset
   700
    val (syms, pos) = read_token str;
28413
ee73353fb87c report_token/parse_token: back to context-less version;
wenzelm
parents: 28407
diff changeset
   701
    val _ = Position.report markup pos;
27822
a6319699e517 added parse_token (from proof_context.ML);
wenzelm
parents: 27808
diff changeset
   702
  in (syms, pos) end;
a6319699e517 added parse_token (from proof_context.ML);
wenzelm
parents: 27808
diff changeset
   703
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   704
local
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   705
  type operations =
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   706
   {parse_sort: Proof.context -> string -> sort,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   707
    parse_typ: Proof.context -> string -> typ,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   708
    parse_term: Proof.context -> string -> term,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   709
    parse_prop: Proof.context -> string -> term,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   710
    unparse_sort: Proof.context -> sort -> Pretty.T,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   711
    unparse_typ: Proof.context -> typ -> Pretty.T,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   712
    unparse_term: Proof.context -> term -> Pretty.T};
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   713
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   714
  val operations = ref (NONE: operations option);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   715
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   716
  fun operation which ctxt x =
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   717
    (case ! operations of
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   718
      NONE => error "Inner syntax operations not yet installed"
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   719
    | SOME ops => which ops ctxt x);
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   720
in
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   721
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   722
val parse_sort = operation #parse_sort;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   723
val parse_typ = operation #parse_typ;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   724
val parse_term = operation #parse_term;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   725
val parse_prop = operation #parse_prop;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   726
val unparse_sort = operation #unparse_sort;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   727
val unparse_typ = operation #unparse_typ;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   728
val unparse_term = operation #unparse_term;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   729
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   730
fun install_operations ops = CRITICAL (fn () =>
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   731
  if is_some (! operations) then error "Inner syntax operations already installed"
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   732
  else operations := SOME ops);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   733
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   734
end;
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   735
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   736
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   737
(* context-sensitive (un)checking *)
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   738
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   739
local
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   740
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   741
type key = int * bool;
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   742
type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option;
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   743
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   744
structure Checks = GenericDataFun
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   745
(
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   746
  type T =
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   747
    ((key * ((string * typ check) * stamp) list) list *
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   748
     (key * ((string * term check) * stamp) list) list);
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   749
  val empty = ([], []);
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   750
  val extend = I;
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   751
  fun merge _ ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   752
    (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   753
     AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   754
);
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   755
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   756
fun gen_add which (key: key) name f =
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   757
  Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   758
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   759
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs));
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   760
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   761
fun gen_check which uncheck ctxt0 xs0 =
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   762
  let
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   763
    val funs = which (Checks.get (Context.Proof ctxt0))
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   764
      |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   765
      |> Library.sort (int_ord o pairself fst) |> map snd
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   766
      |> not uncheck ? map rev;
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   767
    val check_all = perhaps_apply (map check_stage funs);
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   768
  in #1 (perhaps check_all (xs0, ctxt0)) end;
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   769
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   770
fun map_sort f S =
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   771
  (case f (TFree ("", S)) of
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   772
    TFree ("", S') => S'
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   773
  | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   774
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   775
in
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   776
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   777
fun print_checks ctxt =
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   778
  let
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   779
    fun split_checks checks =
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   780
      List.partition (fn ((_, un), _) => not un) checks
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   781
      |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   782
          #> sort (int_ord o pairself fst));
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   783
    fun pretty_checks kind checks =
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   784
      checks |> map (fn (i, names) => Pretty.block
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   785
        [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   786
          Pretty.brk 1, Pretty.strs names]);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   787
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   788
    val (typs, terms) = Checks.get (Context.Proof ctxt);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   789
    val (typ_checks, typ_unchecks) = split_checks typs;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   790
    val (term_checks, term_unchecks) = split_checks terms;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   791
  in
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   792
    pretty_checks "typ_checks" typ_checks @
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   793
    pretty_checks "term_checks" term_checks @
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   794
    pretty_checks "typ_unchecks" typ_unchecks @
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   795
    pretty_checks "term_unchecks" term_unchecks
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   796
  end |> Pretty.chunks |> Pretty.writeln;
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   797
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   798
fun add_typ_check stage = gen_add apfst (stage, false);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   799
fun add_term_check stage = gen_add apsnd (stage, false);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   800
fun add_typ_uncheck stage = gen_add apfst (stage, true);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   801
fun add_term_uncheck stage = gen_add apsnd (stage, true);
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   802
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   803
val check_typs = gen_check fst false;
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   804
val check_terms = gen_check snd false;
27265
49c81f6d7a08 TypeExt.type_constraint;
wenzelm
parents: 27195
diff changeset
   805
fun check_props ctxt = map (TypeExt.type_constraint propT) #> check_terms ctxt;
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   806
24512
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
   807
val check_typ = singleton o check_typs;
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
   808
val check_term = singleton o check_terms;
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
   809
val check_prop = singleton o check_props;
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   810
val check_sort = map_sort o check_typ;
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   811
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   812
val uncheck_typs = gen_check fst true;
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
   813
val uncheck_terms = gen_check snd true;
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
   814
val uncheck_sort = map_sort o singleton o uncheck_typs;
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   815
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   816
end;
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
   817
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   818
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   819
(* derived operations for classrel and arity *)
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   820
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   821
val uncheck_classrel = map o singleton o uncheck_sort;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   822
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   823
fun unparse_classrel ctxt cs = Pretty.block (flat
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   824
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs)));
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   825
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   826
fun uncheck_arity ctxt (a, Ss, S) =
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   827
  let
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   828
    val T = Type (a, replicate (length Ss) dummyT);
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   829
    val a' =
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   830
      (case singleton (uncheck_typs ctxt) T of
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   831
        Type (a', _) => a'
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   832
      | T => raise TYPE ("uncheck_arity", [T], []));
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   833
    val Ss' = map (uncheck_sort ctxt) Ss;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   834
    val S' = uncheck_sort ctxt S;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   835
  in (a', Ss', S') end;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   836
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   837
fun unparse_arity ctxt (a, Ss, S) =
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   838
  let
25043
32ed65dc1bf4 unparse_arity: unparse type constructor as well;
wenzelm
parents: 24970
diff changeset
   839
    val prtT = unparse_typ ctxt (Type (a, []));
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   840
    val dom =
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   841
      if null Ss then []
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   842
      else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
25043
32ed65dc1bf4 unparse_arity: unparse type constructor as well;
wenzelm
parents: 24970
diff changeset
   843
  in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end;
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   844
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   845
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   846
(* read = parse + check *)
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   847
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   848
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   849
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   850
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   851
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   852
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   853
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   854
val read_term = singleton o read_terms;
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   855
val read_prop = singleton o read_props;
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   856
24709
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   857
val read_sort_global = read_sort o ProofContext.init;
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   858
val read_typ_global = read_typ o ProofContext.init;
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   859
val read_term_global = read_term o ProofContext.init;
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
   860
val read_prop_global = read_prop o ProofContext.init;
24278
cf82c471f9ee renamed standard_read_XXX to standard_parse_XXX;
wenzelm
parents: 24263
diff changeset
   861
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
   862
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   863
(* pretty = uncheck + unparse *)
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   864
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   865
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   866
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   867
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   868
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   869
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   870
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   871
val string_of_term = Pretty.string_of oo pretty_term;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   872
val string_of_typ = Pretty.string_of oo pretty_typ;
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   873
val string_of_sort = Pretty.string_of oo pretty_sort;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   874
val string_of_classrel = Pretty.string_of oo pretty_classrel;
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   875
val string_of_arity = Pretty.string_of oo pretty_arity;
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   876
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   877
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   878
(* global pretty printing *)
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   879
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   880
structure PrettyGlobal = ProofDataFun(type T = bool fun init _ = false);
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   881
val is_pretty_global = PrettyGlobal.get;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   882
val set_pretty_global = PrettyGlobal.put;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   883
val init_pretty_global = set_pretty_global true o ProofContext.init;
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   884
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   885
val pretty_term_global = pretty_term o init_pretty_global;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   886
val pretty_typ_global = pretty_typ o init_pretty_global;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   887
val pretty_sort_global = pretty_sort o init_pretty_global;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   888
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   889
val string_of_term_global = string_of_term o init_pretty_global;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   890
val string_of_typ_global = string_of_typ o init_pretty_global;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   891
val string_of_sort_global = string_of_sort o init_pretty_global;
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   892
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   893
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   894
(* pp operations -- deferred evaluation *)
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   895
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   896
fun pp ctxt = Pretty.pp
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   897
 (fn x => pretty_term ctxt x,
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   898
  fn x => pretty_typ ctxt x,
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   899
  fn x => pretty_sort ctxt x,
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   900
  fn x => pretty_classrel ctxt x,
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   901
  fn x => pretty_arity ctxt x);
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
   902
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   903
fun pp_global thy = Pretty.pp
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   904
 (fn x => pretty_term_global thy x,
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   905
  fn x => pretty_typ_global thy x,
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   906
  fn x => pretty_sort_global thy x,
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   907
  fn x => pretty_classrel (init_pretty_global thy) x,
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   908
  fn x => pretty_arity (init_pretty_global thy) x);
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   909
26678
a3ae088dc20f educated guess for infix syntax
haftmann
parents: 25993
diff changeset
   910
15833
78109c7012ed removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents: 15759
diff changeset
   911
(*export parts of internal Syntax structures*)
78109c7012ed removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents: 15759
diff changeset
   912
open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
2366
a163d2be1bb5 added chartrans;
wenzelm
parents: 2287
diff changeset
   913
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   914
end;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   915
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   916
structure BasicSyntax: BASIC_SYNTAX = Syntax;
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   917
open BasicSyntax;
23923
8c10f3515633 hide internal structures (again);
wenzelm
parents: 23660
diff changeset
   918
26684
wenzelm
parents: 26678
diff changeset
   919
forget_structure "Ast";
wenzelm
parents: 26678
diff changeset
   920
forget_structure "SynExt";
wenzelm
parents: 26678
diff changeset
   921
forget_structure "Parser";
wenzelm
parents: 26678
diff changeset
   922
forget_structure "TypeExt";
wenzelm
parents: 26678
diff changeset
   923
forget_structure "SynTrans";
wenzelm
parents: 26678
diff changeset
   924
forget_structure "Mixfix";
wenzelm
parents: 26678
diff changeset
   925
forget_structure "Printer";