Send Beginners mailing list submissions to
        [email protected]

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
        [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:  Polymorphism question from an OO-speaking    newbie
      (Brent Yorgey)
   2.  Re: Polymorphism question from an OO-speaking    newbie
      (Ertugrul Soeylemez)
   3.  Context reducion Stack overflow (Marco T?lio Gontijo e Silva)
   4. Re:  Context reducion Stack overflow (Brent Yorgey)
   5.  GHCI: Main function seems to mask other  functions (aditya siram)
   6. Re:  Context reducion Stack overflow (Marco T?lio Gontijo e Silva)
   7. Re:  GHCI: Main function seems to mask other      functions
      (Daniel Fischer)
   8. Re:  Context reducion Stack overflow (Brent Yorgey)
   9.  Re: Programa??o  Funcional - TP1 (Marco T?lio Gontijo e Silva)


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

Message: 1
Date: Mon, 4 May 2009 12:47:44 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Polymorphism question from an
        OO-speaking     newbie
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Mon, May 04, 2009 at 10:34:09AM -0500, Joel Neely wrote:
> Short version: Is it possible/reasonable to define a single function
> that accepts a single string or a list of strings (in which case it
> maps the single-string "flavor" over the list)?

Short answer: no.  For the long answer (no, but sort of yes) see below. =)

> Longer version: Thus far I know that Haskell allows me to define a
> function on a single string, then define another function that maps
> the first one across a list of strings, as in:
> 
>     *Main> let quote1 s = "\"" ++ s ++ "\""
> 
>     *Main> "Quux said " ++ quote1 "foo" ++ " loudly"
>     "Quux said \"foo\" loudly"
> 
>     *Main> let quote = map quote1
> 
>     *Main> quote ["foo", "baz", "bletch"]
>     ["\"foo\"","\"baz\"","\"bletch\""]
> 
> (BTW, is it standard terminology to say that quote lifts quote1 to lists?)

There are several standard ways to say it, but that is indeed one of them.

> In the above I have different names for the two functions. OO
> languages such as Java allow me to overload quote to accept either
> String or List<String> (and return the same type). AFAICT, Haskell's
> parametric polymorphism allows me to define functions (e.g. length)
> which deal with "listness" concepts indifferent to the type contained
> by the list.

Right, and this is the key: *indifferent* to the type contained by the
list.  The implementation works in *exactly the same way* for any type
you might wish to stick in.  Java has this, too, with generics.
List<foo> is defined independently of foo; you can stick any foo in
you like and it works in the same way.

However, the overloading you're talking about is known as 'ad-hoc'
polymorphism.  It means that you can give a *different* implementation
for each type.  You can't simulate this with parametric polymorphism.
If you write a Haskell function

  foo :: a -> ...
  foo x = ...

and you want foo to behave differently depending on the type of x, you
can't do it.  There's no way in the ... to decide what to do based on
x's type; the implementation has to be the same no matter what type x
is.

However!  You can get something similar to ad-hoc polymorphism with
Haskell's type classes.  Using type classes, you could do something
like this:
 
  class Quotable q where  -- any type which is an instance of Quotable must 
provide 
    quote :: q -> q       -- an implementation of quote

  instance Quotable String where
    quote = quote1

  instance Quotable [String] where
    quote = map quote1

Now quote has type

  quote :: (Quotable q) => q -> q

and can be used on either Strings or lists of Strings.  Haskell will
use type inference to figure out which version of quote to use
depending on the context in which it is called.

-Brent



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

Message: 2
Date: Mon, 4 May 2009 18:57:03 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: Polymorphism question from an
        OO-speaking     newbie
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Ertugrul Soeylemez <[email protected]> wrote:

> [...]  If that's all you need to know, you can generalize the function
>
>   foo :: Integer -> Integer -> Integer
>   foo a b = a*a + b*b
>
> to the following:
>
>   foo :: Integral i => i -> i -> i
>   foo a b = a*a + b*b
>
> Now 'foo' is defined for every type, which is an instance of the class
> Integral.  Being an instance of that class precisely means that (+),
> (*) and some other functions are defined for the particular type.
> Since this is all you need to know for 'foo', there is no reason to
> restrict it to Integer.

Sorry, I actually meant the Num class, not the Integer class, but the
example is still valid, because any Integral type is also a Num type.
So here is a more general 'foo':

  foo :: Num a => a -> a -> a
  foo a b = a*a + b*b


Greets,
Ertugrul.


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




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

Message: 3
Date: Mon, 04 May 2009 11:59:54 -0300
From: Marco T?lio Gontijo e Silva <[email protected]>
Subject: [Haskell-beginners] Context reducion Stack overflow
To: [email protected]
Message-ID: <1241449194.5122.181.ca...@zezinho>
Content-Type: text/plain

