1 |
(* simple.sml |
(* simple.sml |
2 |
* |
* |
3 |
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu) |
* COPYRIGHT (c) 2013 The Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 |
* All rights reserved. |
* All rights reserved. |
5 |
* |
* |
6 |
* A simplified AST representation of a Diderot program. This representation has the property |
* A simplified AST representation of a Diderot program. This representation has the property |
11 |
structure Simple = |
structure Simple = |
12 |
struct |
struct |
13 |
|
|
14 |
|
(* types with the meta variables resolved *) |
15 |
|
type ty = SimpleTypes.ty |
16 |
|
|
17 |
|
(* resolved meta-variable arguments to basis functions *) |
18 |
|
datatype meta_arg = datatype SimpleTypes.meta_arg |
19 |
|
|
20 |
datatype var_kind = datatype AST.var_kind |
datatype var_kind = datatype AST.var_kind |
21 |
|
|
22 |
datatype var = datatype AST.var |
datatype var = V of { |
23 |
|
name : string, (* print name of variable *) |
24 |
|
id : Stamp.stamp, (* unique ID *) |
25 |
|
kind : var_kind, (* variable kind *) |
26 |
|
ty : ty (* type *) |
27 |
|
} |
28 |
|
|
29 |
datatype program = Program of { |
datatype program = Program of { |
30 |
globals : var list, |
globals : var list, |
90 |
= E_Var of var |
= E_Var of var |
91 |
| E_Lit of Literal.literal |
| E_Lit of Literal.literal |
92 |
| E_Tuple of var list |
| E_Tuple of var list |
93 |
| E_Apply of var * Types.meta_var list * var list * Types.ty |
| E_Apply of var * var list * ty (* user-defined function *) |
94 |
|
| E_Prim of AST.var * meta_arg list * var list * ty (* Diderot builtin *) |
95 |
| E_Cons of var list |
| E_Cons of var list |
96 |
| E_Slice of var * var option list * Types.ty (* tensor slicing *) |
| E_Slice of var * var option list * ty (* tensor slicing *) |
97 |
| E_Coerce of {srcTy : Types.ty, dstTy : Types.ty, x : var} |
| E_Coerce of {srcTy : ty, dstTy : ty, x : var} |
98 |
| E_Input of Types.ty * string * string * var option |
| E_Input of ty * string * string option * var option |
99 |
| E_LoadImage of ImageInfo.info * var |
| E_LoadImage of ImageInfo.info * var |
100 |
|
|
101 |
fun typeOf (E_Var x) = Var.monoTypeOf x |
fun typeOf (E_Var(V{ty, ...})) = ty |
102 |
| typeOf (E_Lit lit) = (case lit |
| typeOf (E_Lit lit) = (case lit |
103 |
of (Literal.Int _) => Types.T_Int |
of (Literal.Int _) => SimpleTypes.T_Int |
104 |
| (Literal.Float _) => Types.realTy |
| (Literal.Float _) => SimpleTypes.T_Tensor[] |
105 |
| (Literal.String s) => Types.T_String |
| (Literal.String s) => SimpleTypes.T_String |
106 |
| (Literal.Bool _) => Types.T_Bool |
| (Literal.Bool _) => SimpleTypes.T_Bool |
107 |
(* end case *)) |
(* end case *)) |
108 |
| typeOf (E_Tuple _) = raise Fail "E_Tuple" |
| typeOf (E_Tuple _) = raise Fail "E_Tuple" |
109 |
| typeOf (E_Apply(_, _, _, ty)) = ty |
| typeOf (E_Apply(_, _, ty)) = ty |
110 |
|
| typeOf (E_Prim(_, _, _, ty)) = ty |
111 |
| typeOf (E_Cons[]) = raise Fail "impossible empty E_Cons" |
| typeOf (E_Cons[]) = raise Fail "impossible empty E_Cons" |
112 |
| typeOf (E_Cons(x::xs)) = let |
| typeOf (E_Cons(V{ty, ...}::xs)) = let |
113 |
val d = List.length xs + 1 |
val d = List.length xs + 1 |
|
val ty = Var.monoTypeOf x |
|
114 |
in |
in |
115 |
case TypeUtil.pruneHead ty |
case ty |
116 |
of Types.T_Tensor shape => Types.T_Tensor(Types.shapeExt(shape, Types.DimConst d)) |
of SimpleTypes.T_Tensor shape => SimpleTypes.T_Tensor(shape @ [d]) |
117 |
| _ => raise Fail(concat[ |
| _ => raise Fail(concat[ |
118 |
"element of tensor construction is ", TypeUtil.toString ty, ", expected tensor" |
"element of tensor construction is ", SimpleTypes.toString ty, |
119 |
|
", expected tensor" |
120 |
]) |
]) |
121 |
(* end case *) |
(* end case *) |
122 |
end |
end |
123 |
| typeOf (E_Slice(_, _, ty)) = ty |
| typeOf (E_Slice(_, _, ty)) = ty |
124 |
| typeOf (E_Input(ty, _, _, _)) = ty |
| typeOf (E_Input(ty, _, _, _)) = ty |
125 |
| typeOf (E_LoadImage(ImageInfo.ImgInfo{dim, ty=(dd, _), ...}, _)) = |
| typeOf (E_LoadImage(ImageInfo.ImgInfo{dim, ty=(dd, _), ...}, _)) = |
126 |
Types.T_Image{ |
SimpleTypes.T_Image{dim = dim, shape = dd} |
|
dim = Types.DimConst dim, |
|
|
shape = Types.Shape(List.map Types.DimConst dd) |
|
|
} |
|
127 |
|
|
128 |
end |
end |