src/Pure/Syntax/type_ext.ML
author wenzelm
Tue Jul 06 21:03:57 1999 +0200 (1999-07-06)
changeset 6901 5e20ddfdf3c7
parent 5690 4b056ee5435c
child 7500 299949eddba8
permissions -rw-r--r--
export term_of_typ;
wenzelm@18
     1
(*  Title:      Pure/Syntax/type_ext.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
clasohm@0
     4
wenzelm@2584
     5
Utilities for input and output of types. Also the concrete syntax of
wenzelm@2584
     6
types, which is used to bootstrap Pure.
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
signature TYPE_EXT0 =
wenzelm@2584
    10
sig
wenzelm@3778
    11
  val raw_term_sorts: term -> (indexname * sort) list
wenzelm@2584
    12
  val typ_of_term: (indexname -> sort) -> term -> typ
wenzelm@6901
    13
  val term_of_typ: bool -> typ -> term
wenzelm@2584
    14
end;
clasohm@0
    15
clasohm@0
    16
signature TYPE_EXT =
wenzelm@2584
    17
sig
clasohm@0
    18
  include TYPE_EXT0
wenzelm@2584
    19
  val term_of_sort: sort -> term
paulson@1511
    20
  val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast
paulson@1511
    21
  val type_ext: SynExt.syn_ext
wenzelm@2584
    22
end;
clasohm@0
    23
paulson@1511
    24
structure TypeExt : TYPE_EXT =
clasohm@0
    25
struct
wenzelm@2584
    26
wenzelm@2584
    27
wenzelm@2584
    28
(** input utils **)
clasohm@0
    29
wenzelm@2584
    30
(* raw_term_sorts *)
wenzelm@2584
    31
wenzelm@3778
    32
fun raw_term_sorts tm =
wenzelm@557
    33
  let
wenzelm@18
    34
    fun classes (Const (c, _)) = [c]
wenzelm@18
    35
      | classes (Free (c, _)) = [c]
wenzelm@3778
    36
      | classes (Const ("_classes", _) $ Const (c, _) $ cs) = c :: classes cs
wenzelm@3778
    37
      | classes (Const ("_classes", _) $ Free (c, _) $ cs) = c :: classes cs
wenzelm@3778
    38
      | classes tm = raise TERM ("raw_term_sorts: bad encoding of classes", [tm]);
clasohm@0
    39
wenzelm@2584
    40
    fun sort (Const ("_topsort", _)) = []
wenzelm@2584
    41
      | sort (Const (c, _)) = [c]
wenzelm@2584
    42
      | sort (Free (c, _)) = [c]
wenzelm@3778
    43
      | sort (Const ("_sort", _) $ cs) = classes cs
wenzelm@3778
    44
      | sort tm = raise TERM ("raw_term_sorts: bad encoding of sort", [tm]);
wenzelm@2584
    45
wenzelm@3778
    46
    fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) env = ((x, ~1), sort cs) ins env
wenzelm@3778
    47
      | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) env = (xi, sort cs) ins env
wenzelm@3778
    48
      | add_env (Abs (_, _, t)) env = add_env t env
wenzelm@3778
    49
      | add_env (t1 $ t2) env = add_env t1 (add_env t2 env)
wenzelm@3778
    50
      | add_env t env = env;
wenzelm@557
    51
  in
wenzelm@3778
    52
    add_env tm []
wenzelm@557
    53
  end;
wenzelm@557
    54
wenzelm@557
    55
wenzelm@2584
    56
(* typ_of_term *)
clasohm@0
    57
wenzelm@2584
    58
fun typ_of_term get_sort t =
wenzelm@557
    59
  let
wenzelm@557
    60
    fun typ_of (Free (x, _)) =
wenzelm@5690
    61
          if Lexicon.is_tid x then TFree (x, get_sort (x, ~1))
wenzelm@557
    62
          else Type (x, [])
wenzelm@557
    63
      | typ_of (Var (xi, _)) = TVar (xi, get_sort xi)
wenzelm@557
    64
      | typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) =
wenzelm@557
    65
          TFree (x, get_sort (x, ~1))
wenzelm@557
    66
      | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) =
wenzelm@557
    67
          TVar (xi, get_sort xi)
wenzelm@557
    68
      | typ_of tm =
