src/Pure/Syntax/type_ext.ML
author wenzelm
Wed Apr 28 10:51:34 2010 +0200 (2010-04-28)
changeset 36448 edb757388592
parent 35668 69e1740fbfb1
child 36502 586af36cf3cc
permissions -rw-r--r--
get_sort: minimize sorts given in the text, while keeping those from the context unchanged (the latter are preferred);
tuned;
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@35668
    30
  val type_ext: SynExt.syn_ext
wenzelm@2584
    31
end;
clasohm@0
    32
wenzelm@15750
    33
structure TypeExt: 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@22704
   123
          let val c =
wenzelm@35262
   124
            (case try Lexicon.unmark_const a of
wenzelm@35262
   125
              SOME c => c
wenzelm@35262
   126
            | NONE => snd (map_const a))
wenzelm@35429
   127
          in Const (c, T) end
wenzelm@22704
   128
      | decode (Free (a, T)) =
wenzelm@22704
   129
          (case (map_free a, map_const a) of
wenzelm@35429
   130
            (SOME x, _) => Free (x, T)
wenzelm@35429
   131
          | (_, (true, c)) => Const (c, T)
wenzelm@35429
   132
          | (_, (false, c)) => (if Long_Name.is_qualified c then Const else Free) (c, T))
wenzelm@35429
   133
      | decode (Var (xi, T)) = Var (xi, T)
wenzelm@22704
   134
      | decode (t as Bound _) = t;
wenzelm@22704
   135
  in decode tm end;
wenzelm@22704
   136
wenzelm@22704
   137
clasohm@0
   138
wenzelm@2584
   139
(** output utils **)
wenzelm@2584
   140
wenzelm@2699
   141
(* term_of_sort *)
wenzelm@2584
   142
wenzelm@2584
   143
fun term_of_sort S =
wenzelm@2584
   144
  let
wenzelm@35429
   145
    val class = Lexicon.const o Lexicon.mark_class;
wenzelm@2584
   146
wenzelm@35429
   147
    fun classes [c] = class c
wenzelm@5690
   148
      | classes (c :: cs) = Lexicon.const "_classes" $ class c $ classes cs;
wenzelm@2584
   149
  in
wenzelm@2584
   150
    (case S of
wenzelm@5690
   151
      [] => Lexicon.const "_topsort"
wenzelm@2584
   152
    | [c] => class c
wenzelm@5690
   153
    | cs => Lexicon.const "_sort" $ classes cs)
wenzelm@2584
   154
  end;
wenzelm@2584
   155
wenzelm@2584
   156
wenzelm@2584
   157
(* term_of_typ *)
clasohm@0
   158
clasohm@0
   159
fun term_of_typ show_sorts ty =
clasohm@0
   160
  let
wenzelm@2584
   161
    fun of_sort t S =
wenzelm@5690
   162
      if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S
wenzelm@2584
   163
      else t;
clasohm@0
   164
wenzelm@35429
   165
    fun term_of (Type (a, Ts)) =
wenzelm@35429
   166
          Term.list_comb (Lexicon.const (Lexicon.mark_type a), map term_of Ts)
wenzelm@5690
   167
      | term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S
wenzelm@5690
   168
      | term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S;
wenzelm@8895
   169
  in term_of ty end;
clasohm@0
   170
clasohm@0
   171
clasohm@0
   172
clasohm@0
   173
(** the type syntax **)
clasohm@0
   174
wenzelm@10572
   175
(* print mode *)
wenzelm@10572
   176
wenzelm@10572
   177
val bracketsN = "brackets";
wenzelm@10572
   178
val no_bracketsN = "no_brackets";
wenzelm@10572
   179
wenzelm@10572
   180
fun no_brackets () =
wenzelm@24613
   181
  find_first (fn mode => mode = bracketsN orelse mode = no_bracketsN)
wenzelm@24613
   182
    (print_mode_value ()) = SOME no_bracketsN;
nipkow@11312
   183
nipkow@11312
   184
val type_bracketsN = "type_brackets";
nipkow@11312
   185
val no_type_bracketsN = "no_type_brackets";
nipkow@11312
   186
nipkow@11312
   187
fun no_type_brackets () =
wenzelm@24613
   188
  find_first (fn mode => mode = type_bracketsN orelse mode = no_type_bracketsN)
wenzelm@24613
   189
    (print_mode_value ()) <> SOME type_bracketsN;
wenzelm@10572
   190
wenzelm@10572
   191
wenzelm@18
   192
(* parse ast translations *)
clasohm@0
   193
wenzelm@35429
   194
val class_ast = Ast.Constant o Lexicon.mark_class;
wenzelm@35429
   195
val type_ast = Ast.Constant o Lexicon.mark_type;
wenzelm@35429
   196
wenzelm@35429
   197
fun class_name_tr read_class (*"_class_name"*) [Ast.Variable c] = class_ast (read_class c)
wenzelm@35429
   198
  | class_name_tr _ (*"_class_name"*) asts = raise Ast.AST ("class_name_tr", asts);
wenzelm@35429
   199
wenzelm@35429
   200
fun classes_tr read_class (*"_classes"*) [Ast.Variable c, ast] =
wenzelm@35429
   201
      Ast.mk_appl (Ast.Constant "_classes") [class_ast (read_class c), ast]
wenzelm@35429
   202
  | classes_tr _ (*"_classes"*) asts = raise Ast.AST ("classes_tr", asts);
wenzelm@347
   203
wenzelm@35429
   204
fun type_name_tr read_type (*"_type_name"*) [Ast.Variable c] = type_ast (read_type c)
wenzelm@35429
   205
  | type_name_tr _ (*"_type_name"*) asts = raise Ast.AST ("type_name_tr", asts);
wenzelm@35429
   206
wenzelm@35429
   207
fun tapp_ast_tr read_type (*"_tapp"*) [ty, Ast.Variable c] =
wenzelm@35429
   208
      Ast.Appl [type_ast (read_type c), ty]
wenzelm@35429
   209
  | tapp_ast_tr _ (*"_tapp"*) asts = raise Ast.AST ("tapp_ast_tr", asts);
wenzelm@35429
   210
wenzelm@35429
   211
fun tappl_ast_tr read_type (*"_tappl"*) [ty, tys, Ast.Variable c] =
wenzelm@35429
   212
      Ast.Appl (type_ast (read_type c) :: ty :: Ast.unfold_ast "_types" tys)
wenzelm@35429
   213
  | tappl_ast_tr _ (*"_tappl"*) asts = raise Ast.AST ("tappl_ast_tr", asts);
clasohm@0
   214
clasohm@0
   215
fun bracket_ast_tr (*"_bracket"*) [dom, cod] =
wenzelm@35429
   216
      Ast.fold_ast_p "\\<^type>fun" (Ast.unfold_ast "_types" dom, cod)
wenzelm@5690
   217
  | bracket_ast_tr (*"_bracket"*) asts = raise Ast.AST ("bracket_ast_tr", asts);
clasohm@0
   218
clasohm@0
   219
wenzelm@18
   220
(* print ast translations *)
clasohm@0
   221
wenzelm@5690
   222
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f])
wenzelm@5690
   223
  | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
wenzelm@347
   224
  | tappl_ast_tr' (f, ty :: tys) =
wenzelm@5690
   225
      Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
clasohm@0
   226
wenzelm@35429
   227
fun fun_ast_tr' (*"\\<^type>fun"*) asts =
wenzelm@16614
   228
  if no_brackets () orelse no_type_brackets () then raise Match
wenzelm@10572
   229
  else
wenzelm@35429
   230
    (case Ast.unfold_ast_p "\\<^type>fun" (Ast.Appl (Ast.Constant "\\<^type>fun" :: asts)) of
wenzelm@10572
   231
      (dom as _ :: _ :: _, cod)
wenzelm@10572
   232
        => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
wenzelm@10572
   233
    | _ => raise Match);
clasohm@0
   234
clasohm@0
   235
clasohm@0
   236
(* type_ext *)
clasohm@0
   237
clasohm@0
   238
val sortT = Type ("sort", []);
clasohm@0
   239
val classesT = Type ("classes", []);
clasohm@0
   240
val typesT = Type ("types", []);
clasohm@0
   241
wenzelm@5690
   242
local open Lexicon SynExt in
wenzelm@5690
   243
