Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Custom type classes (Guillaume Bouchard)
   2. Re:  Custom type classes (Theodore Lief Gannon)
   3. Re:  Increasing capabilities dramatically increases execution
      time (Elias Diem)
   4. Re:  Custom type classes (Imants Cekusins)
   5.  Ambigous type variable, why this error? (martin)
   6. Re:  Custom type classes (Guillaume Bouchard)
   7. Re:  Custom type classes (Imants Cekusins)


----------------------------------------------------------------------

Message: 1
Date: Wed, 27 Jan 2016 01:23:52 +0100
From: Guillaume Bouchard <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Custom type classes
Message-ID:
        <cagh0hccdnc3zw1gwxs91gyb2h2v3ykyy5kjr-no46rko6p+...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

May I suggest using FunctionalDependencies

https://wiki.haskell.org/Functional_dependencies

The class declaration is changed to

class Indexable idx a | idx -> a where
    first :: idx -> a

This just means that *a* is fully determined by idx (your tuple).

Hence, instead of using as suggested

first $ Tuple3 (1::Int) 'a' False::Int

You can simplify and let the inference do its magic and use:

first $ Tuple3 1 'a' False

On Mon, Jan 25, 2016 at 8:42 AM, Imants Cekusins <[email protected]> wrote:
> Hello Daniel,
>
> it works with these tweaks:
>
> -- begin
>
> {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
> module TupInst where
>
>
> data Tuple3 a b c = Tuple3 a b c deriving (Show)
>
> data Tuple2 a b = Tuple2 a b deriving (Show)
>
> class Indexable idx a where
>    first :: idx -> a
>
>
> instance Indexable (Tuple2 a b) a where
>    first (Tuple2 a0 b0) = a0
>
>
> instance Indexable (Tuple3 a b c) a where
>    first (Tuple3 a0 b0 c0) = a0
>
> -- end
>
> call it in ghci like this:
>
> first $ Tuple3 (1::Int) 'a' False::Int
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

Message: 2
Date: Tue, 26 Jan 2016 16:45:23 -0800
From: Theodore Lief Gannon <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Custom type classes
Message-ID:
        <CAJoPsuBvzX-d4=JQYFEBNM_Wj4kN8Hw_7c2hedboa=uzy+c...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

> class Indexable idx a | idx -> a where
>     first :: idx -> a
>
> This just means that *a* is fully determined by idx (your tuple).

Cutting in to say thanks -- I've had a vague notion of what fundeps do but
this really gelled it for me!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160126/97ce2211/attachment-0001.html>

------------------------------

Message: 3
Date: Wed, 27 Jan 2016 08:07:29 +0100
From: Elias Diem <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Increasing capabilities dramatically
        increases execution time
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed

Hi Thomas

On 2016-01-22, Thomas Koster wrote:

>Hi friends,
>
>I have encountered a situation in a concurrent program where I see an
>unexpected, dramatic increase in execution time when I add additional
>capabilities. On a multi-core CPU, "-N1" is fastest by an order of
>magnitude and the program increasingly slows for an increasing number
>of capabilities (still fewer than the number of real cores, of
>course).
>
><snip/>

Maybe post this to the caf? list at:

https://mail.haskell.org/mailman/listinfo/haskell-cafe

-- 
Greetings
Elias




------------------------------

Message: 4
Date: Wed, 27 Jan 2016 09:41:06 +0100
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Custom type classes
Message-ID:
        <CAP1qinYsS7LT_iwab=7emzwluu1mvvz5__cmtemsmshfndj...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Thank you Guillaume

fun deps are new for me too.

quoting from the above wiki link:


-- Read as: "container" type determines "elem" type.
class Extract container elem | container -> elem where
  extract :: container -> elem
The functional dependency "container -> elem" promises that we won't
declare multiple instances with the same "container" type.



Without the functional dependency, both instances above would be
allowed, and the type of v would be potentially ambiguous. Even if
only one instance is defined, the type system will not figure it out
without the functional dependency.


------------------------------

Message: 5
Date: Wed, 27 Jan 2016 09:44:07 +0100
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Ambigous type variable, why this error?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Hello all,

here is something where I don't understand the second error:

*Main> (Open [1,2,3]) <: (Open ([1,2,4]))

<interactive>:94:8:
    No instance for (Num a0) arising from the literal ?1?
    The type variable ?a0? is ambiguous
    Note: there are several potential instances:
      instance Num Double -- Defined in ?GHC.Float?
      instance Num Float -- Defined in ?GHC.Float?
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in ?GHC.Real?
      ...plus 46 others
    In the expression: 1
    In the first argument of ?Open?, namely ?[1, 2, 3]?
    In the first argument of ?(<:)?, namely ?(Open [1, 2, 3])?

Okay, I understand this one, but why this:

<interactive>:94:16:
    No instance for (Poset a0) arising from a use of ?<:?
    The type variable ?a0? is ambiguous
    Note: there are several potential instances:
      instance (Eq a, Ord a, Poset a) => Poset (Crust a)    -- <== yes, yes, 
yes, take this one
        -- Defined at 
/home/martin/projects/haskell/currychicken/opal/Poset.hs:83:10
      instance (Eq a, Ord a, Poset a) => Poset (PsSet a)
        -- Defined at 
/home/martin/projects/haskell/currychicken/opal/Poset.hs:50:10
      instance (Eq a, Ord a, Poset a) => Poset (PsList a)
        -- Defined at 
/home/martin/projects/haskell/currychicken/opal/Poset.hs:46:10
      ...plus one other
    In the expression: (Open [1, 2, 3]) <: (Open ([1, 2, 4]))
    In an equation for ?it?:
        it = (Open [1, 2, 3]) <: (Open ([1, 2, 4]))

The operands of (<:) are clearly Crusts, so (PsSet a) or (PsList a) shouldn't 
be options

*Main> :t Open [1,2,3]
Open [1,2,3] :: Num a => Crust a
*Main>

The problem goes away, when I make sure my list elements are Ints

*Main> (Open [1::Int,2,3]) <: (Open ([1,2,4]))
False

But why do I see the second error at all?


Here is the complete code:

{-# Language FlexibleInstances #-}
{-# Language UndecidableInstances #-}

import qualified Data.List as L
import qualified Data.Set as S
import Debug.Trace
import Test.QuickCheck hiding ((==>))

------------------------------------------------------------
class Poset p where
------------------------------------------------------------
        (<:) :: p -> p -> Bool

instance Poset Int where (<:) = (==)

------------------------------------------------------------
-- Alternatives
------------------------------------------------------------
newtype PsList a = PsList [a]
newtype PsSet  a = PsSet (S.Set a)

isSubPolist :: (Poset a) => [a] -> [a] ->Bool
isSubPolist as bs = all includedInBs as
        where
            includedInBs a = any (a <:) bs

instance (Eq a, Ord a, Poset a) => Poset (PsList a)
        where
            (PsList as) <: (PsList bs) = isSubPolist as bs

instance (Eq a, Ord a, Poset a) => Poset (PsSet a)
        where
            (PsSet as) <: (PsSet bs) = isSubPolist (S.toList as) (S.toList bs)


------------------------------------------------------------
data Crust a = Open [a] | Closed [a]
------------------------------------------------------------
             deriving (Eq, Ord, Show)

instance (Eq a, Ord a, Poset a) => Poset (Crust a)
        where
            (<:) (Open as) (Closed bs)   = False
            (<:) (Closed as) (Closed bs) = as == bs

            (<:) (Open _) (Open [])         =  True
            (<:) (Open []) (Open _)         =  False
            (<:) (Open (x:xs)) (Open (y:ys)) = x <: y &&
                                               (Open xs) <: (Open ys)

            (<:) (Closed _) (Open [])   = True
            (<:) (Closed []) (Open _)   = False
            (<:) (Closed (x:xs)) (Open (y:ys))  = x <: y &&
                                                  (Closed xs) <: (Open ys)




------------------------------

Message: 6
Date: Wed, 27 Jan 2016 10:17:47 +0100
From: Guillaume Bouchard <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Custom type classes
Message-ID:
        <CAGh0HCB06bE5uNgKNcwHCiJvJvioJSu4A4euOpyiMJ=3ubd...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Wed, Jan 27, 2016 at 9:41 AM, Imants Cekusins <[email protected]> wrote:
> Thank you Guillaume
>
> fun deps are new for me too.
>
> quoting from the above wiki link:
>
>
> -- Read as: "container" type determines "elem" type.
> class Extract container elem | container -> elem where
>   extract :: container -> elem
> The functional dependency "container -> elem" promises that we won't
> declare multiple instances with the same "container" type.
>
>
>
> Without the functional dependency, both instances above would be
> allowed, and the type of v would be potentially ambiguous. Even if
> only one instance is defined, the type system will not figure it out
> without the functional dependency.

At first this is weird because we have the feeling that  `instance
Indexable (Tuple2 a b) a` fully qualifie the second type "a" as
equivalent to the first subtype "a" of Tuple2. This is True for this
instance, but the typechecker does not try to find one instance which
match, it tries to find if , knowing the class definition, it is
possible to be sure that there will only be one instance matching, and
this is not the case because someone can easily define `instance
Indexable (Tuple2 a b) String`.

That's something I really like about this mecanism, is that adding new
instances later cannot change the behavior of previous one.

[A bit of digression]

Actually, I don't know why, but at first though I always think in the
wrong direction when reasoning about types. FunctionalDependencies is
one example, but I had the same issue when I tried to understand why
`fmap` cannot work on unboxed Vector. When reading the types, `fmap ::
Functor f => (a -> b) -> f a -> f b`, I was understanding that it
accepts any `a` as input. It was working on `Vector a`, but not on
`Unbox a => Vector a` which appears more constrained, so if `fmap` was
accepting any `a` as argument, it will certainly accept an `Unbox a`
which is more constrained. But actually it works the opposite, `fmap`
types means that `a` should be anything, and not something
constrained.


------------------------------

Message: 7
Date: Wed, 27 Jan 2016 11:11:22 +0100
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Custom type classes
Message-ID:
        <CAP1qinY2QUV3LvbX8BAvA9XYTS=KPoY4FO=ykqgnsd2wahf...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

> `fmap` types mean that `a` should be anything, and not something
constrained.

just wondering: is it something specific to Functor class or does this
hold for any class declaration:

(a -> b)
is not the same as
... a => (a -> b)


in other words, if class expects (a -> b) with any a, instance must
not constrain a.

as opposed to

... a => a -> b
which seems ok


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 91, Issue 32
*****************************************

Reply via email to