Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  Type inference in ST monad? (Brandon Allbery)
   2. Re:  Type inference in ST monad? (Aurimas)
   3.  multi-parameter typeclass with default   implementation (TP)
   4. Re:  multi-parameter typeclass with default       implementation
      (Ben Gamari)
   5. Re:  How Haskell Fits Into an Operating System / API
      Environment (Magnus Therning)
   6. Re:  multi-parameter typeclass with default       implementation (TP)
   7. Re:  multi-parameter typeclass with default       implementation (TP)


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

Message: 1
Date: Mon, 19 Aug 2013 09:59:28 -0400
From: Brandon Allbery <allber...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Type inference in ST monad?
Message-ID:
        <cakfcl4uavyyq3idjsqx_wa2jmmckcuv9bexehh41yjxom7p...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, Aug 19, 2013 at 1:36 AM, Aurimas <aurimas.anskai...@vgtu.lt> wrote:

> Thanks, this is clearly the most easy way to fix the problem. But the
> question remains - why the line
> "do r1 <- uniformR (-1.0, 1.0 :: Double) gen"    cannot be annotated with
> ST s Double?
>

Maybe you need ScopedTypeVariables? But I'd be suspicious that, given the
forall in the definition of ST, that such a type annotation *always*
creates a new `s` and some other way to "force" the type is needed.

-- 
brandon s allbery kf8nh                               sine nomine associates
allber...@gmail.com                                  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130819/dbf6a21d/attachment-0001.html>

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

Message: 2
Date: Mon, 19 Aug 2013 17:12:27 +0300
From: Aurimas <aurimas.anskai...@vgtu.lt>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Type inference in ST monad?
Message-ID: <521227cb.30...@vgtu.lt>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

On 08/19/2013 04:59 PM, Brandon Allbery wrote:
> On Mon, Aug 19, 2013 at 1:36 AM, Aurimas <aurimas.anskai...@vgtu.lt 
> <mailto:aurimas.anskai...@vgtu.lt>> wrote:
>
>     Thanks, this is clearly the most easy way to fix the problem. But
>     the question remains - why the line
>     "do r1 <- uniformR (-1.0, 1.0 :: Double) gen"  cannot be annotated
>     with ST s Double?
>
>
> Maybe you need ScopedTypeVariables? But I'd be suspicious that, given 
> the forall in the definition of ST, that such a type annotation 
> *always* creates a new `s` and some other way to "force" the type is 
> needed.
>
ScopedTypeVariables does not solve the problem. As you noticed, new s is 
created and runST is not happy about this. It is
strange that this type can be "forced" replacing given line by a 
function returning ST s a.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130819/c4d784c1/attachment-0001.html>

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

Message: 3
Date: Mon, 19 Aug 2013 22:28:14 +0200
From: TP <paratribulati...@free.fr>
To: beginners@haskell.org
Subject: [Haskell-beginners] multi-parameter typeclass with default
        implementation
Message-ID: <unbaea-una.ln1@rama.universe>
Content-Type: text/plain; charset="ISO-8859-1"

Hi,

I struggle with a dummy example using a multi-parameter typeclass containing 
a default implementation for a function:

