Question

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.

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top