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:  Question about constraints, signatures (Philippe Sismondi)
   2. Re:  Question about constraints, signatures (David Virebayre)
   3.  Is there such a thing as a lazy monadic list? (Arlen Cuss)


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

Message: 1
Date: Sun, 8 May 2011 10:48:26 -0400
From: Philippe Sismondi <[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 2011-05-06, at 12:12 PM, Brent Yorgey wrote:

> 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

Thanks for the quick reply, Brent. It will take me a day or so to digest your 
answer.

- Phil -


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

Message: 2
Date: Mon, 9 May 2011 10:04:57 +0200
From: David Virebayre <[email protected]>
Subject: Re: [Haskell-beginners] Question about constraints,
        signatures
To: Philippe Sismondi <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

2011/5/8 Philippe Sismondi <[email protected]>:

> On 2011-05-06, at 12:12 PM, Brent Yorgey wrote:

>> 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.

>>> 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

You can have multiple class constraints, so it's not clear how you
would write them using your syntax; how would you write :

f :: (Eq d, Ord d) => d -> String -> Int ?

Also, you only have to tell the class constraint once, with your syntax

g :: (Num d) => d -> d -> d

would become:

g :: Num d -> Num d -> Num d

but that syntax lets you write nonsense :

h :: Num d -> Eq d -> Ord d


David.



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

Message: 3
Date: Mon, 09 May 2011 18:41:12 +1000
From: Arlen Cuss <[email protected]>
Subject: [Haskell-beginners] Is there such a thing as a lazy monadic
        list?
To: [email protected]
Message-ID: <1304930472.2495.26.camel@asu>
Content-Type: text/plain; charset="utf-8"

Hi all,

I'm currently trying to write a very simple VM in order to learn and
stretch my Haskell. I'm currently up against a problem.

I'm using an (IOUArray Int Word32) to represent the memory of a VM
instance. This is all fine so far, but now I'm trying to write a
'disassemble' function that will disassemble an instruction starting
from a given location in memory.

Ideally I could write a function that would lazily yield successive
elements from memory, sort of like this:

> readVMFrom :: VM -> Address -> IO [Word32]
> readVMFrom v f = do
>   i <- readArray (memory v) f
>   rest <- readVMFrom v (succ f)
>   return $ i : readVMFrom v (succ f)

Of course, this isn't lazy, and so happily runs off the end of memory
each time. (I could stop that, but that's not the point!) I'm not sure
how to lazily construct a list in IO.

My next thought is to instead make a function to return an action that
yields successive values of memory, given some start, i.e. something
like:

> readVMFrom :: VM -> Address -> IO Word32

.. but I'm thinking I might have to do some StateT nonsense in order to
actually keep track of which index we want to read next. (subtext: I
don't know how to do that!)

So I then think that perhaps I can do this by having readVMFrom return
an action which yields a) the next value, b) the address of the next
value (so we know how much we ate), and c) an action to yield the next
value and action and so on, so on. I managed that this way (WARNING,
BEGINNER HASKELL CODE FOLLOWS):

> newtype RVFResult = RVFResult { getRVFResult :: (Word32, Address, IO
RVFResult) }
>
> readVMFrom :: VM -> Address -> IO RVFResult
> readVMFrom v f = do
>   i <- readArray (memory v) f
>   return $ RVFResult (i, succ f, readVMFrom v $ succ f)

A function can then take an argument of type RVFResult as an indication
it wants 1..n sequential values from memory:

> disassemble :: RVFResult -> IO (Instruction, Address)
> disassemble r = do
>   let (i, p, a) = getRVFResult r
>   case i of
>     0x01 -> return (Push, p)
>     0x02 -> return (Pop, p)
>     0x03 -> return (Call, p)
>     0x04 -> do
>               r <- a
>               let (v, p, a) = getRVFResult r
>               return (Out v, p)

Here you can see how we consume an additional value by executing the
returned action 'a'. It's damn ugly, and this almost looks like the
place for a monad (possibly something involving StateT, though, I don't
know!).

Ideally I'd manage a lazily constructed list -- somehow I feel that's
most "Haskelly" -- but does anyone else have any ideas? I kinda fleshed
this last solution out as I wrote this email, so it's kinda cruddy.

Cheers,

Arlen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110509/5a4f9422/attachment-0001.pgp>

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

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


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

Reply via email to