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
        [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:  evaluation of expressions [was Re:   eval    command?]
      (Tillmann Rendel)
   2. Re:  evaluation of expressions [was Re: eval      command?]
      (Andrew Sackville-West)
   3. Re:  evaluation of expressions [was Re: eval      command?]
      (Andrew Sackville-West)
   4.  Control.Monad.State: State CTOR unneeded to      create a State?
      (Larry Evans)
   5. Re:  Control.Monad.State: State CTOR unneeded to  create a
      State? (Antoine Latter)
   6. Re:  Control.Monad.State: State CTOR unneeded     to create a
      State? (Larry Evans)
   7. Re:  Control.Monad.State: State CTOR unneeded to  create a
      State? (Luca Padovani)
   8. Re:  Control.Monad.State: State CTOR unneeded to  create a
      State? (Brandon S. Allbery KF8NH)


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

Message: 1
Date: Wed, 29 Oct 2008 09:27:32 +0100
From: Tillmann Rendel <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] evaluation of expressions [was Re:
        eval    command?]
To: Andrew Sackville-West <[EMAIL PROTECTED]>
Cc: beginners@haskell.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed

Andrew Sackville-West schrieb:
> this raises a question for me, being a bit of a schemer. Is there any
> parallel in haskell to the data is code model of the lisp family? 

No.

> My initial impression is no, that you'd have to parse it as an
> expression and evaluate it as you would in regular imperative
> languages. I'd love to hear otherwise.

I don't see how "code is data" is connected to imperative vs. purely 
functional. After all, lisp & co. are not purely functional, but feature 
"code is data". Another well-known symbolic language, which allows to 
treat code as data and vice versa, is prolog.

Since Haskell features algebraic data types, and a reasonable flexible 
syntax, you do not need to do any parsing. Instead, you can write down 
the AST of the embedded language directly as part of your Haskell 
program. But you have to write an evaluator. With pattern matching, that 
is often very easy, though.

   Tillmann


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

Message: 2
Date: Wed, 29 Oct 2008 07:02:46 -0700
From: Andrew Sackville-West <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] evaluation of expressions [was Re:
        eval    command?]
To: beginners@haskell.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset="us-ascii"

On Wed, Oct 29, 2008 at 09:27:32AM +0100, Tillmann Rendel wrote:
> Andrew Sackville-West schrieb:
>> this raises a question for me, being a bit of a schemer. Is there any
>> parallel in haskell to the data is code model of the lisp family? 
>
> No.
>
>> My initial impression is no, that you'd have to parse it as an
>> expression and evaluate it as you would in regular imperative
>> languages. I'd love to hear otherwise.
>
> I don't see how "code is data" is connected to imperative vs. purely  
> functional. After all, lisp & co. are not purely functional, but feature  
> "code is data". Another well-known symbolic language, which allows to  
> treat code as data and vice versa, is prolog.

I didn't mean to connect the concept to imperative vs. functional. 

>
> Since Haskell features algebraic data types, and a reasonable flexible  
> syntax, you do not need to do any parsing. Instead, you can write down  
> the AST of the embedded language directly as part of your Haskell  
> program. But you have to write an evaluator. With pattern matching, that  
> is often very easy, though.

looks like I'm off to read about Template Haskell.

thanks to all.

A
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20081029/7cc44d4a/attachment-0001.bin

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

Message: 3
Date: Wed, 29 Oct 2008 07:48:59 -0700
From: Andrew Sackville-West <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] evaluation of expressions [was Re:
        eval    command?]
To: beginners@haskell.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset="us-ascii"

On Wed, Oct 29, 2008 at 07:02:46AM -0700, Andrew Sackville-West wrote:
> On Wed, Oct 29, 2008 at 09:27:32AM +0100, Tillmann Rendel wrote:

...
> > Since Haskell features algebraic data types, and a reasonable flexible  
> > syntax, you do not need to do any parsing. Instead, you can write down  
> > the AST of the embedded language directly as part of your Haskell  
> > program. But you have to write an evaluator. With pattern matching, that  
> > is often very easy, though.
> 
> looks like I'm off to read about Template Haskell.

And it looks like:

http://www.haskell.org/tmrwiki/TemplateHaskell#head-96ad34fffdb541d4e334edeec77d71061beb2ec8

is a pretty good starting point. To write a symbolic differentiator in
haskell, with evaluable results, this looks to be the way to start.

A
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20081029/6b0af174/attachment-0001.bin

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

Message: 4
Date: Thu, 30 Oct 2008 22:07:59 -0500
From: Larry Evans <[EMAIL PROTECTED]>
Subject: [Haskell-beginners] Control.Monad.State: State CTOR unneeded
        to      create a State?
To: Beginners@haskell.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Page:

  
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#2

suggests, with the content:

  Constructors
    State    
      runState :: (s -> (a, s))    

that State has a labeled constructor:

  http://www.haskell.org/onlinereport/exps.html#sect3.15.2
 
