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