wenzelm@35668
   244
val type_ext = syn_ext' false (K false)
wenzelm@347
   245
  [Mfix ("_",           tidT --> typeT,                "", [], max_pri),
wenzelm@239
   246
   Mfix ("_",           tvarT --> typeT,               "", [], max_pri),
wenzelm@35429
   247
   Mfix ("_",           idT --> typeT,                 "_type_name", [], max_pri),
wenzelm@35429
   248
   Mfix ("_",           longidT --> typeT,             "_type_name", [], max_pri),
clasohm@330
   249
   Mfix ("_::_",        [tidT, sortT] ---> typeT,      "_ofsort", [max_pri, 0], max_pri),
wenzelm@239
   250
   Mfix ("_::_",        [tvarT, sortT] ---> typeT,     "_ofsort", [max_pri, 0], max_pri),
wenzelm@14838
   251
   Mfix ("'_()::_",     sortT --> typeT,               "_dummy_ofsort", [0], max_pri),
wenzelm@35429
   252
   Mfix ("_",           idT --> sortT,                 "_class_name", [], max_pri),
wenzelm@35429
   253
   Mfix ("_",           longidT --> sortT,             "_class_name", [], max_pri),
wenzelm@2584
   254
   Mfix ("{}",          sortT,                         "_topsort", [], max_pri),
wenzelm@239
   255
   Mfix ("{_}",         classesT --> sortT,            "_sort", [], max_pri),
wenzelm@35429
   256
   Mfix ("_",           idT --> classesT,              "_class_name", [], max_pri),
wenzelm@35429
   257
   Mfix ("_",           longidT --> classesT,          "_class_name", [], max_pri),
wenzelm@239
   258
   Mfix ("_,_",         [idT, classesT] ---> classesT, "_classes", [], max_pri),
wenzelm@3829
   259
   Mfix ("_,_",         [longidT, classesT] ---> classesT, "_classes", [], max_pri),
clasohm@330
   260
   Mfix ("_ _",         [typeT, idT] ---> typeT,       "_tapp", [max_pri, 0], max_pri),
wenzelm@3829
   261
   Mfix ("_ _",         [typeT, longidT] ---> typeT,   "_tapp", [max_pri, 0], max_pri),
wenzelm@9067
   262
   Mfix ("((1'(_,/ _')) _)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri),
wenzelm@9067
   263
   Mfix ("((1'(_,/ _')) _)", [typeT, typesT, longidT] ---> typeT, "_tappl", [], max_pri),
wenzelm@239
   264
   Mfix ("_",           typeT --> typesT,              "", [], max_pri),
wenzelm@239
   265
   Mfix ("_,/ _",       [typeT, typesT] ---> typesT,   "_types", [], max_pri),
wenzelm@35429
   266
   Mfix ("(_/ => _)",   [typeT, typeT] ---> typeT,     "\\<^type>fun", [1, 0], 0),
clasohm@624
   267
   Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT,    "_bracket", [0, 0], 0),
wenzelm@2678
   268
   Mfix ("'(_')",       typeT --> typeT,               "", [0], max_pri),
wenzelm@35429
   269
   Mfix ("'_",          typeT,                         "\\<^type>dummy", [], max_pri)]
wenzelm@35433
   270
  ["_type_prop"]
wenzelm@35668
   271
  ([], [], [], map SynExt.mk_trfun [("\\<^type>fun", K fun_ast_tr')])
wenzelm@26708
   272
  []
wenzelm@239
   273
  ([], []);
clasohm@0
   274
wenzelm@35668
   275
fun type_ast_trs {read_class, read_type} =
wenzelm@35668
   276
 [("_class_name", class_name_tr o read_class),
wenzelm@35668
   277
  ("_classes", classes_tr o read_class),
wenzelm@35668
   278
  ("_type_name", type_name_tr o read_type),
wenzelm@35668
   279
  ("_tapp", tapp_ast_tr o read_type),
wenzelm@35668
   280
  ("_tappl", tappl_ast_tr o read_type),
wenzelm@35668
   281
  ("_bracket", K bracket_ast_tr)];
wenzelm@35668
   282
clasohm@0
   283
end;
wenzelm@5690
   284
wenzelm@5690
   285
end;