Skip to content

Instantly share code, notes, and snippets.

@hugoduncan
Created January 12, 2014 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugoduncan/8385991 to your computer and use it in GitHub Desktop.
Save hugoduncan/8385991 to your computer and use it in GitHub Desktop.
An attempt to create a function that process an external representation of different data types
{-# LANGUAGE ScopedTypeVariables #-}
module Test () where
-- a type class to specify parsing and encoding of a type
-- (in reality I'm using hedn, which is similar to aeson)
class T a where
f :: String -> a
g :: a -> String
-- I want a function that can take a string, parse it, do something
-- on the resulting instance, and then encode it back into a string.
-- To do this I'm trying to use a dummy initial argument that is used
-- to specify the type the String is supposed to be parsed to.
invf :: (T a) => a -> String -> String
invf x y = g (f y :: a)
-- Test.hs:17:12:
-- Could not deduce (T a0) arising from a use of `g'
-- from the context (T a)
-- bound by the type signature for
-- invf :: T a => a -> String -> String
-- at Pallet/Test.hs:16:9-38
-- The type variable `a0' is ambiguous
-- Possible fix: add a type signature that fixes these type variable(s)
-- In the expression: g (f y :: a)
-- In an equation for `invf': invf x y = g (f y :: a)
@hugoduncan
Copy link
Author

Moving invf to be a function in the type class T seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment