src/Pure/Syntax/type_ext.ML
author wenzelm
Mon May 31 21:06:57 2010 +0200 (2010-05-31)
changeset 37216 3165bc303f66
parent 36502 586af36cf3cc
child 39288 f1ae2493d93f
permissions -rw-r--r--
modernized some structure names, keeping a few legacy aliases;
wenzelm@18
     1
(*  Title:      Pure/Syntax/type_ext.ML
clasohm@0
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
clasohm@0
     3
wenzelm@35429
     4
Utilities for input and output of types.  The concrete syntax of types.
clasohm@0
     5
*)
clasohm@0
     6
clasohm@0
     7
signature TYPE_EXT0 =
wenzelm@2584
     8
sig
wenzelm@35429
     9
  val sort_of_term: term -> sort
wenzelm@35429
    10
  val term_sorts: term -> (indexname * sort) list
wenzelm@35429
    11
  val typ_of_term: (indexname -> sort) -> term -> typ
wenzelm@27266
    12
  val type_constraint: typ -> term -> term
wenzelm@22764
    13
  val decode_term: (((string * int) * sort) list -> string * int -> sort) ->
wenzelm@35429
    14
    (string -> bool * string) -> (string -> string option) -> term -> term
wenzelm@6901
    15
  val term_of_typ: bool -> typ -> term
wenzelm@10572
    16
  val no_brackets: unit -> bool
wenzelm@16614
    17
  val no_type_brackets: unit -> bool
wenzelm@35668
    18
  val type_ast_trs:
wenzelm@35668
    19
   {read_class: Proof.context -> string -> string,
wenzelm@35668
    20
    read_type: Proof.context -> string -> string} ->
wenzelm@35668
    21
    (string * (Proof.context -> Ast.ast list -> Ast.ast)) list
wenzelm@2584
    22
end;
clasohm@0
    23
clasohm@0
    24
signature TYPE_EXT =
wenzelm@2584
    25
sig
clasohm@0
    26
  include TYPE_EXT0
wenzelm@2584
    27
  val term_of_sort: sort -> term
paulson@1511
    28
  val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
wenzelm@8895
    29
  val sortT: typ
wenzelm@37216
    30
  val type_ext: Syn_Ext.syn_ext
wenzelm@2584
    31
end;
clasohm@0
    32
wenzelm@37216
    33
structure Type_Ext: TYPE_EXT =
clasohm@0
    34
struct
wenzelm@2584
    35
wenzelm@2584
    36
(** input utils **)
clasohm@0
    37
wenzelm@8895
    38
(* sort_of_term *)
wenzelm@2584
    39
wenzelm@35429
    40
fun sort_of_term tm =
wenzelm@557
    41
  let
wenzelm@35429
    42
    fun err () = raise TERM ("sort_of_term: bad encoding of classes", [tm]);
wenzelm@35429
    43
wenzelm@35429
    44
    fun class s = Lexicon.unmark_class s handle Fail _ => err ();
wenzelm@35429
    45
wenzelm@35429
    46
    fun classes (Const (s, _)) = [class s]
wenzelm@35429
    47
      | classes (Const ("_classes", _) $ Const (s, _) $ cs) = class s :: classes cs
wenzelm@35429
    48
      | classes _ = err ();
clasohm@0
    49
wenzelm@2584
    50
    fun sort (Const ("_topsort", _)) = []
wenzelm@35429
    51
      | sort (Const (s, _)) = [class s]
wenzelm@3778
    52
      | sort (Const ("_sort", _) $ cs) = classes cs
wenzelm@35429
    53
      | sort _ = err ();
wenzelm@35429
    54
  in sort tm end;
wenzelm@8895
    55
wenzelm@2584
    56
wenzelm@22704
    57
(* term_sorts *)
wenzelm@8895
    58
wenzelm@35429
    59
fun term_sorts tm =
wenzelm@8895
    60
  let
wenzelm@35429
    61
    val sort_of = sort_of_term;
wenzelm@22704
    62
wenzelm@22704
    63
    fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) =
wenzelm@22704
    64
          insert (op =) ((x, ~1), sort_of cs)
