-
-
Save guibou/c0bbe73a5fb70c54694785e09c4bb706 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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