Determining the length of a Foldable Applicative.

Hi all, I thought I had a simple way to determine the “length" (i.e. - number of elements in) of a Foldable Applicative container: import Prelude hiding (sum) import Data.Foldable (Foldable(..), sum) import Control.Applicative -- Calculate the "length" (i.e. - number of elements in) an Applicative container. app_len :: (Applicative f, Foldable f) => f a -> Int app_len = sum $ pure 1 but I didn’t: app_len_test.hs:9:11: Could not deduce (Foldable t0) arising from a use of ‘sum’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Foldable ((,) a) -- Defined in ‘Data.Foldable’ instance GHC.Arr.Ix i => Foldable (GHC.Arr.Array i) -- Defined in ‘Data.Foldable’ instance Foldable (Const m) -- Defined in ‘Data.Foldable’ ...plus four others In the expression: sum In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1 app_len_test.hs:9:17: Could not deduce (Applicative t0) arising from a use of ‘pure’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Data.Monoid.Monoid a => Applicative ((,) a) -- Defined in ‘Control.Applicative’ instance Applicative ((->) a) -- Defined in ‘Control.Applicative’ instance Control.Arrow.Arrow a => Applicative (Control.Arrow.ArrowMonad a) -- Defined in ‘Control.Applicative’ ...plus 14 others In the second argument of ‘($)’, namely ‘pure 1’ In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1 Can anyone help me understand what I’m missing? Thanks, and have a great weekend, -db

Maybe what you are missing is an argument to the app_len function? Am Freitag, 30. Oktober 2015 schrieb David Banas :
Hi all,
I thought I had a simple way to determine the “length" (i.e. - number of elements in) of a Foldable Applicative container:
import Prelude hiding (sum) import Data.Foldable (Foldable(..), sum) import Control.Applicative
-- Calculate the "length" (i.e. - number of elements in) an Applicative container. app_len :: (Applicative f, Foldable f) => f a -> Int app_len = sum $ pure 1
but I didn’t:
app_len_test.hs:9:11: Could not deduce (Foldable t0) arising from a use of ‘sum’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Foldable ((,) a) -- Defined in ‘Data.Foldable’ instance GHC.Arr.Ix i => Foldable (GHC.Arr.Array i) -- Defined in ‘Data.Foldable’ instance Foldable (Const m) -- Defined in ‘Data.Foldable’ ...plus four others In the expression: sum In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1
app_len_test.hs:9:17: Could not deduce (Applicative t0) arising from a use of ‘pure’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Data.Monoid.Monoid a => Applicative ((,) a) -- Defined in ‘Control.Applicative’ instance Applicative ((->) a) -- Defined in ‘Control.Applicative’ instance Control.Arrow.Arrow a => Applicative (Control.Arrow.ArrowMonad a) -- Defined in ‘Control.Applicative’ ...plus 14 others In the second argument of ‘($)’, namely ‘pure 1’ In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1
Can anyone help me understand what I’m missing?
Thanks, and have a great weekend, -db
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org javascript:; http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

You don't need the Applicative context. In fact, in the latest version
of the library, length is a member of the Foldable class, but even for
older versions, you can define
length :: Foldable f => f a -> Int
Does limiting yourself that way help guide you to a correct solution?
On Fri, Oct 30, 2015 at 4:03 PM, David Banas
Hi all,
I thought I had a simple way to determine the “length" (i.e. - number of elements in) of a Foldable Applicative container:
import Prelude hiding (sum) import Data.Foldable (Foldable(..), sum) import Control.Applicative
-- Calculate the "length" (i.e. - number of elements in) an Applicative container. app_len :: (Applicative f, Foldable f) => f a -> Int app_len = sum $ pure 1
but I didn’t:
app_len_test.hs:9:11: Could not deduce (Foldable t0) arising from a use of ‘sum’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Foldable ((,) a) -- Defined in ‘Data.Foldable’ instance GHC.Arr.Ix i => Foldable (GHC.Arr.Array i) -- Defined in ‘Data.Foldable’ instance Foldable (Const m) -- Defined in ‘Data.Foldable’ ...plus four others In the expression: sum In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1
app_len_test.hs:9:17: Could not deduce (Applicative t0) arising from a use of ‘pure’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Data.Monoid.Monoid a => Applicative ((,) a) -- Defined in ‘Control.Applicative’ instance Applicative ((->) a) -- Defined in ‘Control.Applicative’ instance Control.Arrow.Arrow a => Applicative (Control.Arrow.ArrowMonad a) -- Defined in ‘Control.Applicative’ ...plus 14 others In the second argument of ‘($)’, namely ‘pure 1’ In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1
Can anyone help me understand what I’m missing?
Thanks, and have a great weekend, -db
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Your types don't match here, but you have the right idea. 'sum' won't be enough. Take a look at the definition of sum:
sum :: (Foldable t, Num a) => t a -> a sum = getSum . foldMap Sum
That won't work, since that requires '(Num a)'. We just want to count 1 up whenever we map an element. Try doing something with const and Sum. (Forgot to reply to list) On 10/30/2015 9:03 PM, David Banas wrote:
Hi all,
I thought I had a simple way to determine the “length" (i.e. - number of elements in) of a Foldable Applicative container:
import Prelude hiding (sum) import Data.Foldable (Foldable(..), sum) import Control.Applicative
-- Calculate the "length" (i.e. - number of elements in) an Applicative container. app_len :: (Applicative f, Foldable f) => f a -> Int app_len = sum $ pure 1
but I didn’t:
app_len_test.hs:9:11: Could not deduce (Foldable t0) arising from a use of ‘sum’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Foldable ((,) a) -- Defined in ‘Data.Foldable’ instance GHC.Arr.Ix i => Foldable (GHC.Arr.Array i) -- Defined in ‘Data.Foldable’ instance Foldable (Const m) -- Defined in ‘Data.Foldable’ ...plus four others In the expression: sum In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1
app_len_test.hs:9:17: Could not deduce (Applicative t0) arising from a use of ‘pure’ from the context (Applicative f, Foldable f) bound by the type signature for app_len :: (Applicative f, Foldable f) => f a -> Int at app_len_test.hs:8:12-52 The type variable ‘t0’ is ambiguous Note: there are several potential instances: instance Data.Monoid.Monoid a => Applicative ((,) a) -- Defined in ‘Control.Applicative’ instance Applicative ((->) a) -- Defined in ‘Control.Applicative’ instance Control.Arrow.Arrow a => Applicative (Control.Arrow.ArrowMonad a) -- Defined in ‘Control.Applicative’ ...plus 14 others In the second argument of ‘($)’, namely ‘pure 1’ In the expression: sum $ pure 1 In an equation for ‘app_len’: app_len = sum $ pure 1
Can anyone help me understand what I’m missing?
Thanks, and have a great weekend, -db
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (4)
-
David Banas
-
David Feuer
-
David Kraeutmann
-
Janis Voigtländer