wenzelm@22704
    65
      | add_env (Const ("_ofsort", _) $ (Const ("_tfree", _) $ Free (x, _)) $ cs) =
wenzelm@22704
    66
          insert (op =) ((x, ~1), sort_of cs)
wenzelm@22704
    67
      | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) =
wenzelm@22704
    68
          insert (op =) (xi, sort_of cs)
wenzelm@22704
    69
      | add_env (Const ("_ofsort", _) $ (Const ("_tvar", _) $ Var (xi, _)) $ cs) =
wenzelm@22704
    70
          insert (op =) (xi, sort_of cs)
wenzelm@22704
    71
      | add_env (Abs (_, _, t)) = add_env t
wenzelm@22704
    72
      | add_env (t1 $ t2) = add_env t1 #> add_env t2
wenzelm@22704
    73
      | add_env _ = I;
wenzelm@8895
    74
  in add_env tm [] end;
wenzelm@557
    75
wenzelm@557
    76
wenzelm@2584
    77
(* typ_of_term *)
clasohm@0
    78
wenzelm@35429
    79
fun typ_of_term get_sort tm =
wenzelm@557
    80
  let
wenzelm@35429
    81
    fun err () = raise TERM ("typ_of_term: bad encoding of type", [tm]);
wenzelm@35429
    82
wenzelm@35429
    83
    fun typ_of (Free (x, _)) = TFree (x, get_sort (x, ~1))
wenzelm@557
    84
      | typ_of (Var (xi, _)) = TVar (xi, get_sort xi)
wenzelm@32784
    85
      | typ_of (Const ("_tfree",_) $ (t as Free _)) = typ_of t
wenzelm@32784
    86
      | typ_of (Const ("_tvar",_) $ (t as Var _)) = typ_of t
wenzelm@12317
    87
      | typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = TFree (x, get_sort (x, ~1))
wenzelm@16614
    88
      | typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) =
wenzelm@16614
    89
          TFree (x, get_sort (x, ~1))
wenzelm@12317
    90
      | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi)
wenzelm@16614
    91
      | typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) =
wenzelm@16614
    92
          TVar (xi, get_sort xi)
wenzelm@35429
    93
      | typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", sort_of_term t)
wenzelm@35429
    94
      | typ_of t =
clasohm@0
    95
          let
wenzelm@35429
    96
            val (head, args) = Term.strip_comb t;
clasohm@0
    97
            val a =
wenzelm@35429
    98
              (case head of
wenzelm@35429
    99
                Const (c, _) => (Lexicon.unmark_type c handle Fail _ => err ())
wenzelm@35429
   100
              | _ => err ());
wenzelm@35429
   101
          in Type (a, map typ_of args) end;
wenzelm@35429
   102
  in typ_of tm end;
clasohm@0
   103
clasohm@0
   104
wenzelm@22764
   105
(* decode_term -- transform parse tree into raw term *)
wenzelm@22704
   106
wenzelm@27266
   107
fun type_constraint T t =
wenzelm@27266
   108
  if T = dummyT then t
wenzelm@27266
   109
  else Const ("_type_constraint_", T --> T) $ t;
wenzelm@27266
   110
wenzelm@35429
   111
fun decode_term get_sort map_const map_free tm =
wenzelm@22704
   112
  let
wenzelm@36448
   113
    val decodeT = typ_of_term (get_sort (term_sorts tm));
wenzelm@22704
   114
wenzelm@22704
   115
    fun decode (Const ("_constrain", _) $ t $ typ) =
wenzelm@27266
   116
          type_constraint (decodeT typ) (decode t)
wenzelm@22704
   117
      | decode (Const ("_constrainAbs", _) $ (Abs (x, T, t)) $ typ) =
wenzelm@22704
   118
          if T = dummyT then Abs (x, decodeT typ, decode t)
wenzelm@35429
   119
          else type_constraint (decodeT typ --> dummyT) (Abs (x, T, decode t))
wenzelm@35429
   120
      | decode (Abs (x, T, t)) = Abs (x, T, decode t)
wenzelm@22704
   121
      | decode (t $ u) = decode t $ decode u
