HOME

2020-08-03

sexp-grammar

I tried to experiment a bit with sexp-grammar today, to see if I could use it to create an isomorphic transformation from Futhark core language (IR) to sexps, which would be very useful for testing stuff like my last use and interference analyses. After getting Troels to bump the stack snapshot, I was successfully able to add sexp-grammar as a dependency to futhark and start creating isomorphisms. Everything started out pretty well. For example, here are the isomorphisms for Name and VName:

 1: instance SexpIso Name where
 2:   sexpIso = with (\name -> name . symbol)
 3: 
 4: 
 5: instance SexpIso VName where
 6:   sexpIso = with $ \vname ->  -- Person is isomorphic to:
 7:     Sexp.list (                           -- a list with
 8:       Sexp.el (Sexp.sym "vname") >>>          -- a symbol "person",
 9:       Sexp.el sexpIso         >>>          -- a string, and
10:       Sexp.el Sexp.int)  >>>     -- an optional keyword :age with int value.
11:     vname

I then tried taking on a bigger task: PrimValue. Unfortunately, down the line that requires isomorphims for Int8 and friends, which don't derive Generic, and sexp-grammar doesn't work

Well, turns out it wasn't actually that hard, to fix. I can just use iso to transform to and from an int, which I then read:

1: instance SexpIso IntValue where
2:   sexpIso = match
3:     $ With (\int8val -> int8val . iso fromIntegral fromIntegral . Sexp.int)
4:     $ With (\int16val -> int16val . iso fromIntegral fromIntegral . Sexp.int)
5:     $ With (\int32val -> int32val . iso fromIntegral fromIntegral . Sexp.int)
6:     $ With (\int64val -> int64val . iso fromIntegral fromIntegral . Sexp.int)
7:     $ End

Then I don't need to specify an isomorphism for Int8.

The next problem came when I tried to use sexp-grammar inside Futhark.IR.Primitive. Because it is marked as Safe, but sexp-grammar is not, I need to change Futhark.IR.Primitive to be Trustworthy instead. I don't know if this is the right way to go about things, and ideally sexp-grammar would just be safe as well.