Skip to content

Instantly share code, notes, and snippets.

@goldfirere
Created November 11, 2016 02:44
Show Gist options
  • Save goldfirere/a8c52c81e70407af7fd2b50f73ba34a1 to your computer and use it in GitHub Desktop.
Save goldfirere/a8c52c81e70407af7fd2b50f73ba34a1 to your computer and use it in GitHub Desktop.
Inferring constraints with polymorphic recursion
module EvalPolyRec where
import Prelude hiding (seq)
class Eval a where
seq :: a -> b -> b
instance Eval (a,b) where
seq (_, _) = id
data SP a b = SP !a !b
instance (Eval a, Eval b) => Eval (SP a b) where
seq (SP a b) = a `seq` b `seq` id
szip :: [a] -> [b] -> [SP a b]
szip (a : as) (b : bs) = SP a b : szip as bs
szip _ _ = []
f :: Eval a => [a] -> ()
f [] = ()
f (x : xs) = x `seq` f (zip xs xs)
g :: Eval a => [a] -> ()
g [] = ()
g (x : xs) = x `seq` f (szip xs xs)
@Icelandjack
Copy link

Not a commentary on the code, I'm just bored

class Eval a where
  seq :: a -> (forall x. x -> x)

instance Eval (a, b) where
  seq :: (a, b) -> (forall x. x -> x)
  seq (_, _) = id -- @x

...

-- @ is good for showing polymorphic recursion
f :: forall a. Eval a => [a] -> ()
f []     = ()
f (x:xs) = x `seq` f @(a, a) (zip xs xs)

g :: forall a. Eval a => [a] -> ()
g []     = ()
g (x:xs) = x `seq` f @(SP a a) (szip xs xs) 

Should that last f be a g?

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