Hello,

I'm trying to compile the following code:

> import Data.IORef

> class F f where
>   m :: (a -> a) -> f a -> f a

> class M m a where
>   mm :: (a -> a) -> m -> IO ()

> instance (F f, M m (f a)) => M m a where
>   mm f v = mm (m f) v

And I'm getting:

$ ghci teste.lhs -XMultiParamTypeClasses -XFlexibleInstances
-XFlexibleContexts -XUndecidableInstances -XIncoherentInstances

GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Failed, modules loaded: none.
Prelude> ^DLeaving GHCi.
GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( teste.lhs, interpreted )

teste.lhs:1:0:
    Context reduction stack overflow; size = 20
    Use -fcontext-stack=N to increase stack size to N
        `$dM{atp} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f
(f (f (f (f a))))))))))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{atm} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f
(f (f (f a)))))))))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{atj} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f
(f (f a))))))))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{atg} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f
(f a)))))))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{atd} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f
a))))))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{ata} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f
a)))))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{at7} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f (f
a))))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{at4} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f (f
a)))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{at1} :: {M m
                        (f (f (f (f (f (f (f (f (f (f (f (f
a))))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asY} :: {M m (f (f (f (f (f (f (f (f (f (f (f
a)))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asV} :: {M m (f (f (f (f (f (f (f (f (f (f a))))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asS} :: {M m (f (f (f (f (f (f (f (f (f a)))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asP} :: {M m (f (f (f (f (f (f (f (f a))))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asM} :: {M m (f (f (f (f (f (f (f a)))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asJ} :: {M m (f (f (f (f (f (f a))))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asG} :: {M m (f (f (f (f (f a)))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asD} :: {M m (f (f (f (f a))))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asA} :: {M m (f (f (f a)))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asx} :: {M m (f (f a))}'
          arising from a use of `mm' at teste.lhs:10:13-22
        `$dM{asj} :: {M m (f a)}'
          arising from a use of `mm' at teste.lhs:10:13-22
Failed, modules loaded: none.

An example of instances of these classes:
> instance F [] where
>   m = map

> instance M (IORef a) a where
>   mm = flip modifyIORef

I don't get what's the problem with my definition.  Can someone clarify
me on this?

Greetings.
-- 
marcot
http://marcot.iaaeee.org/




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

Message: 4
Date: Mon, 4 May 2009 15:54:28 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Context reducion Stack overflow
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

On Mon, May 04, 2009 at 11:59:54AM -0300, Marco Túlio Gontijo e Silva wrote:

> > instance (F f, M m (f a)) => M m a where
> >   mm f v = mm (m f) v

Perhaps you mean

  instance (F f, M m a) => M m (f a) where ...

?

What you have written means that to have an instance M m a, you must
have an instance M m (f a), for which you must have an instance M m (f
(f a)), for which you must have an instance M m (f (f (f a)))...

What I have written above expresses what I presume you meant: we can
automatically derive an instance of (M m) for some structured type (f
a) as long as we have a base instance M m a and can lift functions (a
-> a) to (f a -> f a).
 
-Brent


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

Message: 5
Date: Tue, 5 May 2009 08:11:23 -0500
From: aditya siram <[email protected]>
Subject: [Haskell-beginners] GHCI: Main function seems to mask other
        functions
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi all,
I'm having a weird problem where when I add the 'main' function to my
program and load it into GHCI, whenever I check the type or try to run my
other functions I get a 'Not in scope: <functionname>' error.  When I remove
the 'main' function and reload the code, the problem goes away.

I can't seem to reproduce this with a trivial example, but this has happened
to me several times in my larger programs.

thanks ...
deech
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090505/f05a2b69/attachment-0001.htm

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

Message: 6
Date: Tue, 05 May 2009 10:28:10 -0300
From: Marco T?lio Gontijo e Silva <[email protected]>
Subject: Re: [Haskell-beginners] Context reducion Stack overflow
To: [email protected]
Message-ID: <1241530090.5122.264.ca...@zezinho>
Content-Type: text/plain; charset="UTF-8"

Em Seg, 2009-05-04 às 15:54 -0400, Brent Yorgey escreveu:
> On Mon, May 04, 2009 at 11:59:54AM -0300, Marco Túlio Gontijo e Silva wrote:
> 
> > > instance (F f, M m (f a)) => M m a where
> > >   mm f v = mm (m f) v
> 
> Perhaps you mean
> 
>   instance (F f, M m a) => M m (f a) where ...
> 
> ?

No, I really meant what I wrote.  An example: If I the instances I
wrote:

> instance F [] where
>   m = map

> instance M (IORef a) a where
>   mm = flip modifyIORef

I want to define:

