src/Pure/Syntax/type_ext.ML
author wenzelm
Wed, 06 Apr 2011 17:00:40 +0200
changeset 42254 f427c9890c46
parent 42245 29e3967550d5
child 42262 4821a2a91548
permissions -rw-r--r--
more abstract print translation;
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/type_ext.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
     4
Utilities for input and output of types.  The concrete syntax of types.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
signature TYPE_EXT0 =
2584
b386951e15e6 improved comments;
wenzelm
parents: 2438
diff changeset
     8
sig
42242
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42223
diff changeset
     9
  val decode_position_term: term -> Position.T option
42053
006095137a81 update_name_tr: more precise handling of explicit constraints, including positions;
wenzelm
parents: 42052
diff changeset
    10
  val is_position: term -> bool
42050
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    11
  val strip_positions: term -> term
42052
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    12
  val strip_positions_ast: Ast.ast -> Ast.ast
10572
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    13
  val no_brackets: unit -> bool
16614
a493a50e6c0a export no_type_brackets;
wenzelm
parents: 15833
diff changeset
    14
  val no_type_brackets: unit -> bool
2584
b386951e15e6 improved comments;
wenzelm
parents: 2438
diff changeset
    15
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
signature TYPE_EXT =
2584
b386951e15e6 improved comments;
wenzelm
parents: 2438
diff changeset
    18
sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  include TYPE_EXT0
1511
09354d37a5ab Elimination of fully-functorial style.
paulson
parents: 764
diff changeset
    20
  val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36502
diff changeset
    21
  val type_ext: Syn_Ext.syn_ext
2584
b386951e15e6 improved comments;
wenzelm
parents: 2438
diff changeset
    22
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36502
diff changeset
    24
structure Type_Ext: TYPE_EXT =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
struct
2584
b386951e15e6 improved comments;
wenzelm
parents: 2438
diff changeset
    26
b386951e15e6 improved comments;
wenzelm
parents: 2438
diff changeset
    27
(** input utils **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
42050
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    29
(* positions *)
22704
f67607c3e56e added decode_types (from type_infer.ML);
wenzelm
parents: 20854
diff changeset
    30
42242
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42223
diff changeset
    31
fun decode_position_term (Free (x, _)) = Lexicon.decode_position x
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42223
diff changeset
    32
  | decode_position_term _ = NONE;
42131
1d9710ff7209 decode_term/disambig: report resolved term variables for the unique (!) result;
wenzelm
parents: 42053
diff changeset
    33
42242
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42223
diff changeset
    34
val is_position = is_some o decode_position_term;
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 39288
diff changeset
    35
42050
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    36
fun strip_positions ((t as Const (c, _)) $ u $ v) =
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    37
      if (c = "_constrain" orelse c = "_constrainAbs") andalso is_position v
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    38
      then strip_positions u
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    39
      else t $ strip_positions u $ strip_positions v
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    40
  | strip_positions (t $ u) = strip_positions t $ strip_positions u
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    41
  | strip_positions (Abs (x, T, t)) = Abs (x, T, strip_positions t)
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    42
  | strip_positions t = t;
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    43
42052
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    44
fun strip_positions_ast (Ast.Appl ((t as Ast.Constant c) :: u :: (v as Ast.Variable x) :: asts)) =
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    45
      if (c = "_constrain" orelse c = "_constrainAbs") andalso is_some (Lexicon.decode_position x)
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    46
      then Ast.mk_appl (strip_positions_ast u) (map strip_positions_ast asts)
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    47
      else Ast.Appl (map strip_positions_ast (t :: u :: v :: asts))
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    48
  | strip_positions_ast (Ast.Appl asts) = Ast.Appl (map strip_positions_ast asts)
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    49
  | strip_positions_ast ast = ast;
34f1d2d81284 statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents: 42050
diff changeset
    50
42050
5a505dfec04e datatype case_tr: strip positions -- type constraints are unexpected despite the "any" category in "case_syn";
wenzelm
parents: 42048
diff changeset
    51
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
(** the type syntax **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
10572
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    55
(* print mode *)
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    56
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    57
val bracketsN = "brackets";
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    58
val no_bracketsN = "no_brackets";
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    59
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    60
fun no_brackets () =
24613
bc889c3d55a3 added print_mode_value (CRITICAL);
wenzelm
parents: 23167
diff changeset
    61
  find_first (fn mode => mode = bracketsN orelse mode = no_bracketsN)
bc889c3d55a3 added print_mode_value (CRITICAL);
wenzelm
parents: 23167
diff changeset
    62
    (print_mode_value ()) = SOME no_bracketsN;
11312
4104bd8d1528 added (no)_type_brackets
nipkow
parents: 10572
diff changeset
    63
4104bd8d1528 added (no)_type_brackets
nipkow
parents: 10572
diff changeset
    64
val type_bracketsN = "type_brackets";
4104bd8d1528 added (no)_type_brackets
nipkow
parents: 10572
diff changeset
    65
val no_type_bracketsN = "no_type_brackets";
4104bd8d1528 added (no)_type_brackets
nipkow
parents: 10572
diff changeset
    66
4104bd8d1528 added (no)_type_brackets
nipkow
parents: 10572
diff changeset
    67
fun no_type_brackets () =
24613
bc889c3d55a3 added print_mode_value (CRITICAL);
wenzelm
parents: 23167
diff changeset
    68
  find_first (fn mode => mode = type_bracketsN orelse mode = no_type_bracketsN)
bc889c3d55a3 added print_mode_value (CRITICAL);
wenzelm
parents: 23167
diff changeset
    69
    (print_mode_value ()) <> SOME type_bracketsN;
10572
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    70
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    71
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    72
(* parse ast translations *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
    74
fun tapp_ast_tr [ty, c] = Ast.Appl [c, ty]
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
    75
  | tapp_ast_tr asts = raise Ast.AST ("tapp_ast_tr", asts);
347
cd41a57221d0 changed translation of type applications according to new grammar;
wenzelm
parents: 330
diff changeset
    76
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
    77
fun tappl_ast_tr [ty, tys, c] = Ast.mk_appl c (ty :: Ast.unfold_ast "_types" tys)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
    78
  | tappl_ast_tr asts = raise Ast.AST ("tappl_ast_tr", asts);
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    79
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
    80
fun bracket_ast_tr [dom, cod] = Ast.fold_ast_p "\\<^type>fun" (Ast.unfold_ast "_types" dom, cod)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
    81
  | bracket_ast_tr asts = raise Ast.AST ("bracket_ast_tr", asts);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    84
(* print ast translations *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
5690
4b056ee5435c no open;
wenzelm
parents: 3829
diff changeset
    86
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f])
4b056ee5435c no open;
wenzelm
parents: 3829
diff changeset
    87
  | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
347
cd41a57221d0 changed translation of type applications according to new grammar;
wenzelm
parents: 330
diff changeset
    88
  | tappl_ast_tr' (f, ty :: tys) =
5690
4b056ee5435c no open;
wenzelm
parents: 3829
diff changeset
    89
      Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    91
fun fun_ast_tr' (*"\\<^type>fun"*) asts =
16614
a493a50e6c0a export no_type_brackets;
wenzelm
parents: 15833
diff changeset
    92
  if no_brackets () orelse no_type_brackets () then raise Match
10572
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    93
  else
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
    94
    (case Ast.unfold_ast_p "\\<^type>fun" (Ast.Appl (Ast.Constant "\\<^type>fun" :: asts)) of
10572
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    95
      (dom as _ :: _ :: _, cod)
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    96
        => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
b070825579b8 no_brackets mode;
wenzelm
parents: 9067
diff changeset
    97
    | _ => raise Match);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
(* type_ext *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   102
fun typ c = Type (c, []);
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   103
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   104
val class_nameT = typ "class_name";
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   105
val type_nameT = typ "type_name";
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   106
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   107
val sortT = typ "sort";
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   108
val classesT = typ "classes";
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   109
val typesT = typ "types";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36502
diff changeset
   111
local open Lexicon Syn_Ext in
5690
4b056ee5435c no open;
wenzelm
parents: 3829
diff changeset
   112
35668
69e1740fbfb1 simplified Syntax.basic_syntax (again);
wenzelm
parents: 35433
diff changeset
   113
val type_ext = syn_ext' false (K false)
347
cd41a57221d0 changed translation of type applications according to new grammar;
wenzelm
parents: 330
diff changeset
   114
  [Mfix ("_",           tidT --> typeT,                "", [], max_pri),
239
08b6e842ec16 minor internal changes;
wenzelm
parents: 18
diff changeset
   115
   Mfix ("_",           tvarT --> typeT,               "", [], max_pri),
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   116
   Mfix ("_",           type_nameT --> typeT,          "", [], max_pri),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   117
   Mfix ("_",           idT --> type_nameT,            "_type_name", [], max_pri),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   118
   Mfix ("_",           longidT --> type_nameT,        "_type_name", [], max_pri),
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 258
diff changeset
   119
   Mfix ("_::_",        [tidT, sortT] ---> typeT,      "_ofsort", [max_pri, 0], max_pri),
239
08b6e842ec16 minor internal changes;
wenzelm
parents: 18
diff changeset
   120
   Mfix ("_::_",        [tvarT, sortT] ---> typeT,     "_ofsort", [max_pri, 0], max_pri),
14838
b12855d44c97 tuned _dummy_ofsort syntax;
wenzelm
parents: 14255
diff changeset
   121
   Mfix ("'_()::_",     sortT --> typeT,               "_dummy_ofsort", [0], max_pri),
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   122
   Mfix ("_",           class_nameT --> sortT,         "", [], max_pri),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   123
   Mfix ("_",           idT --> class_nameT,           "_class_name", [], max_pri),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   124
   Mfix ("_",           longidT --> class_nameT,       "_class_name", [], max_pri),
2584
b386951e15e6 improved comments;
wenzelm
parents: 2438
diff changeset
   125
   Mfix ("{}",          sortT,                         "_topsort", [], max_pri),
239
08b6e842ec16 minor internal changes;
wenzelm
parents: 18
diff changeset
   126
   Mfix ("{_}",         classesT --> sortT,            "_sort", [], max_pri),
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   127
   Mfix ("_",           class_nameT --> classesT,      "", [], max_pri),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   128
   Mfix ("_,_",         [class_nameT, classesT] ---> classesT, "_classes", [], max_pri),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   129
   Mfix ("_ _",         [typeT, type_nameT] ---> typeT, "_tapp", [max_pri, 0], max_pri),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   130
   Mfix ("((1'(_,/ _')) _)", [typeT, typesT, type_nameT] ---> typeT, "_tappl", [], max_pri),
239
08b6e842ec16 minor internal changes;
wenzelm
parents: 18
diff changeset
   131
   Mfix ("_",           typeT --> typesT,              "", [], max_pri),
08b6e842ec16 minor internal changes;
wenzelm
parents: 18
diff changeset
   132
   Mfix ("_,/ _",       [typeT, typesT] ---> typesT,   "_types", [], max_pri),
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   133
   Mfix ("(_/ => _)",   [typeT, typeT] ---> typeT,     "\\<^type>fun", [1, 0], 0),
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 557
diff changeset
   134
   Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT,    "_bracket", [0, 0], 0),
2678
d5fe793293ac added "_" syntax for dummyT;
wenzelm
parents: 2584
diff changeset
   135
   Mfix ("'(_')",       typeT --> typeT,               "", [0], max_pri),
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35262
diff changeset
   136
   Mfix ("'_",          typeT,                         "\\<^type>dummy", [], max_pri)]
35433
73cc288b4f83 "_type_prop" is syntax const -- recovered printing of OFCLASS;
wenzelm
parents: 35429
diff changeset
   137
  ["_type_prop"]
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   138
  (map Syn_Ext.mk_trfun
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   139
    [("_tapp", K tapp_ast_tr), ("_tappl", K tappl_ast_tr), ("_bracket", K bracket_ast_tr)],
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42170
diff changeset
   140
   [], [], map Syn_Ext.mk_trfun [("\\<^type>fun", K fun_ast_tr')])
26708
fc2e7d2f763d moved default token translations to proof_context.ML, if all fails the pretty printer falls back on plain output;
wenzelm
parents: 26039
diff changeset
   141
  []
239
08b6e842ec16 minor internal changes;
wenzelm
parents: 18
diff changeset
   142
  ([], []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
end;
5690
4b056ee5435c no open;
wenzelm
parents: 3829
diff changeset
   145
4b056ee5435c no open;
wenzelm
parents: 3829
diff changeset
   146
end;