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.  show vs. Data.ByteString.Char8.unpack for        ByteString (Hong Yang)
   2. Re:  foldr for Nats ([email protected])
   3. Re:  show vs. Data.ByteString.Char8.unpack for    ByteString
      (Daniel Fischer)
   4. Re:  show vs. Data.ByteString.Char8.unpack for    ByteString
      (Stephen Blackheath [to Haskell-Beginners])
   5.  Typeclass problems (Patrick LeBoutillier)
   6. Re:  Typeclass problems
      (Stephen Blackheath [to Haskell-Beginners])
   7. Re:  Typeclass problems (Daniel Fischer)
   8.  Simulation of interconnect network (Roger King)
   9. Re:  Simulation of interconnect network (Ulrik Rasmussen)


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

Message: 1
Date: Thu, 4 Feb 2010 14:46:39 -0600
From: Hong Yang <[email protected]>
Subject: [Haskell-beginners] show vs. Data.ByteString.Char8.unpack for
        ByteString
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hi,

What is the difference between show and unpack when the function arguments
are of ByteString type? I thought they are the same, but they are not
interchangeable at compile time.

Thanks,

Hong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100204/f5b18c1c/attachment-0001.html

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

Message: 2
Date: Thu, 04 Feb 2010 21:58:45 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] foldr for Nats
To: Stephen Tetley <[email protected]>,  Daniel Fischer
        <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

@Stephan thanks for the code so far.
@Daniel it should look like foldrNat a b S(S(S(S(Z)))) gives a(a(a(a(b))))


The thing is the definition have to be
-------- Original-Nachricht --------
> Datum: Mon, 1 Feb 2010 21:56:14 +0000
> Von: Stephen Tetley <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] foldr for Nats

> Hello
> 
> I'm suspecting this isn't homework as you've waited a week so would
> presumably have missed a deadline.
> 
> As Daniel Fischer wrote, one view of folds is that they replace the
> constructors of a data type, code follows...
> 
> 
> data Nat = Z | S Nat   deriving (Eq,Ord,Show)
> 
> 
> -- Look at the type of foldr...
> 
> -- *GHCi> :t foldr
> -- foldr :: (a -> b -> b) -> b -> [a] -> b
> 
> -- It has 2 'constructor replacements':
> -- (a -> b -> b) & b
> 
> -- Replacing Z is easy, we can get some code to compile
> -- by avoiding the hard bit with a wildcard pattern "_"...
> 
> foldrNat1 :: unknown -> b -> Nat -> b
> foldrNat1 _ b Z = b
> 
> -- What to do about the constructor (S ..) takes a bit more
> -- thought or at least some experimenting. I'll do the later...
> 
> -- One thing to try, is to simply translate foldr with as few
> -- changes as possible:
> 
> -- foldr            :: (a -> b -> b) -> b -> [a] -> b
> -- foldr _ z []     =  z
> -- foldr f z (x:xs) =  f x (foldr f z xs)
> 
> -- Unfortunately this leads to a problem:
> 
> foldrNat2 :: (Nat -> b -> b) -> b -> Nat -> b
> foldrNat2 f b Z     = b     -- Z case is the same as before
> foldrNat2 f b (S n) = f undefined (foldrNat2 f b n) -- Arggh! undefined
> 
> -- undefined is useful for prototyping, but its a real
> -- problem for running code!
> 
> -- Actually I had another problem as well...
> --
> -- The difference between Nat and [a] is that List 'carries' some data
> -- therefore (Nat -> b -> b) on Nat is not equivalent to (a -> b -> b)
> -- on [a].
> 
> -- So rather than change the type signature first, get rid of the
> -- undefined and see what happens
> 
> foldrNat3 f b Z     = b
> foldrNat3 f b (S n) = f (foldrNat3 f b n)
> 
> -- *GHCi> :t foldrNat3
> -- > (t -> t) -> t -> Nat -> t
> 
> -- GHCi likes to call type variables t, but the signature is equal to
> 
> -- foldrNat3 :: (b -> b) -> b -> Nat -> b
> 
> 
> -- This looks promising - it typechecks!
> -- So try a test:
> 
> fromNat :: Nat -> Int
> fromNat n = foldrNat3 (+1) 0 n
> 
> demo1 = fromNat (S (S (S Z)))   -- 3 ??
> 
> -- By experimenting we seem to have a good answer,
> -- other people might prefer a more rigorous proof though.