> instance M (IORef [a]) a where
>   mm f v = mm (m f) v

This could of course be written as:

mm = mm . m

I'd like to get this last instance automaticly from that definition,
having:

f = []
m = IORef [a]

> What you have written means that to have an instance M m a, you must
> have an instance M m (f a), for which you must have an instance M m (f
> (f a)), for which you must have an instance M m (f (f (f a)))...

After this explanation I think I located the problem.  My problem seems
to be with how IncoherentInstances work, by picking always the most
generic instance.

Thanks for that.  Do you know if there a way to get around this, maybe
using another GHC extension?

Greetings.
-- 
marcot
http://marcot.iaaeee.org/




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

Message: 7
Date: Tue, 5 May 2009 16:42:06 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] GHCI: Main function seems to mask
        other   functions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-15"

Am Dienstag 05 Mai 2009 15:11:23 schrieb aditya siram:
> Hi all,
> I'm having a weird problem where when I add the 'main' function to my
> program and load it into GHCI, whenever I check the type or try to run my
> other functions I get a 'Not in scope: <functionname>' error.  When I
> remove the 'main' function and reload the code, the problem goes away.
>
> I can't seem to reproduce this with a trivial example, but this has
> happened to me several times in my larger programs.
>
> thanks ...
> deech

Odd. Does it happen with the module header

module Main where

, with an explicit export list, or without any module header?

Is it so that with the main function, you load the compiled function, then when 
you get 
the Not in scope, you remove the main function and reload the module without 
compiling?
Then it's probably the correct behaviour, cf. the user's guide, section 3.4.3. 
What's 
really in scope at the prompt?



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

Message: 8
Date: Tue, 5 May 2009 11:33:58 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Context reducion Stack overflow
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

On Tue, May 05, 2009 at 10:28:10AM -0300, Marco Túlio Gontijo e Silva wrote:
> Em Seg, 2009-05-04 às 15:54 -0400, Brent Yorgey escreveu:
> > On Mon, May 04, 2009 at 11:59:54AM -0300, Marco Túlio Gontijo e Silva wrote:
> > 
> > > > instance (F f, M m (f a)) => M m a where
> > > >   mm f v = mm (m f) v
> > 
> > Perhaps you mean
> > 
> >   instance (F f, M m a) => M m (f a) where ...
> > 
> > ?
> 
> No, I really meant what I wrote.  An example: If I the instances I
> wrote:
> 
> > instance F [] where
> >   m = map
> 
> > instance M (IORef a) a where
> >   mm = flip modifyIORef
> 
> I want to define:
> 
> > instance M (IORef [a]) a where
> >   mm f v = mm (m f) v
> 
> This could of course be written as:
> 
> mm = mm . m
> 
> I'd like to get this last instance automaticly from that definition,
> having:
> 
> f = []
> m = IORef [a]

I don't follow.  In order to get 
 
  instance M (IORef [a]) a

from

  instance (F f, M m (f a)) => M m a

would require

  instance M (IORef [a]) (f a)

for some f, which you don't have.

I might try rewriting M as

  class M f a where
    mm :: (a -> a) -> f a -> IO ()

and then your automatic lifting instance would be something like

  instance (F f, M f2 a) => M (f :.: f2) a

where :.: denotes functor composition.


-Brent


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

Message: 9
Date: Tue, 05 May 2009 15:06:41 -0300
From: Marco T?lio Gontijo e Silva <[email protected]>
Subject: [Haskell-beginners] Re: Programa??o    Funcional - TP1
To: [email protected]
Message-ID: <1241546801.5122.520.ca...@zezinho>
Content-Type: text/plain; charset="UTF-8"

Olá Henrique,

seu problema Polepos.hs excedeu o tempo limite de execução no SPOJ.

Algumas dicas: você pode escrever:

cars = map fst startGrid
positions = map snd startGrid

(cars, positions) = unzip

take (length positions) [0..]
[0 .. pred $ length positions]

do scoreBoard <- parseScoreboard n
                      let startGrid = getStartGrid scoreBoard
                      putStrLn $ showGrid startGrid
                      main

parseScoreboard n >>= putStrLn . showGrid . getStartGrid >> main

showGrid Nothing = "-1"
showGrid (Just xs) = foldl1 (++) $ intersperse " " $ map show xs

showGrid = maybe "-1" (foldl1 (++) . intersperse " " . map show)

Talvez fosse mais legal criar um tipo de dados e definir a classe Ord
para eles do que usar tupla e sortBy.

Essas dicas não estão relacionadas com a demora no tempo de execução.
Isso se deve, acredito eu, ao seu algoritmo.

-- 
marcot
http://marcot.iaaeee.org/




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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 11, Issue 4
****************************************

Reply via email to