コード
下記のように自作のMaybeぽいものを作ってみました。
data MayNull a = Have a | Null deriving Show
incMayNull (Have x) = x + 1
incMayNull Null = Null
v = Have 1
foo = incMayNull v
main = do print foo
実行結果
実行すると下記エラーです。 Have
の中から値を取り出し +1
できる関数を定義できていると思ったのですが、なにがいけないんでしょうか?
% docker run -it --rm --name bar-haskell -v "$PWD":/tmp -w /tmp haskell:8 runghc Bar.hs
Bar.hs:3:1: error:
* Non type-variable argument in the constraint: Num (MayNull a)
(Use FlexibleContexts to permit this)
* When checking the inferred type
incMayNull :: forall a.
Num (MayNull a) =>
MayNull (MayNull a) -> MayNull a
|
3 | incMayNull (Have x) = x + 1
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^...
Bar.hs:9:11: error:
* Ambiguous type variable `a0' arising from a use of `print'
prevents the constraint `(Show a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
instance Show Ordering -- Defined in `GHC.Show'
instance Show Integer -- Defined in `GHC.Show'
instance Show a => Show (MayNull a) -- Defined at Bar.hs:1:41
...plus 23 others
...plus 12 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In a stmt of a 'do' block: print foo
In the expression: do print foo
In an equation for `main': main = do print foo
|
9 | main = do print foo
| ^^^^^^^^^