clasohm@0
    69
          let
clasohm@0
    70
            val (t, ts) = strip_comb tm;
clasohm@0
    71
            val a =
wenzelm@18
    72
              (case t of
clasohm@0
    73
                Const (x, _) => x
clasohm@0
    74
              | Free (x, _) => x
wenzelm@3778
    75
              | _ => raise TERM ("typ_of_term: bad encoding of type", [tm]));
clasohm@0
    76
          in
wenzelm@557
    77
            Type (a, map typ_of ts)
wenzelm@557
    78
          end;
clasohm@0
    79
  in
wenzelm@557
    80
    typ_of t
clasohm@0
    81
  end;
clasohm@0
    82
clasohm@0
    83
clasohm@0
    84
wenzelm@2584
    85
(** output utils **)
wenzelm@2584
    86
wenzelm@2699
    87
(* term_of_sort *)
wenzelm@2584
    88
wenzelm@2584
    89
fun term_of_sort S =
wenzelm@2584
    90
  let
wenzelm@5690
    91
    fun class c = Lexicon.const "_class" $ Lexicon.free c;
wenzelm@2584
    92
wenzelm@2584
    93
    fun classes [] = sys_error "term_of_sort"
wenzelm@2584
    94
      | classes [c] = class c
wenzelm@5690
    95
      | classes (c :: cs) = Lexicon.const "_classes" $ class c $ classes cs;
wenzelm@2584
    96
  in
wenzelm@2584
    97
    (case S of
wenzelm@5690
    98
      [] => Lexicon.const "_topsort"
wenzelm@2584
    99
    | [c] => class c
wenzelm@5690
   100
    | cs => Lexicon.const "_sort" $ classes cs)
wenzelm@2584
   101
  end;
wenzelm@2584
   102
wenzelm@2584
   103
wenzelm@2584
   104
(* term_of_typ *)
clasohm@0
   105
clasohm@0
   106
fun term_of_typ show_sorts ty =
clasohm@0
   107
  let
wenzelm@2584
   108
    fun of_sort t S =
wenzelm@5690
   109
      if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S
wenzelm@2584
   110
      else t;
clasohm@0
   111
wenzelm@5690
   112
    fun term_of (Type (a, Ts)) = list_comb (Lexicon.const a, map term_of Ts)
wenzelm@5690
   113
      | term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S
wenzelm@5690
   114
      | term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S;
clasohm@0
   115
  in
wenzelm@18
   116
    term_of ty
clasohm@0
   117
  end;
clasohm@0
   118
clasohm@0
   119
clasohm@0
   120
clasohm@0
   121
(** the type syntax **)
clasohm@0
   122
wenzelm@18
   123
(* parse ast translations *)
clasohm@0
   124
wenzelm@5690
   125
fun tapp_ast_tr (*"_tapp"*) [ty, f] = Ast.Appl [f, ty]
wenzelm@5690
   126
  | tapp_ast_tr (*"_tapp"*) asts = raise Ast.AST ("tapp_ast_tr", asts);
wenzelm@347
   127
wenzelm@347
   128
fun tappl_ast_tr (*"_tappl"*) [ty, tys, f] =
wenzelm@5690
   129
      Ast.Appl (f :: ty :: Ast.unfold_ast "_types" tys)
wenzelm@5690
   130
  | tappl_ast_tr (*"_tappl"*) asts = raise Ast.AST ("tappl_ast_tr", asts);
clasohm@0
   131
clasohm@0
   132
fun bracket_ast_tr (*"_bracket"*) [dom, cod] =
wenzelm@5690
   133
      Ast.fold_ast_p "fun" (Ast.unfold_ast "_types" dom, cod)
wenzelm@5690
   134
  | bracket_ast_tr (*"_bracket"*) asts = raise Ast.AST ("bracket_ast_tr", asts);
clasohm@0
   135
clasohm@0
   136
wenzelm@18
   137
(* print ast translations *)
clasohm@0
   138
wenzelm@5690
   139
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f])
wenzelm@5690
   140
  | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
wenzelm@347
   141
  | tappl_ast_tr' (f, ty :: tys) =
wenzelm@5690
   142
      Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
clasohm@0
   143
clasohm@0
   144
