0

I have the following custom prelude:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}


module Prelude (
  module ClassyPrelude
, module Numeric.Units.Dimensional.Prelude
, headMay
, sec
) where

import ClassyPrelude hiding
    ( (+), (-), (*), (/), (^), (**)
    , abs, signum, negate, recip, pi, exp, log, logBase, sqrt
    , sin, cos, tan, asin, acos, atan, atan2
    , sinh, cosh, tanh, asinh, acosh, atanh
    , sum, product, minimum, maximum
    ) -- Hide definitions overridden by 'Numeric.Dimensional'.

import Numeric.Units.Dimensional.Prelude hiding (
    (++)
  , all,and, any, break, concat, concatMap, drop, dropWhile, elem, filter, foldMap, foldr
  , getChar, getContents, getLine, head, init, interact, lookup, map, mapM_, notElem, null, or, print, putChar
  , putStr, putStrLn, readFile, replicate, sequence_, splitAt, undefined, writeFile
  , last, length, lines, reverse, second, span, tail, take, takeWhile, unlines, unwords
  , unzip, unzip3, words, zip, zip3, zipWith, zipWith3
  ) -- hide other conflicts specific to classy prelude

import qualified Numeric.Units.Dimensional.Prelude as NUDP

headMay :: Foldable f => f a -> Maybe a
headMay = foldr (const . Just) Nothing

sec :: Num a => Unit Metric DTime a
sec = NUDP.second

The relevant bit here is just (I think) Classy Prelude and the definition of headMay:

headMay :: Foldable f => f a -> Maybe a
headMay = foldr (const . Just) Nothing

The error I get when trying to compile this is:

/prelude/Prelude.hs:32:18: error:
    • Couldn't match type ‘a’ with ‘Element (f a)’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          Prelude.headMay :: forall (f :: * -> *) a.
                             Foldable f =>
                             f a -> Maybe a
        at prelude/Prelude.hs:31:1-39
      Expected type: Element (f a) -> Maybe a -> Maybe a
        Actual type: a -> Maybe a -> Maybe a
    • In the first argument of ‘foldr’, namely ‘(const . Just)’
      In the expression: foldr (const . Just) Nothing
      In an equation for ‘Prelude.headMay’:
          Prelude.headMay = foldr (const . Just) Nothing
    • Relevant bindings include
        headMay :: f a -> Maybe a (bound at prelude/Prelude.hs:32:1)
   |
32 | headMay = foldr (const . Just) Nothing
   |                  ^^^^^^^^^^^^
bbarker
  • 11,636
  • 9
  • 38
  • 62

1 Answers1

3

classy-prelude exports a MonoFoldable version of foldr, through the reexport of Data.MonoTraversable.Unprefixed:

https://hackage.haskell.org/package/mono-traversable-1.0.9.0/docs/Data-MonoTraversable-Unprefixed.html

Li-yao Xia
  • 31,896
  • 2
  • 33
  • 56
  • I had just gone to the haddocks and saw a non-linked foldr, which I assumed it meant the "standard" one was used, but I now realize it could potentially mean a re-export from anywhere: https://hackage.haskell.org/package/classy-prelude-1.5.0/docs/doc-index-F.html Is there a way to chase through the haddocks to find this on hackage or did you have to go to the sources? – bbarker Nov 19 '19 at 01:00
  • I chased through the haddocks, but my starting point was that this was the only possible explanation for your error message. A more efficient way for you, since you are already in the right environment, would be to use ghci, `:t foldr`, `:info foldr`. – Li-yao Xia Nov 19 '19 at 01:38
  • ordinarily yes, but looks like I was hit by this: https://gitlab.haskell.org/ghc/ghc/issues/10920 when running `stack --nix ghci` – bbarker Nov 19 '19 at 13:45
  • 1
    Oh, that's nasty. In any case, haddock has some room for improvement here. Here's the relevant issue: https://github.com/haskell/haddock/issues/682 – Li-yao Xia Nov 19 '19 at 14:24