Are type synonyms with typeclass constraints possible?

后端 未结 2 964
囚心锁ツ
囚心锁ツ 2021-01-19 00:18

Feel free to change the title, I\'m just not experienced enough to know what\'s really going on.

So, I was writing a program loosely based on this, and wrote this (a

2条回答
  •  心在旅途
    2021-01-19 01:10

    Constraints on a type variable can not be part of any Haskell type signature.

    This may seem a bit of a ridiculous statement: "what's (==) :: Eq a => a -> a -> a then?"

    The answer is that a doesn't really exist, in much the same way there is not really an x in the definition f x = x * log x. You've sure enough used the x symbol in defining that function, but really it was just a local tool used in the lambda-abstraction. There is absolutely no way to access this symbol from the outside, indeed it's not required that the compiler even generates anything corresponding to x in the machine code – it might just get optimised away.

    Indeed, any polymorphic signature can basically be read as a lambda expression accepting a type variable; various writing styles:

    (==) :: forall a . Eq a => a -> a -> a
    (==) :: ∀ a . Eq a => a -> a -> a
    (==) :: Λa. {Eq a} -> a -> a -> a
    

    This is called System F.

    Note that there is not really a "constraint" in this signature, but an extra argument: the Eq-class dictionary.

提交回复
热议问题