-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.ml
More file actions
45 lines (41 loc) · 1.3 KB
/
type.ml
File metadata and controls
45 lines (41 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(* Type.t : プログラムの型を表す型 *)
type t = TInt
| TBool
| TFun of t * t
| TVar of t option ref (* 型変数 *)
| Tlist of t
(* 新しい型変数を作る *)
(* Type.gen_type : unit -> Type.t *)
let gen_type () = TVar (ref None)
(* 型変数を中身で置き換えた型を返す。返ってくる型には型変数は含まれない *)
(* deref_type : Type.t -> Type.t *)
let rec deref_type ty = match ty with
TInt -> TInt
| TBool -> TBool
| TFun (ty1, ty2) ->
TFun(deref_type ty1, deref_type ty2)
| TVar (r) ->
begin match !r with
None ->
r := Some (TInt); (* 何でも良い *)
TInt
| Some (ty') ->
let ty'' = deref_type ty' in
r := Some (ty'');
ty''
end
| Tlist (ty) ->
Tlist (deref_type ty)
(* プログラムの型を文字列にする関数 *)
(* Type.to_string : Type.t -> string *)
let rec to_string ty = match ty with
TInt -> "int"
| TBool -> "bool"
| TFun (ty1, ty2) -> "(" ^ to_string ty1 ^ " -> " ^ to_string ty2 ^ ")"
| TVar (r) -> "tvar"
| Tlist (ty) -> to_string ty ^ " list"
(* プログラムの型をプリントする関数 *)
(* Type.print : Type.t -> unit *)
let print ty =
let str = to_string (deref_type ty) in
print_string str