How to define function signatures partially in Haskell?

后端 未结 6 1185
刺人心
刺人心 2021-02-03 17:54

Starting point:

fn :: [a] -> Int
fn = (2 *) . length

Let\'s say we only want to constrain the return value, then we could write:

6条回答
  •  囚心锁ツ
    2021-02-03 18:16

    You're looking for feature that many of us would like, but that Haskell doesn't have. Nor ghc. You want a kind of partial type signatures. The suggested notation for this is

    fn :: [Char] -> _
    fn = (2*) . length
    

    Where the _ means "there's a type here, but I can't be bothered to write it out".

    It looks like a very easy feature to implement (instantiate _ with unification variables in the signature), but nobody has bothered to work out the semantic details and the interaction with other features.

提交回复
热议问题