3

I was going through some notes and I realized something is amiss.

When emulating lazy computation (without open Lazy;) one can do the following for a stream of ones.

datatype 'a susp = Susp of (unit -> 'a)

datatype 'a stream' = Cons of 'a * ('a stream') susp
type 'a stream = ('a stream') susp

fun delay (f ) = Susp(f);

fun force  (Susp(f)) = f ();

val rec ones' = fn () => Cons(1, delay(ones'));

val ones = delay(ones')

fun ltail(Susp(s)) = ltail'(force s)   
and ltail' (Cons(x,s)) = s

But for getting a suspended tail the types do not match up.

operator domain: 'Z susp   
operand:         unit -> 'Y

What will need to change for the proper types for ltail ? I know what happens with a tail not suspended. I just want to figure out what the notes were saying for the suspended version.

phwd
  • 19,975
  • 5
  • 50
  • 78

1 Answers1

2
fun ltail(Susp(s)) = ltail'(force s)

The problem here is that force takes a value of type susp, but you call it with a value of type () -> 'a. I.e. you take the function out of the susp value and then call force on the function instead of the susp value. You should just do:

fun ltail s = ltail' (force s)
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Yeah I tried that when I sat down with the notes , though I wasn't sure why it was with Susp(s) in the notes. Oh well ... I will assume it was a typo and life moves on. – phwd Mar 15 '11 at 02:15