-- 
NEU: Mit GMX DSL über 1000,- ¿ sparen!
http://portal.gmx.net/de/go/dsl02


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

Message: 3
Date: Thu, 4 Feb 2010 22:05:18 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] show vs. Data.ByteString.Char8.unpack
        for     ByteString
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Donnerstag 04 Februar 2010 21:46:39 schrieb Hong Yang:
> Hi,
>
> What is the difference between show and unpack when the function
> arguments are of ByteString type? I thought they are the same, but they
> are not interchangeable at compile time.
>
> Thanks,
>
> Hong

If you're using Data.ByteString, unpack has type

ByteString -> [Word8]

unpack and show are the same (perhaps excepting efficiency) if you use 
Data.ByteString.Char8
(ditto for Data.ByteString.Lazy[.Char8]).


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

Message: 4
Date: Fri, 05 Feb 2010 10:08:41 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] show vs. Data.ByteString.Char8.unpack
        for     ByteString
To: Hong Yang <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Hong,

'show' of a ByteString will put quotes around the text and lots of \ for
escaping certain characters, whereas unpack will translate each byte
one-to-one to a Char.


Steve

Hong Yang wrote:
> Hi,
> 
> What is the difference between show and unpack when the function
> arguments are of ByteString type? I thought they are the same, but they
> are not interchangeable at compile time.
> 
> Thanks,
> 
> Hong
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners


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

Message: 5
Date: Thu, 4 Feb 2010 17:14:33 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Typeclass problems
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

I've written a small type class that is meant to behave a bit like Show:

class Show a => Echo a where
  echo :: a -> String
  echoList :: [a] -> String
  echoListSep :: a -> String

  echo = show
  echoListSep _ = " "

  echoList [] = ""
  echoList [x] = echo x
  echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs


instance Echo Char where
  echo c = [c]
  echoListSep _ = ""

instance Echo a => Echo [a] where
  echo = echoList

instance Echo Int where
instance Echo Integer where
instance Echo Double where
instance Echo Float where


gen = map (const 1)

f xs = map echo $ gen xs


However the code doesn't compile on the last line.
But it does compile if I replace 'echo' with 'show'.
What's missing to make my typeclass behave like Show and accept input
of any type (besides all the missing instances...)?
I looked at the code for Show but I didn't see anything that looked "magical"...


Thanks,

Patrick


-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 6
Date: Fri, 05 Feb 2010 11:30:32 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] Typeclass problems
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Patrick,

I fixed it by adding a type signature to this line:

gen = map (const (1::Int))

The problem is that the literal '1' is of type Num t => t, which is
never tied to a concrete type anywhere.  It has to know the type
definitely before it can decide which type class to use.


Steve

Patrick LeBoutillier wrote:
> Hi all,
> 
> I've written a small type class that is meant to behave a bit like Show:
> 
> class Show a => Echo a where
>   echo :: a -> String
>   echoList :: [a] -> String
>   echoListSep :: a -> String
> 
>   echo = show
>   echoListSep _ = " "
> 
>   echoList [] = ""
>   echoList [x] = echo x
>   echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs
> 
> 
> instance Echo Char where
>   echo c = [c]
>   echoListSep _ = ""
> 
> instance Echo a => Echo [a] where
>   echo = echoList
> 
> instance Echo Int where
> instance Echo Integer where
> instance Echo Double where
> instance Echo Float where
> 
> 
> gen = map (const 1)
> 
> f xs = map echo $ gen xs
> 
> 
> However the code doesn't compile on the last line.
> But it does compile if I replace 'echo' with 'show'.
> What's missing to make my typeclass behave like Show and accept input
> of any type (besides all the missing instances...)?
> I looked at the code for Show but I didn't see anything that looked 
> "magical"...
> 
> 
> Thanks,
> 
> Patrick
> 
> 


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