wenzelm@22704
   122
      | decode (Const (a, T)) =
wenzelm@36502
   123
          (case try Lexicon.unmark_fixed a of
wenzelm@36502
   124
            SOME x => Free (x, T)
wenzelm@36502
   125
          | NONE =>
wenzelm@36502
   126
              let val c =
wenzelm@36502
   127
                (case try Lexicon.unmark_const a of
wenzelm@36502
   128
                  SOME c => c
wenzelm@36502
   129
                | NONE => snd (map_const a))
wenzelm@36502
   130
              in Const (c, T) end)
wenzelm@22704
   131
      | decode (Free (a, T)) =
wenzelm@22704
   132
          (case (map_free a, map_const a) of
wenzelm@35429
   133
            (SOME x, _) => Free (x, T)
wenzelm@35429
   134
          | (_, (true, c)) => Const (c, T)
wenzelm@35429
   135
          | (_, (false, c)) => (if Long_Name.is_qualified c then Const else Free) (c, T))
wenzelm@35429
   136
      | decode (Var (xi, T)) = Var (xi, T)
wenzelm@22704
   137
      | decode (t as Bound _) = t;
wenzelm@22704
   138
  in decode tm end;
wenzelm@22704
   139
wenzelm@22704
   140
clasohm@0
   141
wenzelm@2584
   142
(** output utils **)
wenzelm@2584
   143
wenzelm@2699
   144
(* term_of_sort *)
wenzelm@2584
   145
wenzelm@2584
   146
fun term_of_sort S =
wenzelm@2584
   147
  let
wenzelm@35429
   148
    val class = Lexicon.const o Lexicon.mark_class;
wenzelm@2584
   149
wenzelm@35429
   150
    fun classes [c] = class c
wenzelm@5690
   151
      | classes (c :: cs) = Lexicon.const "_classes" $ class c $ classes cs;
wenzelm@2584
   152
  in
wenzelm@2584
   153
    (case S of
wenzelm@5690
   154
      [] => Lexicon.const "_topsort"
wenzelm@2584
   155
    | [c] => class c
wenzelm@5690
   156
    | cs => Lexicon.const "_sort" $ classes cs)
wenzelm@2584
   157
  end;
wenzelm@2584
   158
wenzelm@2584
   159
wenzelm@2584
   160
(* term_of_typ *)
clasohm@0
   161
clasohm@0
   162
fun term_of_typ show_sorts ty =
clasohm@0
   163
  let
wenzelm@2584
   164
    fun of_sort t S =
wenzelm@5690
   165
      if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S
wenzelm@2584
   166
      else t;
clasohm@0
   167
wenzelm@35429
   168
    fun term_of (Type (a, Ts)) =
wenzelm@35429
   169
          Term.list_comb (Lexicon.const (Lexicon.mark_type a), map term_of Ts)
wenzelm@5690
   170
      | term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S
wenzelm@5690
   171
      | term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S;
wenzelm@8895
   172
  in term_of ty end;
clasohm@0
   173
clasohm@0
   174
clasohm@0
   175
clasohm@0
   176
(** the type syntax **)
clasohm@0
   177
wenzelm@10572
   178
(* print mode *)
wenzelm@10572
   179
wenzelm@10572
   180
val bracketsN = "brackets";
wenzelm@10572
   181
val no_bracketsN = "no_brackets";
wenzelm@10572
   182
wenzelm@10572
   183
fun no_brackets () =
wenzelm@24613
   184
  find_first (fn mode => mode = bracketsN orelse mode = no_bracketsN)
wenzelm@24613
   185
    (print_mode_value ()) = SOME no_bracketsN;
nipkow@11312
   186
nipkow@11312
   187
val type_bracketsN = "type_brackets";
nipkow@11312
   188
val no_type_bracketsN = "no_type_brackets";
nipkow@11312
   189
nipkow@11312
   190
fun no_type_brackets () =
wenzelm@24613
   191
  find_first (fn mode => mode = type_bracketsN orelse mode = no_type_bracketsN)
wenzelm@24613
   192
    (print_mode_value ()) <> SOME type_bracketsN;
