src/Pure/Syntax/type_ext.ML
author wenzelm
Mon Oct 06 18:25:04 1997 +0200 (1997-10-06)
changeset 3778 b70c41bc7491
parent 2699 932fae4271d7
child 3829 d7333ef9e72c
permissions -rw-r--r--
fixed raw_term_sorts (again!);
eliminated raise_ast;
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@2584
    13
end;
clasohm@0
    14
clasohm@0
    15
signature TYPE_EXT =
wenzelm@2584
    16
sig
clasohm@0
    17
  include TYPE_EXT0
wenzelm@2584
    18
  val term_of_sort: sort -> term
paulson@1511
    19
  val term_of_typ: bool -> typ -> 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
paulson@1511
    27
open Lexicon SynExt Ast;
clasohm@0
    28
wenzelm@2584
    29
wenzelm@2584
    30
(** input utils **)
clasohm@0
    31
wenzelm@2584
    32
(* raw_term_sorts *)
wenzelm@2584
    33
wenzelm@3778
    34
fun raw_term_sorts tm =
wenzelm@557
    35
  let
wenzelm@18
    36
    fun classes (Const (c, _)) = [c]
wenzelm@18
    37
      | classes (Free (c, _)) = [c]
wenzelm@3778
    38
      | classes (Const ("_classes", _) $ Const (c, _) $ cs) = c :: classes cs
wenzelm@3778
    39
      | classes (Const ("_classes", _) $ Free (c, _) $ cs) = c :: classes cs
wenzelm@3778
    40
      | classes tm = raise TERM ("raw_term_sorts: bad encoding of classes", [tm]);
clasohm@0
    41
wenzelm@2584
    42
    fun sort (Const ("_topsort", _)) = []
wenzelm@2584
    43
      | sort (Const (c, _)) = [c]
wenzelm@2584
    44
      | sort (Free (c, _)) = [c]
wenzelm@3778
    45
      | sort (Const ("_sort", _) $ cs) = classes cs
wenzelm@3778
    46
      | sort tm = raise TERM ("raw_term_sorts: bad encoding of sort", [tm]);
wenzelm@2584
    47
wenzelm@3778
    48
    fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) env = ((x, ~1), sort cs) ins env
wenzelm@3778
    49
      | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) env = (xi, sort cs) ins env
wenzelm@3778
    50
      | add_env (Abs (_, _, t)) env = add_env t env
wenzelm@3778
    51
      | add_env (t1 $ t2) env = add_env t1 (add_env t2 env)
wenzelm@3778
    52
      | add_env t env = env;
wenzelm@557
    53
  in
wenzelm@3778
    54
    add_env tm []
wenzelm@557
    55
  end;
wenzelm@557
    56
wenzelm@557
    57
wenzelm@2584
    58
(* typ_of_term *)
clasohm@0
    59
wenzelm@2584
    60
fun typ_of_term get_sort t =
wenzelm@557
    61
  let
wenzelm@557
    62
    fun typ_of (Free (x, _)) =
wenzelm@557
    63
          if is_tid x then TFree (x, get_sort (x, ~1))
wenzelm@557
    64
          else Type (x, [])
wenzelm@557
    65
      | typ_of (Var (xi, _)) = TVar (xi, get_sort xi)
wenzelm@557
    66
      | typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) =
wenzelm@557
    67
          TFree (x, get_sort (x, ~1))
wenzelm@557
    68
      | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) =
wenzelm@557
    69
          TVar (xi, get_sort xi)
wenzelm@557
    70
      | typ_of tm =
clasohm@0
    71
          let
clasohm@0
    72
            val (t, ts) = strip_comb tm;
clasohm@0
    73
            val a =
wenzelm@18
    74
              (case t of
clasohm@0
    75
                Const (x, _) => x
clasohm@0
    76
              | Free (x, _) => x
wenzelm@3778
    77
              | _ => raise TERM ("typ_of_term: bad encoding of type", [tm]));
clasohm@0
    78
          in
wenzelm@557
    79
            Type (a, map typ_of ts)
wenzelm@557
    80
          end;
clasohm@0
    81
  in
wenzelm@557
    82
    typ_of t
clasohm@0
    83
  end;
clasohm@0
    84
clasohm@0
    85
clasohm@0
    86
wenzelm@2584
    87
(** output utils **)
wenzelm@2584
    88
wenzelm@2699
    89
(* term_of_sort *)
wenzelm@2584
    90
wenzelm@2584
    91
fun term_of_sort S =
wenzelm@2584
    92
  let
wenzelm@2699
    93
    fun class c = const "_class" $ free c;
wenzelm@2584
    94
wenzelm@2584
    95
    fun classes [] = sys_error "term_of_sort"
wenzelm@2584
    96
      | classes [c] = class c
wenzelm@2584
    97
      | classes (c :: cs) = const "_classes" $ class c $ classes cs;
wenzelm@2584
    98
  in
wenzelm@2584
    99
    (case S of
wenzelm@2584
   100
      [] => const "_topsort"
wenzelm@2584
   101
    | [c] => class c
wenzelm@2584
   102
    | cs => const "_sort" $ classes cs)
wenzelm@2584
   103
  end;
wenzelm@2584
   104
wenzelm@2584
   105
wenzelm@2584
   106
(* term_of_typ *)
clasohm@0
   107
clasohm@0
   108
fun term_of_typ show_sorts ty =
clasohm@0
   109
  let
wenzelm@2584
   110
    fun of_sort t S =