---------------------------------------
{-# LANGUAGE MultiParamTypeClasses #-}

class Foo a b where

    bar :: a -> Int

    foobar :: a -> b -> Int
    foobar avalue bvalue = bar avalue

instance Foo Int Int where

    bar i = 5

main = do

print $ bar (4::Int)
---------------------------------------

I obtain the following errors. I have tried various things without any 
success.
Any help appreciated!

Thanks

TP


PS: The errors:

$ runghc test.hs
test.hs:8:28:
    Could not deduce (Foo a b1) arising from a use of `bar'
    from the context (Foo a b)
      bound by the class declaration for `Foo' at test.hs:(3,1)-(8,37)
    The type variable `b1' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    In the expression: bar avalue
    In an equation for `foobar': foobar avalue bvalue = bar avalue
test.hs:16:9:
    No instance for (Foo Int b0) arising from a use of `bar'
    The type variable `b0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there is a potential instance available:
      instance Foo Int Int -- Defined at test.hs:10:10
    Possible fix: add an instance declaration for (Foo Int b0)
    In the second argument of `($)', namely `bar (4 :: Int)'
    In a stmt of a 'do' block: print $ bar (4 :: Int)
    In the expression: do { print $ bar (4 :: Int) }




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

Message: 4
Date: Mon, 19 Aug 2013 16:59:42 -0400
From: Ben Gamari <bgamari.f...@gmail.com>
To: TP <paratribulati...@free.fr>, beginners@haskell.org
Subject: Re: [Haskell-beginners] multi-parameter typeclass with
        default implementation
Message-ID: <87haeltn0h....@gmail.com>
Content-Type: text/plain; charset="us-ascii"

TP <paratribulati...@free.fr> writes:

> Hi,
>
> I struggle with a dummy example using a multi-parameter typeclass containing 
> a default implementation for a function:
>
> ---------------------------------------
> {-# LANGUAGE MultiParamTypeClasses #-}
>
> class Foo a b where
>
>     bar :: a -> Int
>
The problem is in this declaration, which does not mention the type
"b". This makes it impossible for the compiler to infer which instance
to use when "bar" is used. This is what the compiler is trying to tell
you when it says "The type variable `b1' is ambiguous".

As far as I know, you'd need to do something like this to accomplish
what you are after,

    {-# LANGUAGE MultiParamTypeClasses, DefaultSignatures #-}
    
    class Bar a where
        bar :: a -> Int
    
    class FooBar a b where
        foobar :: a -> b -> Int
        default foobar :: Bar a => a -> b -> Int
        foobar avalue bvalue = bar avalue
    
    instance Bar Int where
        bar i = 5
    instance FooBar Int Int
    
    main = do
        print $ bar (4::Int)
        print $ foobar (5::Int) (2::Int)
        
                                
Cheers,

- Ben
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 489 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130819/11e10321/attachment-0001.sig>

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

Message: 5
Date: Tue, 20 Aug 2013 10:11:39 +0200
From: Magnus Therning <mag...@therning.org>
To: Henk-Jan van Tuyl <hjgt...@chello.nl>
Cc: beginners@haskell.org
Subject: Re: [Haskell-beginners] How Haskell Fits Into an Operating
        System / API Environment
Message-ID: <20130820081139.ga8...@mteis.semcon.se>
Content-Type: text/plain; charset="us-ascii"

On Mon, Aug 19, 2013 at 12:52:20PM +0200, Henk-Jan van Tuyl wrote:
> On Mon, 19 Aug 2013 10:18:11 +0200, Magnus Therning
> <mag...@therning.org> wrote:
> 
> >There are also a few articles on Erlang, and I believe there's good
> >reason to believe the results with that language can be transferred to
> >other "languages without assignment".
> >
> >- Productivity gains with Erlarng
> >  http://dl.acm.org/citation.cfm?id=1362710
> >  slides: http://is.gd/BgGZyh
> >  (behind a paywall, I haven't found a freely available copy)
> >
> 
> For free:
>   "Four-fold Increase in Productivity and Quality"
>   Ulf Wiger, FemSYS 2001, Munich
>   http://www.erlang.se/publications/Ulf_Wiger.pdf

Thanks!  I was trying to find that paper yesterday, but apparently my
google-fu isn't as strong as yours ;)

/M

-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4 
email: mag...@therning.org   jabber: mag...@therning.org
twitter: magthe               http://therning.org/magnus

Most software today is very much like an Egyptian pyramid with
millions of bricks piled on top of each other, with no structural
integrity, but just done by brute force and thousands of slaves.
     -- Alan Kay
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 230 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130820/0df22a06/attachment-0001.sig>

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

Message: 6
Date: Tue, 20 Aug 2013 11:44:49 +0200
From: TP <paratribulati...@free.fr>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] multi-parameter typeclass with
        default implementation
Message-ID: <hdqbea-evi.ln1@rama.universe>
Content-Type: text/plain; charset="UTF-8"

Ben Gamari wrote:

>> {-# LANGUAGE MultiParamTypeClasses #-}
>>
>> class Foo a b where
>>
>>     bar :: a -> Int
>>
> The problem is in this declaration, which does not mention the type
> "b". This makes it impossible for the compiler to infer which instance
> to use when "bar" is used. This is what the compiler is trying to tell
> you when it says "The type variable `b1' is ambiguous".

Thanks, this works perfectly. Yet, to try to improve myself, I would like to 
discuss a bit on the text of the two obtained errors in my example (correct 
me if I am wrong below).

"""
test.hs:16:9:
    No instance for (Foo Int b0) arising from a use of `bar'
"""

The second error is clear: indeed by calling `bar` on `4::Int`, we don't 
indicate the type of b allowing to choose the right typeclass for the 
implementation of the `bar` function. The fact there is only one typeclass 
instance with Int as type for type variable `a` does not change anything: 
Haskell does not look at the implemented instances. In other words, 
?typeclasses are open?: more typeclasses with Int as first type variable 
(and types different from `Int` for type variable `b`) may be added in the 
future, and Haskell asks to specify which one is to use now, not in the 
future. (Is this the right meaning for "typeclasses are open"?).

"""
test.hs:8:28:
    Could not deduce (Foo a b1) arising from a use of `bar'
    from the context (Foo a b)
      bound by the class declaration for `Foo' at test.hs:(3,1)-(8,37)
    The type variable `b1' is ambiguous
"""

Is this the same interpretation here? That is, even in the default 
implementation of a typeclass function, Haskell does not suppose that the 
instance of `bar` to use is the one of the considered instance?

Thanks a lot,

TP




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

Message: 7
Date: Tue, 20 Aug 2013 12:13:28 +0200
From: TP <paratribulati...@free.fr>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] multi-parameter typeclass with
        default implementation
Message-ID: <83sbea-ove.ln1@rama.universe>
Content-Type: text/plain; charset="ISO-8859-1"

Ben Gamari wrote:

> As far as I know, you'd need to do something like this to accomplish
> what you are after,
> 
>     {-# LANGUAGE MultiParamTypeClasses, DefaultSignatures #-}
>     
>     class Bar a where
>         bar :: a -> Int
>     
>     class FooBar a b where
>         foobar :: a -> b -> Int
>         default foobar :: Bar a => a -> b -> Int
>         foobar avalue bvalue = bar avalue
>     
>     instance Bar Int where
>         bar i = 5
>     instance FooBar Int Int
>     
>     main = do
>         print $ bar (4::Int)
>         print $ foobar (5::Int) (2::Int)

It seems that the "DefaultSignatures" extension is not necessary, the 
following version works correctly on my computer (ghc 7.6.2):

-------
{-# LANGUAGE MultiParamTypeClasses #-}

class Bar a where
    bar :: a -> Int

class FooBar a b where
    foobar :: Bar a => a -> b -> Int
    foobar avalue bvalue = bar avalue

instance Bar Int where
    bar i = 5
instance FooBar Int Int

main = do
    print $ bar (4::Int)
    print $ foobar (5::Int) (2::Int)
-------




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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 62, Issue 20
*****************************************

Reply via email to