git @ Cat's Eye Technologies
Add failing tests for the shadow-avoiding name-mangling. Chris Pressey 1 year, 1 month ago
1 changed file(s) with 93 addition(s) and 2 deletion(s).
 325 325 oddsump(5,3,1) 326 326 ===> true 327 327 328 ### Properties of the `letrec` transformation 329 328 330 -> Tests for functionality "Desugar Lanthorn Program" 329 330 ### Properties of the `letrec` transformation 331 331 332 332 When a `letrec` is desugared, the generated functions have argument 333 333 names that are based on the original argument names. Also, the 354 354 => evensump = fun(x, y, z) -> evensump0(x, y, z, oddsump0, evensump0) 355 355 => in 356 356 => evensump(5, 3, 1) 357 358 The transformation mangles names that it generates so that they never 359 shadow names that appear in the user's program. 360 361 let 362 odd0 = fun(a, b, c) -> a 363 in 364 letrec 365 odd = fun(x) -> if eq(x, 0) then false else even(sub(x, 1)) 366 even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1)) 367 in 368 even(6) 369 => let 370 => odd0 = fun(a, b, c) -> a 371 => in 372 => let 373 => odd0 = fun(x, odd1, even1) -> let 374 => odd = fun(x1) -> odd1(x1, odd1, even1) 375 => even = fun(x1) -> even1(x1, odd1, even1) 376 => in 377 => if eq(x, 0) then false else even(sub(x, 1)) 378 => even0 = fun(x, odd1, even1) -> let 379 => odd = fun(x1) -> odd1(x1, odd1, even1) 380 => even = fun(x1) -> even1(x1, odd1, even1) 381 => in 382 => if eq(x, 0) then true else odd(sub(x, 1)) 383 => odd = fun(x) -> odd0(x, odd0, even0) 384 => even = fun(x) -> even0(x, odd0, even0) 385 => in 386 => even(6) 387 388 -> Tests for functionality "Evaluate Lanthorn Program" 389 390 letrec 391 odd = fun(x) -> if eq(x, 0) then false else even(sub(x, 1)) 392 odd0 = fun(a, b, c) -> a 393 even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1)) 394 in 395 even(6) 396 ===> true 397 398 You might think that instead of mangling names, we could just allow shadowing 399 in the language. But that by itself doesn't solve our problem, since you 400 could still say something like the following. The `letrec` desugaring would 401 have to be more aware of how it constructs names, at any rate, in order to 402 avoid the conflict here. And mangling is the simplest way to do that. 403 404 -> Tests for functionality "Desugar Lanthorn Program" 405 406 letrec 407 odd = fun(x) -> if eq(x, 0) then false else even(sub(x, 1)) 408 odd0 = fun(a, b, c) -> a 409 even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1)) 410 in 411 even(6) 412 => let 413 => odd0 = fun(x, odd1, odd01, even1) -> let 414 => odd = fun(x1) -> odd1(x1, odd1, odd01, even1) 415 => odd0 = fun(a1, b1, c1) -> odd01(a1, b1, c1, odd1, odd01, even1) 416 => even = fun(x1) -> even1(x1, odd1, odd01, even1) 417 => in 418 => if eq(x, 0) then false else even(sub(x, 1)) 419 => odd00 = fun(a, b, c, odd1, odd01, even1) -> let 420 => odd = fun(x1) -> odd1(x1, odd1, odd01, even1) 421 => odd0 = fun(a1, b1, c1) -> odd01(a1, b1, c1, odd1, odd01, even1) 422 => even = fun(x1) -> even1(x1, odd1, odd01, even1) 423 => in 424 => a 425 => even0 = fun(x, odd1, odd01, even1) -> let 426 => odd = fun(x1) -> odd1(x1, odd1, odd01, even1) 427 => odd0 = fun(a1, b1, c1) -> odd01(a1, b1, c1, odd1, odd01, even1) 428 => even = fun(x1) -> even1(x1, odd1, odd01, even1) 429 => in 430 => if eq(x, 0) then true else odd(sub(x, 1)) 431 => odd = fun(x) -> odd0(x, odd0, odd00, even0) 432 => odd0 = fun(a, b, c) -> odd00(a, b, c, odd0, odd00, even0) 433 => even = fun(x) -> even0(x, odd0, odd00, even0) 434 => in 435 => even(6) 436 437 -> Tests for functionality "Evaluate Lanthorn Program" 438 439 let 440 odd0 = fun(a, b, c) -> a 441 in 442 letrec 443 odd = fun(x) -> if eq(x, 0) then false else even(sub(x, 1)) 444 even = fun(x) -> if eq(x, 0) then true else odd(sub(x, 1)) 445 in 446 even(6) 447 ===> true