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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
structure ScopeRule =
struct
open SMLUnit.Test SMLUnit.Assert
structure S1 = struct
val A = 1
type A = int
structure S2 = struct
val A = 2
end
end
structure S1 = struct
val A = 3
type A = string
structure S2 = struct
val A = 4
datatype d1 = B
exception E1
datatype A1 = A1 of A
end
val d1B = S2.B
val E2 = S2.E1
val A = 5
type A = bool
structure S3 = S1
end
structure S1 = S1
type A = word
fun testVID () =
let
val A = "A"
datatype A = B
val _ = assertEqualString "A" A
datatype A = A
| C
val _ = assertTrue (A <> C)
in
()
end
fun testVIDFun () =
let
val A = 1
fun f1 (0, _) = A
| f1 (A, 0) = A + 1
| f1 (_, B) = A + B + 2
val _ = assertEqualInt 1 (f1 (0, 2))
val _ = assertEqualInt 3 (f1 (2, 0))
val _ = assertEqualInt 6 (f1 (2, 3))
in
()
end
fun testVIDFunSameName () =
let
fun A A = A
val _ = assertEqualInt 2 (A 2)
in
()
end
fun testVIDFunRec () =
let
fun A x = if true then 1 else A x
val _ = assertEqualInt 1 (A 2)
in
()
end
fun testVIDFunLet () =
let
fun A x =
let
fun A x = if true then x else A x
in
A
end
val _ = assertEqualInt 2 (A 1 2)
in
()
end
fun testVIDFn1 () =
let
val A = 1
val f1 = fn (0, _) => A
| (A, 0) => A + 1
| (_, B) => A + B + 2
val _ = assertEqualInt 1 (f1 (0, 2))
val _ = assertEqualInt 3 (f1 (2, 0))
val _ = assertEqualInt 6 (f1 (2, 3))
in
()
end
fun testVIDFn2 () =
let
val A = 1
val _ = assertEqualInt 3 ((fn A => (fn A => A)) 2 3)
in
()
end
fun testVIDFnDatatype () =
let
datatype d1 = A
val _ = assertTrue (A = ((fn A => A) A))
in
()
end
fun testVIDCase () =
let
val A = 1
val v1 = case (0, 2) of
(0, _) => A
| (A, 0) => A + 1
| (_, B) => A + B + 2
val v2 = case (2, 0) of
(0, _) => A
| (A, 0) => A + 1
| (_, B) => A + B + 2
val v3 = case (2, 3) of
(0, _) => A
| (A, 0) => A + 1
| (_, B) => A + B + 2
val _ = assertEqualInt 1 v1
val _ = assertEqualInt 3 v2
val _ = assertEqualInt 6 v3
in
()
end
fun testVIDCaseDatatype () =
let
datatype d1 = A
val v1 = case A of
A => A
val _ = assertTrue (v1 = A)
in
()
end
fun testVIDStruct () =
let
val A = 1
val _ = assertEqualInt 1 S1.S3.A
val _ = assertEqualInt 2 S1.S3.S2.A
val _ = assertEqualInt 4 S1.S2.A
val _ = assertEqualInt 5 S1.A
val _ = assertTrue (S1.S2.B = S1.d1B)
val _ = case S1.E2 of
S1.S2.E1 => ()
| _ => fail "NG"
in
()
end
fun testVIDLocal () =
let
val A = 1
val B = 2
val C = 3
local
val _ = assertEqualInt 1 A
val A = 4
val _ = assertEqualInt 4 A
datatype d1 = C
in
val _ = assertEqualInt 4 A
val _ = assertEqualInt 2 B
val B = 5
val _ = assertEqualInt 5 B
end
val _ = assertEqualInt 1 A
val _ = assertEqualInt 5 B
val _ = assertEqualInt 3 C
in
()
end
fun testVIDLet () =
let
val A = 1
val B = 2
val C = 3
val _ =
let
val _ = assertEqualInt 1 A
val A = 4
val _ = assertEqualInt 4 A
datatype d1 = C
in
assertEqualInt 4 A;
assertEqualInt 2 B
end
val _ = assertEqualInt 1 A
val _ = assertEqualInt 3 C
in
()
end
fun testVIDExn () =
let
datatype A = A
exception A of int
val B = 1
val v1 = (raise A 2; B) handle A B => B
val _ = assertEqualInt 2 v1
val v2 = (raise A 2) handle A B => (raise A 3) handle A B => B
val _ = assertEqualInt 3 v2
in
()
end
fun testTycon () =
let
type A = int
type A = A
datatype B = A of A
type A = string
val _ = A 1
datatype A = A1 of A
| A2
val _ = A1 (A1 A2)
in
()
end
fun testTyconStruct () =
let
val _ = S1.S2.A1 "A"
in
()
end
fun testTyconLocal () =
let
type A = int
local
datatype A1 = A1 of A
type A = string
in
val A2 = A1
type A = A
datatype A3 = A3 of A
end
datatype A4 = A4 of A
val _ = A2 1
val _ = A3 "A"
val _ = A4 "A"
in
()
end
fun testTyconLet () =
let
type A = int
val _ =
let
datatype A1 = A1 of A
type A = string
val A2 = A1
type A = A
datatype A3 = A3 of A
in
A2 1;
A3 "A"
end
datatype A4 = A4 of A
val _ = A4 1
in
()
end
fun testTyvar () =
let
datatype ('a, 'b) A = A1 of 'a * 'b
type ('b, 'a) A = ('a, 'b) A
datatype ('a, 'b) B = A2 of ('a, 'b) A
val v1 : (string, int) B = A2 (A1 (1, "A"))
in
()
end
val tests = TestList [
Test ("testVID", testVID),
Test ("testVIDFun", testVIDFun),
Test ("testVIDFunSameName", testVIDFunSameName),
Test ("testVIDFunRec", testVIDFunRec),
Test ("testVIDFunLet", testVIDFunLet),
Test ("testVIDFn1", testVIDFn1),
Test ("testVIDFn2", testVIDFn2),
Test ("testVIDFnDatatype", testVIDFnDatatype),
Test ("testVIDCase", testVIDCase),
Test ("testVIDCaseDatatype", testVIDCaseDatatype),
Test ("testVIDStruct", testVIDStruct),
Test ("testVIDLocal", testVIDLocal),
Test ("testVIDLet", testVIDLet),
Test ("testVIDExn", testVIDExn),
Test ("testTycon", testTycon),
Test ("testTyconStruct", testTyconStruct),
Test ("testTyconLocal", testTyconLocal),
Test ("testTyconLet", testTyconLet),
Test ("testTyvar", testTyvar)
]
end
|