4 |
* All rights reserved. |
* All rights reserved. |
5 |
*) |
*) |
6 |
|
|
7 |
structure Types : TYPES = |
structure Types = |
8 |
sig |
struct |
9 |
|
|
10 |
(* kinds for type variables *) |
(* kinds for type variables *) |
11 |
datatype kind |
datatype kind |
13 |
| TK_INT (* ranges over integer types *) |
| TK_INT (* ranges over integer types *) |
14 |
| TK_FLT (* ranges over floating-point types *) |
| TK_FLT (* ranges over floating-point types *) |
15 |
| TK_RAW (* ranges over raw (scalar) types *) |
| TK_RAW (* ranges over raw (scalar) types *) |
16 |
|
| TK_TYPE (* ranges over types *) |
17 |
|
(* Question: do we want kinds for tensors of different orders (e.g., TK_TENSOR of word?); |
18 |
|
* Then TK_RAW would be TK_TENSOR of 0w0. |
19 |
|
*) |
20 |
|
|
21 |
|
(* raw numeric types as supported by NRRD *) |
22 |
datatype raw_ty |
datatype raw_ty |
23 |
= Int8 | UInt8 |
= RT_Int8 | RT_UInt8 |
24 |
| Int16 | UInt16 |
| RT_Int16 | RT_UInt16 |
25 |
| Int32 | UInt32 |
| RT_Int32 | RT_UInt32 |
26 |
| Int64 | UInt64 |
| RT_Int64 | RT_UInt64 |
27 |
| Float | Double |
| RT_Float | RT_Double |
28 |
|
| RT_Var of var |
29 |
|
|
30 |
datatype ty |
and ty |
31 |
= T_Var of var |
= T_Var of var |
32 |
| T_Bool |
| T_Bool |
33 |
(* scalars, vectors, matrices, etc. *) |
(* scalars, vectors, matrices, etc. *) |
36 |
| T_Data of { |
| T_Data of { |
37 |
dim : dim, (* 2D or 3D data set *) |
dim : dim, (* 2D or 3D data set *) |
38 |
order : dim list, (* tensor order/dimension info *) |
order : dim list, (* tensor order/dimension info *) |
39 |
rep_ty : ty (* representation type of elements (raw kind) *) |
repTy : raw_ty (* representation type of elements (raw kind) *) |
40 |
} |
} |
41 |
(* continuous field reconstructed from a data set *) |
(* continuous field reconstructed from a data set *) |
42 |
| T_Field of { |
| T_Field of { |
43 |
dim : dim, (* 2D or 3D field *) |
dim : dim, (* 2D or 3D field *) |
44 |
order : dim list, (* tensor order/dimension info *) |
order : dim list, (* tensor order/dimension info *) |
45 |
rep_ty : ty, (* representation type of elements (raw kind) *) |
repTy : raw_ty, (* representation type of elements (raw kind) *) |
46 |
ty : ty (* type of samples (float kind) *) |
ty : raw_ty (* type of samples (float kind) *) |
47 |
} |
} |
48 |
|
|
49 |
and dim = DIM of word | DIMVAR of var |
and dim = DIM of word | DIMVAR of var |
50 |
|
|
51 |
(* type variables *) |
(* type variables; the kind field restricts their range *) |
52 |
and var = TV of { |
and var = TV of { |
53 |
stamp : Stamp.stamp, |
kind : kind, |
54 |
kind : kind |
stamp : Stamp.stamp |
55 |
} |
} |
56 |
|
|
57 |
(* given a dataset or field type, return the element type, which will be |
(* given a dataset or field type, return the element type, which will be |
58 |
* a tensor type. |
* a tensor type. |
59 |
*) |
*) |
60 |
fun elemTypeOf (T_Data{order, rep_ty, ...}) = T_Tensor{order=order, ty=rep_ty} |
fun elemTypeOf (T_Data{order, repTy, ...}) = T_Tensor{order=order, ty=repTy} |
61 |
| elemTypeOf (T_Field{order, ty, ...}) = T_Tensor{order=order, ty=ty} |
| elemTypeOf (T_Field{order, ty, ...}) = T_Tensor{order=order, ty=ty} |
62 |
| elemTypeOf _ = raise Fail "not a dataset/field" |
| elemTypeOf _ = raise Fail "not a dataset/field" |
63 |
|
|
64 |
|
(* is a type well-formed? *) |
65 |
|
fun wellFormed ty = let |
66 |
|
(* is a dimension well-formed? *) |
67 |
|
fun okDim (DIM w) = (w > 0w0) |
68 |
|
| okDim (DIMVAR(TV{kind=TK_DIM, ...})) = true |
69 |
|
| okDim _ = false |
70 |
|
(* is a raw type well formed *) |
71 |
|
fun okRawTy (RT_Var(TV{kind=TK_INT, ...})) = true |
72 |
|
| okRawTy (RT_Var(TV{kind=TK_FLT, ...})) = true |
73 |
|
| okRawTy (RT_Var(TV{kind=TK_RAW, ...})) = true |
74 |
|
| okRawTy (RT_Var _) = false |
75 |
|
| okRawTy _ = true |
76 |
|
(* is a raw type a floating-point type? *) |
77 |
|
fun okFloatTy (RT_Var(TV{kind=TK_FLT, ...})) = true |
78 |
|
| okFloatTy RT_Float = true |
79 |
|
| okFloatTy RT_Double = true |
80 |
|
| okFloatTy _ = false |
81 |
|
fun wellFormed' (T_Var(TV{kind=TK_TYPE, ...})) = true |
82 |
|
| wellFormed' (T_Var _) = false |
83 |
|
| wellFormed' (T_Tensor{order, ty}) = |
84 |
|
List.all okDim order |
85 |
|
andalso okRawTy ty |
86 |
|
| wellFormed' (T_Data{dim, order, repTy}) = |
87 |
|
okDim dim |
88 |
|
andalso List.all okDim order |
89 |
|
andalso okRawTy repTy |
90 |
|
| wellFormed' (T_Field{dim, order, repTy, ty}) = |
91 |
|
okDim dim |
92 |
|
andalso List.all okDim order |
93 |
|
andalso okRawTy repTy |
94 |
|
andalso okFloatTy repTy |
95 |
|
| wellFormed' _ = false |
96 |
|
in |
97 |
|
wellFormed' ty |
98 |
|
end |
99 |
|
|
100 |
end |
end |