VinylRecords/Vinyl

Make a Rec a functor, applicative, and monad?

Closed this issue · 3 comments

Could something like this be possible?

parsedRecord = do
    stringRecord
        <- p @"firstName" "Bob" 
        :& p @"lastName"  "Bill"
        :& p @"age"       "55"
        :& RNil
    
    parserRecord
        <- p @"firstName" id
        :& p @"lastName"  id
        :& p @"age"       (read :: String -> Int)
        :& RNil
    
    return $ parserRecord stringRecord

result = do
    p1
        <- p @"x" 10
        :& p @"y" 5
        :& RNil
    p2
        <- p @"x" 4
        :& p @"y" 6
        :& RNil

    return $ p1 + p2

Record types do not have the right kind to be Functor/Applicative/Monad, but vinyl has (<<$>>) and (<<*>>) for applicative style composition, which is sufficient for your examples minus the sugar.

Here is an example, with some type classes to reduce boilerplate.

A form of monadic composition seems possible but probably not worth the hassle.

(should this be closed?)

I think so, but I’m happy to re-open if there is more to be said!