【发布时间】:2015-02-01 15:11:20
【问题描述】:
以下文件编译失败:
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module AltMonad.Monoid where
import AltMonad.Category
import Prelude (curry)
class Category c
=> Monoid i p c m where
mid :: i `c` m
mcomb :: (m `p` m) `c` m
class Monoid () (,) (->) m
=> HaskellMonoid m where
empty :: m
append :: m -> m -> m
instance Monoid () (,) (->) m
=> HaskellMonoid m where
empty = mid ()
append = curry mcomb
它给出了错误:
[3 of 3] Compiling AltMonad.Monoid ( AltMonad\Monoid.hs, interpreted )
AltMonad\Monoid.hs:24:12:
Could not deduce (Monoid () p0 (->) m)
arising from a use of `mid'
from the context (Monoid () (,) (->) m)
bound by the instance declaration
at AltMonad\Monoid.hs:(22,10)-(23,24)
The type variable `p0' is ambiguous
Relevant bindings include
empty :: m (bound at AltMonad\Monoid.hs:24:3)
In the expression: mid ()
In an equation for `empty': empty = mid ()
In the instance declaration for `HaskellMonoid m'
AltMonad\Monoid.hs:25:18:
Could not deduce (Monoid i0 (,) (->) m)
arising from a use of `mcomb'
from the context (Monoid () (,) (->) m)
bound by the instance declaration
at AltMonad\Monoid.hs:(22,10)-(23,24)
The type variable `i0' is ambiguous
Relevant bindings include
append :: m -> m -> m (bound at AltMonad\Monoid.hs:25:3)
In the first argument of `curry', namely `mcomb'
In the expression: curry mcomb
In an equation for `append': append = curry mcomb
Failed, modules loaded: AltMonad.Category, AltMonad.Hask.
在我看来,我给出了正确的上下文,但我不知道如何让 GHC 相信这一点。
相关文件:
AltMonad.Category
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
module AltMonad.Category where
import AltMonad.Hask
class Category cat where
id :: cat a a
(.) :: b `cat` c -> a `cat` b -> a `cat` c
instance Category Hask where
id = \x -> x
g . f = \x -> g (f x)
AltMonad.Hask
{-# LANGUAGE NoImplicitPrelude #-}
module AltMonad.Hask where
type Hask = (->)
【问题讨论】:
-
This 有同样的问题。
标签: haskell types functional-programming ghc typeclass