Skip to content

Instantly share code, notes, and snippets.

@guibou
Forked from esoeylemez/NumberDispatch.hs
Last active October 17, 2016 08:33
Show Gist options
  • Save guibou/c0bbe73a5fb70c54694785e09c4bb706 to your computer and use it in GitHub Desktop.
Save guibou/c0bbe73a5fb70c54694785e09c4bb706 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
module NumberDispath where
data Number
= DoubleNum !Double
| IntegerNum !Integer
| IntNum !Int
| RationalNum !Rational
withNumber :: (c Double, c Int, c Integer, c Rational) => (forall a. (c a) => a -> r) -> Number -> r
withNumber f (DoubleNum x) = f x
withNumber f (IntegerNum x) = f x
withNumber f (IntNum x) = f x
withNumber f (RationalNum x) = f x
-- that one works (compiles and runs)
a = withNumber' @Show show (RationalNum 3)
-- that one does not compiles
b = withNumber' @Integral toInteger (IntegerNum 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment