[Ur] Polymorphic variants and JSON

Edward Z. Yang ezyang at MIT.EDU
Sat Apr 14 13:34:58 EDT 2012


I'm a little confused by the definition of json_record;
why does it take a (fl : folder ts) as an argument, but then
not use it for anything?

Edward

Excerpts from Edward Z. Yang's message of Sat Apr 14 03:31:49 -0400 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