Message: 7
Date: Thu, 4 Feb 2010 23:37:12 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Typeclass problems
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag 04 Februar 2010 23:14:33 schrieb Patrick LeBoutillier:
> Hi all,
>
> I've written a small type class that is meant to behave a bit like Show:
>
> class Show a => Echo a where
>   echo :: a -> String
>   echoList :: [a] -> String
>   echoListSep :: a -> String
>
>   echo = show
>   echoListSep _ = " "
>
>   echoList [] = ""
>   echoList [x] = echo x
>   echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs
>
>
> instance Echo Char where
>   echo c = [c]
>   echoListSep _ = ""
>
> instance Echo a => Echo [a] where
>   echo = echoList
>
> instance Echo Int where
> instance Echo Integer where
> instance Echo Double where
> instance Echo Float where
>
>
> gen = map (const 1)
>
> f xs = map echo $ gen xs
>
>
> However the code doesn't compile on the last line.
> But it does compile if I replace 'echo' with 'show'.
> What's missing to make my typeclass behave like Show and accept input
> of any type (besides all the missing instances...)?
> I looked at the code for Show but I didn't see anything that looked
> "magical"...

The "magic" is that Show is defined in the standard libraries.
Read http://haskell.org/onlinereport/decls.html#sect4.3.4
In particular, the last bullet point of

"In situations where an ambiguous type is discovered, an ambiguous type 
variable, v, is defaultable if:

    * v appears only in constraints of the form C v, where C is a class, 
and
    * at least one of these classes is a numeric class, (that is, Num or a 
subclass of Num), and
    * all of these classes are defined in the Prelude or a standard library 
(Figures 6.2--6.3, pages -- show the numeric classes, and Figure 6.1, page 
, shows the classes defined in the Prelude.) "


Now,

gen xs :: Num a => [a]

And you want to map echo over it, so we get the constraint
(Num a, Echo a).

Echo is not defined in the standard libraries, thus no defaulting takes 
place, you have to specify the type of 1 explicitly,

map echo (gen xs :: [Int])

should work.

In real programmes, the type is often deducible from the context, so you'll 
be bitten by the defaulting restrictions less often then than at the ghci 
prompt.

There's a proposal (or a couple) to change the defaulting rules in coming 
language standards, see e.g. http://hackage.haskell.org/trac/haskell-
prime/wiki/Defaulting


>
>
> Thanks,
>
> Patrick

HTH,
Daniel



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

Message: 8
Date: Thu, 4 Feb 2010 21:37:23 -0800 (PST)
From: Roger King <[email protected]>
Subject: [Haskell-beginners] Simulation of interconnect network
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

I am building a simulator for an interconnect network for a multiprocessor 
computer.  I would like to develop it in Haskell as an opportunity to learn 
Haskell.

The network will have a number of routers with input ports and output ports and 
crossbars between them.  I would like to simulate the protocol.  This would be 
an event driven simulator.  It would be at a high level, leaving out many 
details.  I would like it to be fast and be able to run it on several 
processors.

I would like to know if you have any advice.  Has anyone done this before?  Are 
there any discrete event simulators written in Haskell?
R



      


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

Message: 9
Date: Fri, 05 Feb 2010 10:42:03 +0100
From: Ulrik Rasmussen <[email protected]>
Subject: Re: [Haskell-beginners] Simulation of interconnect network
To: Roger King <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Roger King skrev:
> I am building a simulator for an interconnect network for a multiprocessor 
> computer.  I would like to develop it in Haskell as an opportunity to learn 
> Haskell.
>
> The network will have a number of routers with input ports and output ports 
> and crossbars between them.  I would like to simulate the protocol.  This 
> would be an event driven simulator.  It would be at a high level, leaving out 
> many details.  I would like it to be fast and be able to run it on several 
> processors.
>
> I would like to know if you have any advice.  Has anyone done this before?  
> Are there any discrete event simulators written in Haskell?
> R
>   
(Resubmussion: Forgot to send the mail to the list)

You could take a look at the CHP library: 
http://www.cs.kent.ac.uk/projects/ofa/chp/. The library is influenced by 
the CSP calculus, which is used to reason about systems of communicating 
processes. Using this, you could model each node of your network as a 
CHP process which communicates with the other nodes over channels. All 
the communication is synchronous, and the theory behind it is pretty 
solid: http://www.usingcsp.com/

/Ulrik


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

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


End of Beginners Digest, Vol 20, Issue 10
*****************************************

Reply via email to