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:  Function for working with functions in data?
      (Brandon S Allbery KF8NH)
   2. Re:  Function for working with functions in data? (Daniel Fischer)
   3. Re:  Making a generic interpreter (Erik Helin)
   4. Re:  Making a generic interpreter (Patrick LeBoutillier)
   5. Re:  Making a generic interpreter (Erik Helin)


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

Message: 1
Date: Fri, 06 May 2011 15:13:01 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] Function for working with functions
        in data?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 5/6/11 14:57 , Mike Meyer wrote:
> 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.

It's there, but it's not a dedicated function; it's "sequence" applied to
the environment monad (-> e).

    Prelude Control.Monad.Reader> :t sequence
    sequence :: (Monad m) => [m a] -> m [a]
    Prelude Control.Monad.Reader> :t sequence [(+1),(*2),(`subtract` 3)]
    sequence [(+1),(*2),(`subtract` 3)] :: (Num a) => a -> [a]

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]    [email protected]
system administrator  [openafs,heimdal,too many hats]                kf8nh
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3ESD0ACgkQIn7hlCsL25XFeQCgnD2GTT4sjYKJ5Nw9QGG+qGJb
8NkAoKq07s8sUxjqw3CthG/0HNiKwnlU
=ifKQ
-----END PGP SIGNATURE-----



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

Message: 2
Date: Fri, 6 May 2011 21:27:22 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Function for working with functions
        in data?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Friday 06 May 2011 21:13:01, Brandon S Allbery KF8NH wrote:
> On 5/6/11 14:57 , Mike Meyer wrote:
> > 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.
> 
> It's there, but it's not a dedicated function; it's "sequence" applied
> to the environment monad (-> e).
> 
>     Prelude Control.Monad.Reader> :t sequence
>     sequence :: (Monad m) => [m a] -> m [a]
>     Prelude Control.Monad.Reader> :t sequence [(+1),(*2),(`subtract` 3)]
>     sequence [(+1),(*2),(`subtract` 3)] :: (Num a) => a -> [a]

The Monad instance for ((->) e) is defined in Control.Monad.Instances, so 
to use it, you must directly or indirectly (e.g. via Control.Monad.Reader 
or Control.Monad.State) import that.




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

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

On Fri, May 6, 2011 at 20:01, Brent Yorgey <[email protected]> wrote:
>
> Why not use
>
>> class (Show a) => AbsInteger a where
>> ? ? (+) ? ? ? ?:: a -> a -> a
>> ? ? (==) ? ? ? :: AbsBool b => a -> a -> b
>> ? ? absInteger :: Integer -> a
>
> ?

Because I believe (I might be very wrong now, I am very new to
Haskell) that I would run into problem with the b in "AbsBool b => a
-> a -> b" being unbounded when it would be used in:

data StackElement a b = StackInteger a
                                   |  StackBool b
                                   deriving (Show)

eval :: (AbsInteger a, AbsNum b) => [Code] -> [StackElement a b] ->
[StackElement a b]
{- some cases omitted -}
eval (CMP:c) (StackInteger x:StackInteger y:t) = eval c (StackBool (x == y):t)

Now, GHC tells me that the type b returned by x == y does not need to
equal the b specified in the type definition of eval.

Did I do something wrong in my definiton of eval?

On Fri, May 6, 2011 at 21:12, Patrick LeBoutillier
<[email protected]> wrote:
> Did you try Brent's suggestion? For me it worked great and also allows
> you to drop the language extentions.

I tried, but I didn't manage to get it working, due to the what I
describe above. Could you post your code using Brent's suggestion?

Clearly I am doing something wrong here, I just don't know what it is...



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

Message: 4
Date: Fri, 6 May 2011 21:11:04 -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,

Here's the code I came up with. One thing I changed was using the
names "add" and "eq" for the AbsInteger type class (I got confused by
all the =s and +s).

Coming from very imperative (and mostly dynamically typed) background
I must admit that this kind of stuff blows my mind a bit...

Patrick

==========================

data Code a = PUSH a | ADD | CMP
            | BRANCH [Code a] [Code a]
            deriving (Show)


data StackElement a b = StackValue a | StackBool b
                      deriving (Show)


type Stack a b = [StackElement a b]


class AbsInteger i where
   add :: i -> i -> i
   eq :: AbsBool b => i -> i -> b
   absInteger :: Integer -> i

class AbsBool b where
   cond :: (AbsInteger i) => b -> [Code i] -> [Code i] -> [Code i]
   absBool :: Bool -> b


eval :: (AbsInteger a, AbsBool b) => [Code a] -> Stack a b -> Stack a b
eval [] s = s
eval (PUSH n:t) s = eval t (StackValue n:s)
eval (ADD:t) (StackValue x:StackValue y:s) = eval t (StackValue (x `add` y):s)
eval (CMP:t) (StackValue x:StackValue y:s) = eval t (StackBool (x `eq` y):s)
eval (BRANCH c1 c2:t) (StackBool b:s) = eval ((cond b c1 c2) ++ t) s


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

instance AbsInteger Integer where
   a `add` b = (+) a b
   a `eq` b = absBool $ a == b
   absInteger a = a


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 where
   (MyInteger a) `add` (MyInteger b) = MyInteger $ (+) a b
   (MyInteger a) `eq` (MyInteger b) = absBool $ (==) a b
   absInteger a = MyInteger a


code :: AbsInteger a => [Code a]
code = [PUSH . absInteger $ 1, PUSH . absInteger $ 2, ADD, PUSH .
absInteger $ 3, CMP, BRANCH [PUSH . absInteger $ 1] [PUSH . absInteger
$ 0]]

test1 = eval code [] :: Stack Integer Bool
test2 = eval code [] :: Stack Integer MyBool
test3 = eval code [] :: Stack MyInteger Bool
test4 = eval code [] :: Stack MyInteger MyBool


On Fri, May 6, 2011 at 3:32 PM, Erik Helin <[email protected]> wrote:
> On Fri, May 6, 2011 at 20:01, Brent Yorgey <[email protected]> wrote:
>>
>> Why not use
>>
>>> class (Show a) => AbsInteger a where
>>> ? ? (+) ? ? ? ?:: a -> a -> a
>>> ? ? (==) ? ? ? :: AbsBool b => a -> a -> b
>>> ? ? absInteger :: Integer -> a
>>
>> ?
>
> Because I believe (I might be very wrong now, I am very new to
> Haskell) that I would run into problem with the b in "AbsBool b => a
> -> a -> b" being unbounded when it would be used in:
>
> data StackElement a b = StackInteger a
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ?StackBool b
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? deriving (Show)
>
> eval :: (AbsInteger a, AbsNum b) => [Code] -> [StackElement a b] ->
> [StackElement a b]
> {- some cases omitted -}
> eval (CMP:c) (StackInteger x:StackInteger y:t) = eval c (StackBool (x == y):t)
>
> Now, GHC tells me that the type b returned by x == y does not need to
> equal the b specified in the type definition of eval.
>
> Did I do something wrong in my definiton of eval?
>
> On Fri, May 6, 2011 at 21:12, Patrick LeBoutillier
> <[email protected]> wrote:
>> Did you try Brent's suggestion? For me it worked great and also allows
>> you to drop the language extentions.
>
> I tried, but I didn't manage to get it working, due to the what I
> describe above. Could you post your code using Brent's suggestion?
>
> Clearly I am doing something wrong here, I just don't know what it is...
>



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



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

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

Thank you Patrick for posting your code, it helps a lot too see a
complete example!

On Sat, May 7, 2011 at 03:11, Patrick LeBoutillier
<[email protected]> wrote:
> instance AbsInteger Integer where
> ? a `add` b = (+) a b
> ? a `eq` b = absBool $ a == b
> ? absInteger a = a

In my code, I did not use "a `eq` b = absBool $ a == b". Instead, I
used "a `eq` b = a == b".
I thought, since "a == b" is of type Bool, and Bool is an instance of
AbsBool, I can just return type Bool.

Why isn't this the case? What is it about typeclasses that I'm not
understanding?

I updated my code to also use "absBool", and it works great without
muti-parameter typeclasses and functional dependencies!

Patrick and Brent, thank you so much for your help! You have both
inspired me to continue my journey with Haskell :)

Btw, Patrick, do you have any small Haskell project (or can you
recommend some other project) on github (or something similar) that I
can read to see more examples of good Haskell code?



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

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


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

Reply via email to