yet the Examples section:

  
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#4
 
shows, AFAICT, no call to the State CTOR:

  tick :: State Int Int
  tick = do n <- get
            put (n+1)
            return n
            
I assume the n must be an Int (since it's in n+1), but what are the
get and put.  There is a get and put described here:

  
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#1
 
however, I don't see how they are relevant since they are defined for
an already existing MonadState, and AFAICT, tick hasn't been defined
yet since it's on the lhs of the =.

Please, what am I missing?

TIA

-regards
Larry



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

Message: 5
Date: Thu, 30 Oct 2008 21:20:50 -0500
From: "Antoine Latter" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Control.Monad.State: State CTOR
        unneeded to     create a State?
To: "Larry Evans" <[EMAIL PROTECTED]>
Cc: Beginners@haskell.org
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Oct 30, 2008 at 10:07 PM, Larry Evans <[EMAIL PROTECTED]> wrote:
> Page:
>
>  
> http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#2
>
> suggests, with the content:
>
>  Constructors
>   State         runState :: (s -> (a, s))
> that State has a labeled constructor:
>
>  http://www.haskell.org/onlinereport/exps.html#sect3.15.2
>
> yet the Examples section:
>
>  
> http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#4
>
> shows, AFAICT, no call to the State CTOR:
>
>  tick :: State Int Int
>  tick = do n <- get
>           put (n+1)
>           return n
>           I assume the n must be an Int (since it's in n+1), but what are
> the
> get and put.  There is a get and put described here:
>
>  
> http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#1
>
> however, I don't see how they are relevant since they are defined for
> an already existing MonadState, and AFAICT, tick hasn't been defined
> yet since it's on the lhs of the =.
>
> Please, what am I missing?
>

Hi Larry.   Event thought 'get' and 'put' are defined in MonadState,
the concrete implementation of them for "State s a" uses the 'State'
constructor.  They probably look something like so:

get = State (\s -> (s,s))
put newS = State (\oldS ->( (), newS  ))

Also, the definitions of the 'Monad' class functions also need to use
the 'State' constructor, for example return should look something
like:

return x = State (\s -> (x, s))

So when you use expressions in a 'do' block, or 'get' or 'put' then
under the hood you're using the 'State' constructor.

Then at the top-level you use 'runState :: State s a -> s -> (a,s)' to
strip the constructor away.

As long as you aren't writing anything that needs to dig to much into
the workings of 'State', you shouldn't have to worry too much about
it.

I hope I haven't made things more confused ^_^

-Antoine


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

Message: 6
Date: Sat, 01 Nov 2008 11:23:38 -0500
From: Larry Evans <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Control.Monad.State: State CTOR
        unneeded        to create a State?
To: Antoine Latter <[EMAIL PROTECTED]>
Cc: beginners@haskell.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 10/30/08 21:20, Antoine Latter wrote:
 > On Thu, Oct 30, 2008 at 10:07 PM, Larry Evans 
<[EMAIL PROTECTED]> wrote:
 >> Page:
 >>
 >>  
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#2
 >>
 >> suggests, with the content:
 >>
 >>  Constructors
 >>   State         runState :: (s -> (a, s))
 >> that State has a labeled constructor:
 >>
 >>  http://www.haskell.org/onlinereport/exps.html#sect3.15.2
 >>
 >> yet the Examples section:
 >>
 >>  
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#4
 >>
 >> shows, AFAICT, no call to the State CTOR:
 >>
 >>  tick :: State Int Int
 >>  tick = do n <- get
 >>           put (n+1)
 >>           return n
 >>           I assume the n must be an Int (since it's in n+1), but 
what are
 >> the
 >> get and put.  There is a get and put described here:
 >>
 >>  
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#1
 >>
 >> however, I don't see how they are relevant since they are defined for
 >> an already existing MonadState, and AFAICT, tick hasn't been defined
 >> yet since it's on the lhs of the =.
 >>
 >> Please, what am I missing?
 >>
 >
 > Hi Larry.   Event thought 'get' and 'put' are defined in MonadState,
 > the concrete implementation of them for "State s a" uses the 'State'
 > constructor.  They probably look something like so:
 >
 > get = State (\s -> (s,s))
 > put newS = State (\oldS ->( (), newS  ))

That still leaves me confused about:

  put (n+1)
 
AFAICT, with the preceding |n <- get|, this means:

  put (get+1)
 
and since the get has type, |State Int Int|, and there's
not + operator defined on that type (because the following:

  let get1 = get::(State Int Int)
  print (evalState get1 99)
  let get1p1 = get1+1
  putStrLn "get1p1="

compiles with error:

{---cut here---

GenMonads2Arrows.hs:40:15:
    No instance for (Num (State Int Int))
      arising from a use of `+' at GenMonads2Arrows.hs:40:15-20
    Possible fix: add an instance declaration for (Num (State Int Int))
    In the expression: get1 + 1
    In the definition of `get1p1': get1p1 = get1 + 1
    In the expression:
        do putStr "plusOneCtor="
           print (plusOneCtor int1)
           putStr "plusOne="
           print (plusOne int1)
 
}---cut here---

So, looking (again ;) at:

  
http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#4
 
shows:

  get ::m s
 
where, based on:

  Instances:
    MonadState s (State s)
   
from the Control-Monad_state.hmtl#4,   

  m is |State Int| and s is |Int|

however, that's just what was used above that wasn't compatible with
+1.

So, how can get be used in |get+1|?  Is there some way
to trace what's happening in the tick rhs?  Could
template haskell be any help here?  I can't use ghci
because I have to explicity type the get result, and
that leads to the above compile error.  I'm stuck!
Help!

[snip]
 > As long as you aren't writing anything that needs to dig to much into
 > the workings of 'State', you shouldn't have to worry too much about
 > it.

I'm trying to understand how to use MonadState. I would have
expected tick would have been defined:

  tick = State (\n -> (n+1,n+1))
 
instead of the more obscure:

  tick = do n <- get
            put (n+1)
            return n

I figured that understanding how the more obscure definition worked
might show me why it was used instead of the simpler definition.
However, as noted above, I can't figure out how the more obscure
defintion works and I don't even know if the two definitions comput
the same results.

 >
 > I hope I haven't made things more confused ^_^


You've helped.  Thanks.

 -Larry






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

Message: 7
Date: Sat, 1 Nov 2008 16:48:38 +0100
From: "Luca Padovani" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Control.Monad.State: State CTOR
        unneeded to     create a State?
To: "Larry Evans" <[EMAIL PROTECTED]>
Cc: beginners@haskell.org
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Larry,

I approached Haskell very recently and I see in your posts many doubts
that I experienced myself, so perhaps I can be of some help by
providing you with the intuition that guided me, rather than with an
in depth explanation of how monadic state works.

On Sat, Nov 1, 2008 at 5:23 PM, Larry Evans <[EMAIL PROTECTED]> wrote:
>> get = State (\s -> (s,s))
>> put newS = State (\oldS ->( (), newS  ))
>
> That still leaves me confused about:
>
>  put (n+1)
>
> AFAICT, with the preceding |n <- get|, this means:
>
>  put (get+1)

pretty much as with IO, you shouldn't think of get as of a function
(or value) representing the value of the state, nor should you think
of put as of a function that modifies the state. You should think of
get and put (n + 1) as of *actions* that, *when performed*, will read
and write the state. In particular

  put (n + 1)

is NOT equivalent to

  put (get + 1)

because "get" here is not the state (of value Int), but it's an action
that lets you access the state and

  n <- get

is the way you have to say that you want to perform the get action and
bind the value in the state to the name n.

The thing that I found really weird in the beginning was that with
State the state is never mentioned anywhere! But there is where the
intuition "action that reads/writes the state" comes into the game.

--luca


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

Message: 8
Date: Sat, 1 Nov 2008 12:02:41 -0400
From: "Brandon S. Allbery KF8NH" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Control.Monad.State: State CTOR
        unneeded to     create a State?
To: Larry Evans <[EMAIL PROTECTED]>
Cc: beginners@haskell.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

On 2008 Nov 1, at 12:23, Larry Evans wrote:
> That still leaves me confused about:
>
> put (n+1)
> AFAICT, with the preceding |n <- get|, this means:
>
> put (get+1)

The <- syntax (actually (>>=)) "unwraps" a monadic value (on condition  
that it is "rewrapped" later), so instead of |State Int Int| it's just  
|Int|.

> I'm trying to understand how to use MonadState. I would have
> expected tick would have been defined:
>
> tick = State (\n -> (n+1,n+1))

I think you meant \n -> (n,n+1)

>
> instead of the more obscure:
>
> tick = do n <- get
>           put (n+1)
>           return n

The above translates to

     get >>= \n -> put (n+1) >> return n

Expand (>>=):

     State (\s -> let (a,s') = runState get s
                   in runState (put (a+1)) s')

Expand |get| and |put|:

     State (\s -> let (a,s') = runState (State (\s'' -> (s'',s'')))
                   in runState (State (\_ -> ((),a+1))) s')

|runState| and |State| cancel:

     State (\s -> let (a,s') = (\s'' -> (s'',s''))
                   in (\_ -> (s',a+1)))

Now expand the inner lambdas:

     State (\s -> let (a,s') = (s,s)
                   in (s',a+1))

And collapse the let:

     State (\s -> (s,s+1))

(I think I did that right; at least I got the right result...)

The point of using the "more obscure" definition is that it uses the  
machinery already defined for |State| --- and (given that I'm not  
absolutely certain I expanded the above right!) using the machinery is  
easier to keep track of than expanding directly, while demonstrating  
the operation of |State| instead of just presenting a result without  
explanation of how it follows from the definition of |State|.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon university    KF8NH




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

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


End of Beginners Digest, Vol 5, Issue 1
***************************************

Reply via email to