简体   繁体   中英

Specify context from where comes term in Haskell

Here is a dummy example:

class Test a b where
  witness :: a

f :: Test a b => a
f = witness

Haskell then say

Could not deduce (Test a b0) arising from a use of ‘witness’
from the context (Test a b)
  bound by the type signature for f :: Test a b => a
  at test.hs:8:6-18
The type variable ‘b0’ is ambiguous
Relevant bindings include f :: a (bound at test.hs:9:1)
In the expression: witness
In an equation for ‘f’: f = witness

Error comes from the fact that Haskell cannot infer type variable b0 and a solution would be to remove parameter b from definition of the typeclass Test . But, in reality, I cannot.

My question is : Does it exist a way to explicity identify b0 with explicit parameter b given in line f :: Test ab => a ?

Thanks.

Fleshing out Joachim Breitner's suggestion , you could use witness if you can alter its type signature to be proxy b -> a argument or Constant ab .

The two approaches are mostly equivalent, so it's a matter of preference:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
module SO33958506 where

import Data.Functor.Constant
import Data.Proxy

class Test a b where
  constantWitness :: Constant a b
  proxyWitness :: proxy b -> a

constantWitnessWithProxy :: forall proxy a b. Test a b => proxy b -> a
constantWitnessWithProxy _ = getConstant $ (constantWitness :: Constant a b)

proxyWitnessAsConstant :: forall a b. Test a b => Constant a b
proxyWitnessAsConstant = Constant $ proxyWitness (Proxy :: Proxy b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM