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:  Get things from IO monad ([email protected])
   2.  Question about constraints, signatures (Philippe Sismondi)
   3. Re:  Question about constraints, signatures (Brent Yorgey)
   4. Re:  Making a generic interpreter (Patrick LeBoutillier)
   5. Re:  Making a generic interpreter (Erik Helin)
   6. Re:  Making a generic interpreter (Brent Yorgey)
   7.  Function for working with functions in data? (Mike Meyer)
   8. Re:  Making a generic interpreter (Patrick LeBoutillier)


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

Message: 1
Date: Fri, 06 May 2011 10:17:12 +0000
From: [email protected]
Subject: Re: [Haskell-beginners] Get things from IO monad
To: Daniel Fischer <[email protected]>,
        [email protected],  Felipe Almeida Lessa <[email protected]>,
        aditya siram <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="gb2312"; Format="flowed";
        DelSp="yes"

Thanks guys, I thinks I can stop trying to get things out of IO now.  
Instead I should see if I could make my monad as an instance of MonadIO, so  
I can use liftIO, which should be safe I believe.

2011-5-6??3:46?Daniel Fischer <[email protected]>???
> On Friday 06 May 2011 05:28:35, Felipe Almeida Lessa wrote:

> > If you are a beginner, you should never use anything that starts with

> > 'unsafe'.



> Basically yes, use something beginning with 'unsafe' only when you know  
> why

> it's not unsafe in your specific case.



> But of course there are different levels of unsafeness,

> Data.Array.Base.unsafeAt for example is relatively benign (same as in  
> other

> languages: if you don't let the compiler check array bounds and screw your

> check up, you can get nonsense or segfaults, but it's relatively easy to

> understand and control).



> > And you should never use 'unsafePerformIO'. And you

> > should NEVER use 'unsafeCoerce'.



> Those two indeed have a different calibre, use them only

> whenYouReallyKnowWhatYouAreDoing.



> They have their uses and sometimes are the right thing, but like goto, in

> most cases one is tempted to use them, one shouldn't.



> >

> > Cheers, =)

> >

> > --

> > Felipe.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110506/3c219fbe/attachment-0001.htm>

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

Message: 2
Date: Fri, 6 May 2011 11:34:47 -0400
From: Philippe Sismondi <[email protected]>
Subject: [Haskell-beginners] Question about constraints, signatures
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

I am trying to understand the Typeclassopedia article by Brent Yorgey in 
Monad.Reader. I am simultaneously working my way through Real World Haskell, so 
I may be doing things a bit out of order.

Here is my question (which may itself be unclear, as I am a true *noob* at 
this):

Why are there two different kinds of "constraints" in a signature, viz. those 
that specify types and those that specify typeclasses?  For instance, we could 
write this:

f :: (Num d) => d -> String -> Int

In my example above there are two kinds of things that constrain f: 

(a) d must be an instance of Num; and
(b) the second argument to f must be of type String, and f must return an Int.

I (more or less) understand that types and typeclasses are not the same. But it 
seems to me that they do conflate in a way, if we think of types as defined by 
functions on them. So why should we not be able to write:

f ::  Num d -> String -> Int

TIA.

Best,

- Phil -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110506/37ff7665/attachment-0001.htm>

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

Message: 3
Date: Fri, 6 May 2011 12:12:55 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Question about constraints,
        signatures
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, May 06, 2011 at 11:34:47AM -0400, Philippe Sismondi wrote:
> I am trying to understand the Typeclassopedia article by Brent Yorgey in 
> Monad.Reader. I am simultaneously working my way through Real World Haskell, 
> so I may be doing things a bit out of order.
> 
> Here is my question (which may itself be unclear, as I am a true *noob* at 
> this):
> 
> Why are there two different kinds of "constraints" in a signature, viz. those 
> that specify types and those that specify typeclasses?  For instance, we 
> could write this:
> 
> f :: (Num d) => d -> String -> Int
> 
> In my example above there are two kinds of things that constrain f: 
> 
> (a) d must be an instance of Num; and
> (b) the second argument to f must be of type String, and f must return an Int.
> 
> I (more or less) understand that types and typeclasses are not the same. But 
> it seems to me that they do conflate in a way, if we think of types as 
> defined by functions on them. So why should we not be able to write:
> 
> f ::  Num d -> String -> Int

Your intuition is good: in fact, this is the way type classes are
implemented under the hood!  A constraint (Num d => ...) is translated
into a parameter (Num d -> ...) where an argument of type 'Num d' is a
*dictionary* (i.e. record) containing implementations of the Num
methods for the type d.

The reason for the special syntax is that for you, the programmer,
there is a fundamental difference: arguments (dictionaries) coming
before => are *implicit*, that is, you do not provide them yourself,
the compiler figures out what they should be and supplies them
automatically.  Arguments coming after => are explicit arguments which
you must provide when calling a function.

One might ask why it is not possible to explicitly provide
dictionaries if you want: this is a good question and an interesting
area of research, and very related to the notion of "functors"
(i.e. parameterized modules, not the same thing as Haskell Functors)
in ML.

-Brent



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

Message: 4
Date: Fri, 6 May 2011 12:45:43 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Making a generic interpreter
To: Erik Helin <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Erik,

On Fri, May 6, 2011 at 3:01 AM, Erik Helin <[email protected]> wrote:
>
> Now, I would like to change StackElement to be able to work with any
> types, that is:
>
> data StackElement a b = StackInteger a
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | StackInteger b
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? deriving (Show)
>
> How would I make this change?

I don't see how you can make the StackBool something else than Bool.
The CMP and BRANCH operators seem to me very Boolean by definition.
Can you provide an example of how it would work?

Patrick

>
> I thought about using typeclasses, but since type a and type b in
> StackElement a b clearly are related (the == operation on type a must
> result in type b), I need to use multi-parameter type classes (and
> maybe also functional dependecies?).
>
> I also thought about introducing a new type Operation:
>
> data Operation a b = Operation { cmp :: a -> a -> b, add :: a -> a ->
> a, (more required functions) }
>
> Is any of these solutions the preferred one, or is there another
> solution that I don't know of (this is very likely, I'm completely new
> to Haskell)?
>
> Thanks for taking your time and reading this lengthy post,
> Erik
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada



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

Message: 5
Date: Fri, 6 May 2011 19:39:13 +0200
From: Erik Helin <[email protected]>
Subject: Re: [Haskell-beginners] Making a generic interpreter
To: Patrick LeBoutillier <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Patrick,
thanks for taking your time!

On Fri, May 6, 2011 at 18:45, Patrick LeBoutillier
<[email protected]> wrote:
> I don't see how you can make the StackBool something else than Bool.
> The CMP and BRANCH operators seem to me very Boolean by definition.
> Can you provide an example of how it would work?
>
> Patrick
>

I actually can :) I've been hacking today, and the result is:

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

class (Show a) => AbsInteger a b | a -> b where
    (+) :: a -> a -> a
    (==) :: a -> a -> b
    absInteger :: Integer -> a

class (Show a) => AbsBool a where
    cond :: a -> [Code] -> [Code] -> [Code]
    absBool :: Bool -> a

instance AbsBool Bool where
    cond b c1 c2 = if b then c1 else c2
    absBool b = b

instance AbsInteger Integer Bool where
    a + b = (Prelude.+) a b
    a == b = (Prelude.==) a b
    absInteger a = a

Now, if I would like to use my own integer type or my own bool type, I can do:

data MyInteger = MyInteger Integer deriving (Show)
data MyBool = MyBool Bool deriving (Show)

instance AbsBool MyBool where
    cond (MyBool b) c1 c2 = if b then c1 else c2
    absBool b = MyBool b

instance AbsInteger MyInteger MyBool where
    (MyInteger a) + (MyInteger b) = MyInteger $ (Prelude.+) a b
    (MyInteger a) + (MyInteger b) = MyBool $ (Prelude.==) a b
    absInteger a = MyInteger a

The eval function has to be tweaked a little bit, but it is almost
plug'n'play :)

However, due to the functional dependency of AbsInteger, this does not
allow me to use
MyInteger and Bool, since there can only be one pair of an integer
type and a bool type, and Integer and Bool are already instantiating
AbsInteger .

Do you know some of way of making it possible to have several integer
type and boolean type pairs instantiating the class AbsInteger?



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

Message: 6
Date: Fri, 6 May 2011 14:01:08 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Making a generic interpreter
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, May 06, 2011 at 07:39:13PM +0200, Erik Helin wrote:
> Hi Patrick,
> thanks for taking your time!
> 
> On Fri, May 6, 2011 at 18:45, Patrick LeBoutillier
> <[email protected]> wrote:
> > I don't see how you can make the StackBool something else than Bool.
> > The CMP and BRANCH operators seem to me very Boolean by definition.
> > Can you provide an example of how it would work?
> >
> > Patrick
> >
> 
> I actually can :) I've been hacking today, and the result is:
> 
> {-# LANGUAGE MultiParamTypeClasses #-}
> {-# LANGUAGE FunctionalDependencies #-}
> 

Why not use

> class (Show a) => AbsInteger a where
>     (+)        :: a -> a -> a
>     (==)       :: AbsBool b => a -> a -> b
>     absInteger :: Integer -> a

?

-Brent



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

Message: 7
Date: Fri, 6 May 2011 14:57:22 -0400
From: Mike Meyer <[email protected]>
Subject: [Haskell-beginners] Function for working with functions in
        data?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

I'm filling a functional itch that Haskell isn't scratching. I'm
looking for a function to take a list of functions, and apply them all
to a value. The signature is obvious :: [a -> b] -> a -> [b], and it's
nearly trivial to write (\fs a -> map ($ a) [fs]), but Haskell has
most useful list functions already provided.

I couldn't turn it up in hoogle or the prelude. Am I just missing it
somewhere, or is it really not available in a library?

   Thanks,
   <mike
-- 
Mike Meyer <[email protected]>             http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



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

Message: 8
Date: Fri, 6 May 2011 15:12:54 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Making a generic interpreter
To: Erik Helin <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Erik,


On Fri, May 6, 2011 at 1:39 PM, Erik Helin <[email protected]> wrote:
> Hi Patrick,
> thanks for taking your time!
>
> On Fri, May 6, 2011 at 18:45, Patrick LeBoutillier
> <[email protected]> wrote:
>> I don't see how you can make the StackBool something else than Bool.
>> The CMP and BRANCH operators seem to me very Boolean by definition.
>> Can you provide an example of how it would work?
>>
>> Patrick
>>
>
> I actually can :) I've been hacking today, and the result is:

Ok, now I get it!

Did you try Brent's suggestion? For me it worked great and also allows
you to drop the language extentions.


Patrick

-- 
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada



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

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


End of Beginners Digest, Vol 35, Issue 12
*****************************************

Reply via email to