Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

haskell - Incomplete type signature

Lets say we've got a function like f below, that returns a monad. However, where you see Int, pretend it's a really complicated type.

f :: (Monad m) => m Int -- Pretend this isn't Int but something complicated
f = return 42

Now lets say we want to force this into the Maybe monad. We don't need to write the full type of f to do this, we can just do the following:

g :: Maybe a -> Maybe a
g = id

main = print $ (g f)

The dummy function g forces f to become Maybe.

I think the above is rather messy. What I'd rather write is this:

main = print $ (f :: Maybe a)

But it fails with the following error:

Couldn't match expected type `a' against inferred type `Int'
  `a' is a rigid type variable bound by
      the polymorphic type `forall a. Maybe a' at prog.hs:7:16
  Expected type: Maybe a
  Inferred type: Maybe Int
In the second argument of `($)', namely `(f :: Maybe a)'
In the expression: print $ (f :: Maybe a)

Is there a way to do what g above does in a less messy way that doesn't involve creating a new function? I don't want to write f :: Maybe Int, as it becomes a maintenance problem if the return type changes. GHC extensions are okay in answers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use asTypeOf. It returns the first argument while unifying its type with that of the second. It's just a type-restricted version of const, but useful for situations like this.

main = print $ f `asTypeOf` (undefined :: Maybe a)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...