wenzelm@2584
   111
      if show_sorts then const "_ofsort" $ t $ term_of_sort S
wenzelm@2584
   112
      else t;
clasohm@0
   113
wenzelm@2699
   114
    fun term_of (Type (a, Ts)) = list_comb (const a, map term_of Ts)
wenzelm@2699
   115
      | term_of (TFree (x, S)) = of_sort (const "_tfree" $ free x) S
wenzelm@2699
   116
      | term_of (TVar (xi, S)) = of_sort (const "_tvar" $ var xi) S;
clasohm@0
   117
  in
wenzelm@18
   118
    term_of ty
clasohm@0
   119
  end;
clasohm@0
   120
clasohm@0
   121
clasohm@0
   122
clasohm@0
   123
(** the type syntax **)
clasohm@0
   124
wenzelm@18
   125
(* parse ast translations *)
clasohm@0
   126
wenzelm@347
   127
fun tapp_ast_tr (*"_tapp"*) [ty, f] = Appl [f, ty]
wenzelm@3778
   128
  | tapp_ast_tr (*"_tapp"*) asts = raise AST ("tapp_ast_tr", asts);
wenzelm@347
   129
wenzelm@347
   130
fun tappl_ast_tr (*"_tappl"*) [ty, tys, f] =
wenzelm@347
   131
      Appl (f :: ty :: unfold_ast "_types" tys)
wenzelm@3778
   132
  | tappl_ast_tr (*"_tappl"*) asts = raise AST ("tappl_ast_tr", asts);
clasohm@0
   133
clasohm@0
   134
fun bracket_ast_tr (*"_bracket"*) [dom, cod] =
clasohm@0
   135
      fold_ast_p "fun" (unfold_ast "_types" dom, cod)
wenzelm@3778
   136
  | bracket_ast_tr (*"_bracket"*) asts = raise AST ("bracket_ast_tr", asts);
clasohm@0
   137
clasohm@0
   138
wenzelm@18
   139
(* print ast translations *)
clasohm@0
   140
wenzelm@3778
   141
fun tappl_ast_tr' (f, []) = raise AST ("tappl_ast_tr'", [f])
clasohm@0
   142
  | tappl_ast_tr' (f, [ty]) = Appl [Constant "_tapp", ty, f]
wenzelm@347
   143
  | tappl_ast_tr' (f, ty :: tys) =
wenzelm@347
   144
      Appl [Constant "_tappl", ty, fold_ast "_types" tys, f];
clasohm@0
   145
clasohm@0
   146
fun fun_ast_tr' (*"fun"*) asts =
clasohm@0
   147
  (case unfold_ast_p "fun" (Appl (Constant "fun" :: asts)) of
wenzelm@18
   148
    (dom as _ :: _ :: _, cod)
clasohm@0
   149
      => Appl [Constant "_bracket", fold_ast "_types" dom, cod]
clasohm@0
   150
  | _ => raise Match);
clasohm@0
   151
clasohm@0
   152
clasohm@0
   153
(* type_ext *)
clasohm@0
   154
clasohm@0
   155
val sortT = Type ("sort", []);
clasohm@0
   156
val classesT = Type ("classes", []);
clasohm@0
   157
val typesT = Type ("types", []);
clasohm@0
   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),
clasohm@330
   163
   Mfix ("_::_",        [tidT, sortT] ---> typeT,      "_ofsort", [max_pri, 0], max_pri),
wenzelm@239
   164
   Mfix ("_::_",        [tvarT, sortT] ---> typeT,     "_ofsort", [max_pri, 0], max_pri),
wenzelm@239
   165
   Mfix ("_",           idT --> sortT,                 "", [], max_pri),
wenzelm@2584
   166
   Mfix ("{}",          sortT,                         "_topsort", [], max_pri),
wenzelm@239
   167
   Mfix ("{_}",         classesT --> sortT,            "_sort", [], max_pri),
wenzelm@239
   168
   Mfix ("_",           idT --> classesT,              "", [], max_pri),
wenzelm@239
   169
   Mfix ("_,_",         [idT, classesT] ---> classesT, "_classes", [], max_pri),
clasohm@330
   170
   Mfix ("_ _",         [typeT, idT] ---> typeT,       "_tapp", [max_pri, 0], max_pri),
clasohm@330
   171
   Mfix ("((1'(_,/ _'))_)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri),
wenzelm@239
   172
   Mfix ("_",           typeT --> typesT,              "", [], max_pri),
wenzelm@239
   173
   Mfix ("_,/ _",       [typeT, typesT] ---> typesT,   "_types", [], max_pri),
wenzelm@239
   174
   Mfix ("(_/ => _)",   [typeT, typeT] ---> typeT,     "fun", [1, 0], 0),
clasohm@624
   175
   Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT,    "_bracket", [0, 0], 0),
wenzelm@2678
   176
   Mfix ("'(_')",       typeT --> typeT,               "", [0], max_pri),
wenzelm@2678
   177
   Mfix ("'_",          typeT,                         "dummy", [], max_pri)]
wenzelm@258
   178
  []
wenzelm@347
   179
  ([("_tapp", tapp_ast_tr), ("_tappl", tappl_ast_tr), ("_bracket", bracket_ast_tr)],
wenzelm@239
   180
   [],
wenzelm@239
   181
   [],
wenzelm@239
   182
   [("fun", fun_ast_tr')])
wenzelm@2699
   183
  TokenTrans.token_translation
wenzelm@239
   184
  ([], []);
clasohm@0
   185
clasohm@0
   186
end;