[Haskell-beginners] Ambogous error in returning value

Baa aquagnu at gmail.com
Mon Sep 25 10:06:17 UTC 2017


Hello, everyone.

Considering, I have a class:

  class Flt a where
    allows :: FltOpts -> [a]
    denies :: FltOpts -> [a]
    crit :: a -> [a] -> Bool
    flt :: FltOpts -> a -> Bool
    flt opts a = allowed && not denied
      where allowed = if null $ allows opts then True else a `crit` (allows opts)
            denied = if null $ denies opts then False else a `crit` (denies opts)

I get error here:

     • Could not deduce (Flt a1) arising from a use of ‘allows’
       from the context: Flt a
         bound by the class declaration for ‘Flt’
         at .../.stack-work/intero/intero5319V42.hs:(31,1)-(38,97)
       The type variable ‘a1’ is ambiguous
       These potential instance exist:
         instance Flt MyType
           -- Defined at ...
     • In the second argument of ‘($)’, namely ‘allows opts’
       ....................................................

As I understand, GHC can not deduce type if it's a return's value
(contraposition?). OK, but it knows its type: it is `[a]`! What is the
problem to keep `flt` method as a generic, i.e. without concreate type,
but only `[a]` ?

Second, I implemented instance:

  instance Flt MyType where
    allows = ...
    denies = ...
    flt opts a = allowed && not denied
      where allowed = if null $ (allows opts::[MyType]) then True else a `crit` (allows opts)
            denied = if null $ (denies opts::[MyType]) then False else a `crit` (denies opts)

and without this explicite type annotation of `allows opts` I get again
ambigous error. But why? GHC knows that `allows` returns `[a]` and `a`
is `MyType`, so `[a]` is `[MyType]`. Why I need to write it explicitly?
May be I need some extension here?


===
Best regards, Paul


More information about the Beginners mailing list