(* simple-types.sml
*
* This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
*
* COPYRIGHT (c) 2015 The University of Chicago
* All rights reserved.
*
* Internal representation of Diderot types after simplification. This is essentially
* the Types representations with the meta variables stripped out.
*)
structure SimpleTypes =
struct
datatype ty
= T_Bool
| T_Int
| T_String
| T_Sequence of ty * dim (* fixed-sized sequence of values *)
| T_DynSequence of ty (* dynamically sized sequence of values *)
(* convolution kernel; argument is number of levels of differentiation *)
| T_Kernel of diff
(* scalars, vectors, matrices, etc.; argument is tensor shape *)
| T_Tensor of shape
(* data sets from NRRD *)
| T_Image of {
dim : dim, (* 1D, 2D, or 3D data set *)
shape : shape (* tensor shape; order is length of list *)
}
(* continuous field reconstructed from a data set *)
| T_Field of {
diff : diff, (* number of levels of differentiation supported *)
dim : dim, (* dimension of domain (2D or 3D field) *)
shape : shape (* shape of tensors in range; order is length of list *)
}
| T_Fun of ty list * ty
withtype diff = int
and shape = int list
and dim = int
(* instantiated meta variable values *)
datatype meta_arg
= TY of ty
| DIFF of diff
| SHAPE of shape
| DIM of dim
local
fun listToString fmt sep items = String.concatWith sep (List.map fmt items)
val shapeToString = listToString Int.toString ","
in
fun toString ty = (case ty
of T_Bool => "bool"
| T_Int => "int"
| T_String => "string"
| T_Sequence(ty, dim) => concat[toString ty, "{", Int.toString dim, "}"]
| T_DynSequence ty => toString ty ^ "{}"
| T_Kernel n => "kernel#" ^ Int.toString n
| T_Tensor[] => "real"
| T_Tensor[2] => "vec2"
| T_Tensor[3] => "vec3"
| T_Tensor[4] => "vec4"
| T_Tensor shape => concat["tensor[", shapeToString shape, "]"]
| T_Image{dim, shape} => concat[
"image(", Int.toString dim, ")[", shapeToString shape, "]"
]
| T_Field{diff, dim, shape} => concat[
"field#", Int.toString diff, "(", Int.toString dim,
")[", shapeToString shape, "]"
]
| T_Fun(tys1, ty2) => let
fun tysToString [] = "()"
| tysToString [ty] = toString ty
| tysToString tys = String.concat[
"(", listToString toString " * " tys, ")"
]
in
String.concat[tysToString tys1, " -> ", toString ty2]
end
(* end case *))
end (* local *)
(* return the range (return type) of a function type *)
fun rngOf (T_Fun(_, ty)) = ty
| rngOf ty = raise Fail(concat["SimpleTypes.rngOf(", toString ty, ")"])
local
structure Ty = Types
structure TU = TypeUtil
(* convert to fully resolved monomorphic forms *)
in
(* convert a Types.ty to a SimpleTypes.ty *)
fun simplify ty = (case ty
of Ty.T_Var(Ty.TV{bind, ...}) => (case !bind
of NONE => raise Fail "unresolved type variable"
| SOME ty => simplify ty
(* end case *))
| Ty.T_Bool => T_Bool
| Ty.T_Int => T_Int
| Ty.T_String => T_String
| Ty.T_Sequence(ty, dim) => T_Sequence(simplify ty, TU.monoDim dim)
| Ty.T_DynSequence ty => T_DynSequence(simplify ty)
| Ty.T_Kernel n => T_Kernel(TU.monoDiff n)
| Ty.T_Tensor shape => T_Tensor(TU.monoShape shape)
| Ty.T_Image{dim, shape} => T_Image{
dim = TU.monoDim dim,
shape = TU.monoShape shape
}
| Ty.T_Field{diff, dim, shape} => T_Field{
diff = TU.monoDiff diff,
dim = TU.monoDim dim,
shape = TU.monoShape shape
}
| Ty.T_Fun(tys1, ty2) => T_Fun(List.map simplify tys1, simplify ty2)
(* end case *))
end
end