``````class Ped a where
prize' :: Fractional b => a -> b

instance (Integral n, Fractional p) => Ped (Order n p) where
prize' x = prizeOrder x

data (Integral c,Fractional p) => Product c p
= Prod c String p

data (Integral n, Fractional p) => Order n p
= PdMult (Product n p) n
| PdUnit (Product n p)
deriving (Show, Eq)

prize :: (Fractional p, Integral c) =>(Product c p) -> p
prize (Prod _ _ x) = x

prizeOrder :: (Fractional p, Integral c) => (Order c p) -> p
prizeOrder (PdMult p n) = (prize p) * (fromIntegral n)
prizeOrder (PdUnit p) = prize p
``````

Prelude说：

``````Could not deduce (p ~ b)
from the context (Integral n, Fractional p)
bound by the instance declaration
at src\Funciones.hs:6:10-55
or from (Fractional b)
bound by the type signature for
prize' :: Fractional b => Order n p -> b
at src\Funciones.hs:7:5-11
`p' is a rigid type variable bound by
the instance declaration
at src\Funciones.hs:6:10
`b' is a rigid type variable bound by
the type signature for prize' :: Fractional b => Order n p -> b
at src\Funciones.hs:7:5
Expected type: Order b
Actual type: Order n p
Relevant bindings include
x :: Order n p
(bound at src\Funciones.hs:7:13)
prize' :: Order n p -> b
(bound at src\Funciones.hs:7:5)
In the first argument of `prizeOrder', namely `x'
In the expression: prizeOrder x
``````

#### 1 个答案:

``````class Ped a where
prize' :: Fractional b => a -> b
``````

``````instance (Integral n, Fractional p) => Ped (Order n p) where
prize' x = prizeOrder x
``````

``````prizeOrder :: (Fractional p, Integral c) => (Order c p) -> p
``````

``````class Ped a b where
prize' :: a -> b
``````

``````instance (Integral n, Fractional p) => Ped (Order n p) p where
prize' x = prizeOrder x
``````

``````{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DatatypeContexts #-}

class Ped a b where
prize' :: a -> b

instance (Integral n, Fractional p) => Ped (Order n p) p where
prize' x = prizeOrder x

data (Integral c,Fractional p) => Product c p
= Prod c String p

data (Integral n, Fractional p) => Order n p
= PdMult (Product n p) n
| PdUnit (Product n p)
deriving (Show)

prize :: (Fractional p, Integral c) =>(Product c p) -> p
prize (Prod _ _ x) = x

prizeOrder :: (Fractional p, Integral c) => (Order c p) -> p
prizeOrder (PdMult p n) = (prize p) * (fromIntegral n)
prizeOrder (PdUnit p) = prize p
``````

0条回复