wenzelm@10572
   193
wenzelm@10572
   194
wenzelm@18
   195
(* parse ast translations *)
clasohm@0
   196
wenzelm@35429
   197
val class_ast = Ast.Constant o Lexicon.mark_class;
wenzelm@35429
   198
val type_ast = Ast.Constant o Lexicon.mark_type;
wenzelm@35429
   199
wenzelm@35429
   200
fun class_name_tr read_class (*"_class_name"*) [Ast.Variable c] = class_ast (read_class c)
wenzelm@35429
   201
  | class_name_tr _ (*"_class_name"*) asts = raise Ast.AST ("class_name_tr", asts);
wenzelm@35429
   202
wenzelm@35429
   203
fun classes_tr read_class (*"_classes"*) [Ast.Variable c, ast] =
wenzelm@35429
   204
      Ast.mk_appl (Ast.Constant "_classes") [class_ast (read_class c), ast]
wenzelm@35429
   205
  | classes_tr _ (*"_classes"*) asts = raise Ast.AST ("classes_tr", asts);
wenzelm@347
   206
wenzelm@35429
   207
fun type_name_tr read_type (*"_type_name"*) [Ast.Variable c] = type_ast (read_type c)
wenzelm@35429
   208
  | type_name_tr _ (*"_type_name"*) asts = raise Ast.AST ("type_name_tr", asts);
wenzelm@35429
   209
wenzelm@35429
   210
fun tapp_ast_tr read_type (*"_tapp"*) [ty, Ast.Variable c] =
wenzelm@35429
   211
      Ast.Appl [type_ast (read_type c), ty]
wenzelm@35429
   212
  | tapp_ast_tr _ (*"_tapp"*) asts = raise Ast.AST ("tapp_ast_tr", asts);
wenzelm@35429
   213
wenzelm@35429
   214
fun tappl_ast_tr read_type (*"_tappl"*) [ty, tys, Ast.Variable c] =
wenzelm@35429
   215
      Ast.Appl (type_ast (read_type c) :: ty :: Ast.unfold_ast "_types" tys)
wenzelm@35429
   216
  | tappl_ast_tr _ (*"_tappl"*) asts = raise Ast.AST ("tappl_ast_tr", asts);
clasohm@0
   217
clasohm@0
   218
fun bracket_ast_tr (*"_bracket"*) [dom, cod] =
wenzelm@35429
   219
      Ast.fold_ast_p "\\<^type>fun" (Ast.unfold_ast "_types" dom, cod)
wenzelm@5690
   220
  | bracket_ast_tr (*"_bracket"*) asts = raise Ast.AST ("bracket_ast_tr", asts);
clasohm@0
   221
clasohm@0
   222
wenzelm@18
   223
(* print ast translations *)
clasohm@0
   224
wenzelm@5690
   225
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f])
wenzelm@5690
   226
  | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
wenzelm@347
   227
  | tappl_ast_tr' (f, ty :: tys) =
wenzelm@5690
   228
      Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
clasohm@0
   229
wenzelm@35429
   230
fun fun_ast_tr' (*"\\<^type>fun"*) asts =
wenzelm@16614
   231
  if no_brackets () orelse no_type_brackets () then raise Match
wenzelm@10572
   232
  else
wenzelm@35429
   233
    (case Ast.unfold_ast_p "\\<^type>fun" (Ast.Appl (Ast.Constant "\\<^type>fun" :: asts)) of
wenzelm@10572
   234
      (dom as _ :: _ :: _, cod)
wenzelm@10572
   235
        => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
wenzelm@10572
   236
    | _ => raise Match);
clasohm@0
   237
clasohm@0
   238
clasohm@0
   239
(* type_ext *)
clasohm@0
   240
clasohm@0
   241
val sortT = Type ("sort", []);
clasohm@0
   242
val classesT = Type ("classes", []);
clasohm@0
   243
val typesT = Type ("types", []);
clasohm@0
   244
wenzelm@37216
   245
local open Lexicon Syn_Ext in
wenzelm@5690
   246
wenzelm@35668
   247
