Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
Forked from mwotton/gist:2633129
Created May 8, 2012 06:58
Show Gist options
  • Save NathanHowell/2633184 to your computer and use it in GitHub Desktop.
Save NathanHowell/2633184 to your computer and use it in GitHub Desktop.
fun with forall
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Forall where
import Control.Applicative
import Data.Aeson
import Control.Monad
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
data Proxy a = Proxy
-- go fails miserably
go :: forall a . (FromJSON a, Show a) => Proxy a -> Value -> String
go _ v =
case fromJSON v of
Error s -> "broken: " ++ s
Success (thing :: a) -> show thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment