Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Recursive Unions


Is it possible to have recursive "ADT" a-la-ML using unions?

I'm leaning towards no, as I get the following error:

syntax error, unexpected Ttick

making reference to

`Prim (byte[:], `Expression, `Expression)

from the code below:

use std

type Expression = union
    `Int int
    `Prim (byte[:], `Expression, `Expression)
;;

const eval = { e
    match e
    | `Int x: -> x
    | `Prim ("+", x, y): -> eval(x) + eval(y)
    | `Prim ("-", x, y): -> eval(x) - eval(y)
    | `Prim ("*", x, y): -> eval(x) * eval(y)
    ;;
}

const main = {
  var x = eval(`Int 2)
  std.put("2 = {}", x)
  var y = eval(`Prim("+", `Int 2, `Int 5))
  std.put("2 + 5 = {}", y)
}

Follow-Ups:
Re: Recursive UnionsOri Bernstein <ori@xxxxxxxxxxxxxx>