[Ur] Polymorphic variants and JSON
Edward Z. Yang
ezyang at MIT.EDU
Sat Apr 14 03:31:49 EDT 2012
I've brought this discussion here from a private thread with Adam,
at his prompting.
The basic problem is this: I have an algebraic data type:
datatype foo = Foo of int
| Bar of int * bool
| Baz of int * int
and I'd like to send/receive this data from Ur using the JSON interchange
format.
One thing to note is that JSON doesn't have particularly good support
for encoding variants. Let's arbitrarily choose one encoding (taken
from the generic support from the aeson package in Haskell):
Foo 1 === { "Foo": [1] }
Bar (2, True) === { "Bar": [2, True] }
Baz (4, 2) === { "Baz": [4,2] }
So, what is the easiest/cleanest way to write the JSON marshalling code
here, assuming that we are using the metaprogramming Ur library?
Adam suggests that *polymorphic variants* might come to the rescue here.
http://www.impredicative.com/wiki/index.php/Polymorphic_Variants_Usage_Example
has some useful information. One unknown is that both examples in the wiki
correspond to record types that map to unit; does it extend straightforwardly
whe I also want to store data in the variant? I don't see why not,
but I'll experiment with that tomorrow.
Edward
More information about the Ur
mailing list