tags:

views:

37

answers:

1

I want to implement a lazy tree in Standard ML with

datatype tr = N of int * unit->tr * unit->tr;

I tried to use

fun form k = N(k,fn()=>form(k+1),fn()=>form(k+3));

as a test of concept, but I get error message saying type mismatch.

What would be the proper way of implementing a lazy tree in ML?

+3  A: 
datatype tr = N of int * unit->tr * unit->tr;

This is parsed as

datatype tr = N of (int * unit) -> ( (tr * unit) -> tr)

You want

datatype tr = N of int * (unit->tr) * (unit->tr)
sepp2k
I think they are the same. - datatype tr = N of int * unit->tr * unit->tr;> New type names: tr/1 datatype tr = (tr/1,{con N : (int * unit -> tr/1 * unit -> tr/1) -> tr/1}) con N = fn : (int * unit -> tr/1 * unit -> tr/1) -> tr/1- datatype tr = N of int * (unit->tr) * (unit->tr);> New type names: tr/2 datatype tr = (tr/2,{con N : int * (unit -> tr/2) * (unit -> tr/2) -> tr/2}) con N = fn : int * (unit -> tr/2) * (unit -> tr/2) -> tr/2
Bo Tian
@Bo Tian: Why does that output make you think they're the same? The very fact that the output for the second definition still contains the parens (it would remove them if they were redundant) should tell you that they're not. Also the fact that your form function will actually compile if you use my datatype definition.
sepp2k
@Bo Tian: Also note that if you type in the second definition in my answer (which is equivalent to the first), the output will not contain the parents because those are indeed redundant.
sepp2k
that's absolutely correct. Thank you.
Bo Tian