fun fun_ast_tr' (*"fun"*) asts =
wenzelm@5690
   145
  (case Ast.unfold_ast_p "fun" (Ast.Appl (Ast.Constant "fun" :: asts)) of
wenzelm@18
   146
    (dom as _ :: _ :: _, cod)
wenzelm@5690
   147
      => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
clasohm@0
   148
  | _ => raise Match);
clasohm@0
   149
clasohm@0
   150
clasohm@0
   151
(* type_ext *)
clasohm@0
   152
clasohm@0
   153
val sortT = Type ("sort", []);
clasohm@0
   154
val classesT = Type ("classes", []);
clasohm@0
   155
val typesT = Type ("types", []);
clasohm@0
   156
wenzelm@5690
   157
local open Lexicon SynExt in
wenzelm@5690
   158
clasohm@764
   159
val type_ext = mk_syn_ext false []
wenzelm@347
   160
  [Mfix ("_",           tidT --> typeT,                "", [], max_pri),
wenzelm@239
   161
   Mfix ("_",           tvarT --> typeT,               "", [], max_pri),
wenzelm@239
   162
   Mfix ("_",           idT --> typeT,                 "", [], max_pri),
wenzelm@3829
   163
   Mfix ("_",           longidT --> typeT,             "", [], max_pri),
clasohm@330
   164
   Mfix ("_::_",        [tidT, sortT] ---> typeT,      "_ofsort", [max_pri, 0], max_pri),
wenzelm@239
   165
   Mfix ("_::_",        [tvarT, sortT] ---> typeT,     "_ofsort", [max_pri, 0], max_pri),
wenzelm@239
   166
   Mfix ("_",           idT --> sortT,                 "", [], max_pri),
wenzelm@3829
   167
   Mfix ("_",           longidT --> sortT,             "", [], max_pri),
wenzelm@2584
   168
   Mfix ("{}",          sortT,                         "_topsort", [], max_pri),
wenzelm@239
   169
   Mfix ("{_}",         classesT --> sortT,            "_sort", [], max_pri),
wenzelm@239
   170
   Mfix ("_",           idT --> classesT,              "", [], max_pri),
wenzelm@3829
   171
   Mfix ("_",           longidT --> classesT,          "", [], max_pri),
wenzelm@239
   172
   Mfix ("_,_",         [idT, classesT] ---> classesT, "_classes", [], max_pri),
wenzelm@3829
   173
   Mfix ("_,_",         [longidT, classesT] ---> classesT, "_classes", [], max_pri),
clasohm@330
   174
   Mfix ("_ _",         [typeT, idT] ---> typeT,       "_tapp", [max_pri, 0], max_pri),
wenzelm@3829
   175
   Mfix ("_ _",         [typeT, longidT] ---> typeT,   "_tapp", [max_pri, 0], max_pri),
clasohm@330
   176
   Mfix ("((1'(_,/ _'))_)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri),
wenzelm@3829
   177
   Mfix ("((1'(_,/ _'))_)", [typeT, typesT, longidT] ---> typeT, "_tappl", [], max_pri),
wenzelm@239
   178
   Mfix ("_",           typeT --> typesT,              "", [], max_pri),
wenzelm@239
   179
   Mfix ("_,/ _",       [typeT, typesT] ---> typesT,   "_types", [], max_pri),
wenzelm@239
   180
   Mfix ("(_/ => _)",   [typeT, typeT] ---> typeT,     "fun", [1, 0], 0),
clasohm@624
   181
   Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT,    "_bracket", [0, 0], 0),
wenzelm@2678
   182
   Mfix ("'(_')",       typeT --> typeT,               "", [0], max_pri),
wenzelm@2678
   183
   Mfix ("'_",          typeT,                         "dummy", [], max_pri)]
wenzelm@258
   184
  []
wenzelm@347
   185
  ([("_tapp", tapp_ast_tr), ("_tappl", tappl_ast_tr), ("_bracket", bracket_ast_tr)],
wenzelm@239
   186
   [],
wenzelm@239
   187
   [],
wenzelm@239
   188
   [("fun", fun_ast_tr')])
wenzelm@2699
   189
  TokenTrans.token_translation
wenzelm@239
   190
  ([], []);
clasohm@0
   191
clasohm@0
   192
end;
wenzelm@5690
   193
wenzelm@5690
   194
wenzelm@5690
   195
end;