val type_ext = syn_ext' false (K false)
wenzelm@347
   248
  [Mfix ("_",           tidT --> typeT,                "", [], max_pri),
wenzelm@239
   249
   Mfix ("_",           tvarT --> typeT,               "", [], max_pri),
wenzelm@35429
   250
   Mfix ("_",           idT --> typeT,                 "_type_name", [], max_pri),
wenzelm@35429
   251
   Mfix ("_",           longidT --> typeT,             "_type_name", [], max_pri),
clasohm@330
   252
   Mfix ("_::_",        [tidT, sortT] ---> typeT,      "_ofsort", [max_pri, 0], max_pri),
wenzelm@239
   253
   Mfix ("_::_",        [tvarT, sortT] ---> typeT,     "_ofsort", [max_pri, 0], max_pri),
wenzelm@14838
   254
   Mfix ("'_()::_",     sortT --> typeT,               "_dummy_ofsort", [0], max_pri),
wenzelm@35429
   255
   Mfix ("_",           idT --> sortT,                 "_class_name", [], max_pri),
wenzelm@35429
   256
   Mfix ("_",           longidT --> sortT,             "_class_name", [], max_pri),
wenzelm@2584
   257
   Mfix ("{}",          sortT,                         "_topsort", [], max_pri),
wenzelm@239
   258
   Mfix ("{_}",         classesT --> sortT,            "_sort", [], max_pri),
wenzelm@35429
   259
   Mfix ("_",           idT --> classesT,              "_class_name", [], max_pri),
wenzelm@35429
   260
   Mfix ("_",           longidT --> classesT,          "_class_name", [], max_pri),
wenzelm@239
   261
   Mfix ("_,_",         [idT, classesT] ---> classesT, "_classes", [], max_pri),
wenzelm@3829
   262
   Mfix ("_,_",         [longidT, classesT] ---> classesT, "_classes", [], max_pri),
clasohm@330
   263
   Mfix ("_ _",         [typeT, idT] ---> typeT,       "_tapp", [max_pri, 0], max_pri),
wenzelm@3829
   264
   Mfix ("_ _",         [typeT, longidT] ---> typeT,   "_tapp", [max_pri, 0], max_pri),
wenzelm@9067
   265
   Mfix ("((1'(_,/ _')) _)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri),
wenzelm@9067
   266
   Mfix ("((1'(_,/ _')) _)", [typeT, typesT, longidT] ---> typeT, "_tappl", [], max_pri),
wenzelm@239
   267
   Mfix ("_",           typeT --> typesT,              "", [], max_pri),
wenzelm@239
   268
   Mfix ("_,/ _",       [typeT, typesT] ---> typesT,   "_types", [], max_pri),
wenzelm@35429
   269
   Mfix ("(_/ => _)",   [typeT, typeT] ---> typeT,     "\\<^type>fun", [1, 0], 0),
clasohm@624
   270
   Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT,    "_bracket", [0, 0], 0),
wenzelm@2678
   271
   Mfix ("'(_')",       typeT --> typeT,               "", [0], max_pri),
wenzelm@35429
   272
   Mfix ("'_",          typeT,                         "\\<^type>dummy", [], max_pri)]
wenzelm@35433
   273
  ["_type_prop"]
wenzelm@37216
   274
  ([], [], [], map Syn_Ext.mk_trfun [("\\<^type>fun", K fun_ast_tr')])
wenzelm@26708
   275
  []
wenzelm@239
   276
  ([], []);
clasohm@0
   277
wenzelm@35668
   278
fun type_ast_trs {read_class, read_type} =
wenzelm@35668
   279
 [("_class_name", class_name_tr o read_class),
wenzelm@35668
   280
  ("_classes", classes_tr o read_class),
wenzelm@35668
   281
  ("_type_name", type_name_tr o read_type),
wenzelm@35668
   282
  ("_tapp", tapp_ast_tr o read_type),
wenzelm@35668
   283
  ("_tappl", tappl_ast_tr o read_type),
wenzelm@35668
   284
  ("_bracket", K bracket_ast_tr)];
wenzelm@35668
   285
clasohm@0
   286
end;
wenzelm@5690
   287
wenzelm@5690
   288
end;