Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun with forall
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ImpredicativeTypes #-}
module Forall where
import Control.Applicative
import Data.Aeson
import Control.Monad
type Foo = forall a. (FromJSON a) => a -> String
data Bar = Bar Int
data Baz = Baz Int Int
instance FromJSON Baz where
parseJSON (Object v) = Baz <$> v .: "baz1"
<*> v .: "baz2"
parseJSON _ = mzero
instance FromJSON Bar where
parseJSON (Object v) = Bar <$> v .: "bar"
parseJSON _ = mzero
showBaz_foo o = case fromJSON o of
Error s -> s
Success (Baz a b) -> "Baz " ++ show a ++ "," ++ show b
showBar_foo o = case fromJSON o of
Error s -> s
Success (Bar a) -> "Bar " ++ show a
fooList = [showBaz_foo,showBar_foo]
objects = [object ["baz1" .= Number 1, "baz2" .= Number 2],
object ["bar" .= Number 3]]
-- res works fine
res = zipWith (\a b -> a b) fooList objects
-- go fails miserably
go :: [(String, Foo)] -> Value -> String -> String
go table v key = case lookup key table of
Nothing -> "not there"
Just x -> case fromJSON v of
Error s -> "broken: " ++ s
Success thing -> x thing
@mwotton

This comment has been minimized.

Copy link
Owner Author

mwotton commented May 8, 2012

*Forall> :r
[1 of 1] Compiling Forall ( forall.hs, interpreted )

forall.hs:43:22:
Couldn't match expected type t0 -> t1' with actual typeforall a. FromJSON a => a -> String'
The function x' is applied to one argument, but its typeFoo' has none
In the expression: x thing
In a case alternative: Success thing -> x thing
Failed, modules loaded: none.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.