Are there “type-level combinators”? Will they exist in some future?

前端 未结 3 860
不思量自难忘°
不思量自难忘° 2020-12-31 03:47

Much of what makes haskell really nice to use in my opinion are combinators such as (.), flip, $ <*> and etc. It fe

相关标签:
3条回答
  • 2020-12-31 04:15

    You can do the following, but I don't think its actually very useful, since you still can't really partially apply it:

    {-# LANGUAGE TypeFamilies, FlexibleInstances #-}
    module Main where
    
    class TFlip a where
        type FlipT a
    
    instance TFlip (f a b) where
        type FlipT (f a b) = f b a 
    
    -- *Main> :t (undefined :: FlipT (Either String Int))
    -- (undefined :: FlipT (Either String Int)) :: Either Int [Char]
    

    Also see this previous discussion: Lambda for type expressions in Haskell?

    0 讨论(0)
  • 2020-12-31 04:29

    I'm writing answer here just for clarifying things and to tell about achievements in the last years. There're a lot of features in Haskell and now you can write some operators in type. Using $ you can write something like this:

    foo :: Int -> Either String $ Maybe $ Maybe Int
    

    to avoid parenthesis instead of good old

    foo :: Int -> Either String (Maybe (Maybe Int))
    
    0 讨论(0)
  • 2020-12-31 04:30

    Your question makes sense, but the answer is: no, it's not currently possible.

    The problem is that (in GHC Haskell's type system) you can't have lambdas at the type level. For anything you might try that looks like it could emulate or achieve the effect of a type level lambda, you will discover that it doesn't work. (I know, because I did.)

    What you can do is declare your Flip newtypes, and then write instances of the classes you want for them, painfully with the wrapping and the unwrapping (by the way: use record syntax), and then clients of the classes can use the newtypes in type signatures and not have to worry about the details.

    I'm not a type theorist and I don't know the details of why exactly we can't have type level lambdas. I think it was something to do with type inference becoming impossible, but again, I don't really know.

    0 讨论(0)
提交回复
热议问题