spartanz/schemaz

Allow for recursive (self-referent) schemas

Closed this issue · 3 comments

vil1 commented

We need a way to represent recursive types like:

sealed trait Tree
final case class Node(left: Tree, right: Tree) extends Tree
final case class Leaf(label: Int)              extends Tree

One idea (thx @julienrf) would be to have a dedicated member in the GADT for that:

final class SchemaReference[F[_], A](ref: => F[A]) extends Schema[F, A]

which would allow us to use lazy vals or def to build such recursive schema, but would require interpreters implementers (that is us, most of the time) to properly handle the laziness.

lazy val tree = union(
  "Node" -+>: record(
    "left" -*>: tree :*:
    "right" -*>: tree
  ) :+:
  "Leaf" -+>: record(
    "label" -*>: prim(IntSchema)
  )
)

Another, more "essential" way would be to find a way to encode the "y-combinator", or some kind of fix-point, at the schema level.

Since it's considered "low-hanging fruit" as per @vil1, I'll try to take care of it.

vil1 commented

More precisely: "low hanging fruit hiding possible surprises" ;)
Have fun!

#54 should have closed this?