Last active
December 19, 2015 00:09
-
-
Save nushio3/5867066 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Data.Typeable | |
import Test.Hspec | |
arityOf :: Typeable a => a -> Int | |
arityOf x = go $ typeOf x | |
where | |
go tr | |
| isFun $ typeRepTyCon tr = 1 + go (last $ snd $ splitTyConApp tr) | |
| otherwise = 0 | |
funTyCon = typeRepTyCon $ typeOf ((1+):: Int -> Int) | |
isFun = (funTyCon ==) | |
main :: IO () | |
main = hspec spec | |
func :: (Int -> Int) -> Int -> Int | |
func = undefined | |
spec :: Spec | |
spec = describe "arityOf" $ do | |
it "evaluates Integers correctly" $ arityOf (1::Int) `shouldBe` 0 | |
it "evaluates Strings correctly" $ arityOf "(1::Int)" `shouldBe` 0 | |
it "evaluates monads correctly" $ arityOf main `shouldBe` 0 | |
it "evaluates multiplications correctly" $ arityOf ((*) :: Int -> Int -> Int) | |
`shouldBe` 2 | |
it "is not deceived by non-tail argument" $ arityOf func `shouldBe` 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment