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: Overlapping Instances (John Smith)
2. Re: [Haskell-cafe] running and understanding a lifting
program (Patrick LeBoutillier)
3. Re: Re: Overlapping Instances (Markus L?ll)
4. Re: Re: Overlapping Instances (J?rgen Doser)
5. Re: [Haskell-cafe] running and understanding a lifting
program (Patrick LeBoutillier)
6. Please, help me for myFirstCode (Sok H. Chang)
----------------------------------------------------------------------
Message: 1
Date: Mon, 25 Oct 2010 22:00:34 +0200
From: John Smith <[email protected]>
Subject: [Haskell-beginners] Re: Overlapping Instances
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Thank you. Why does this code succeed for a, but stills fails on instance Show
B? Do they not both invoke the same Show A?
{-# LANGUAGE TypeSynonymInstances, OverlappingInstances, IncoherentInstances #-}
import Data.Array
type A = Array Int Bool
data B = B A
instance Show A where
show a = "foo"
instance Show B where
show (B a) = show a
a = show $ listArray (1,3) $ repeat True
On 24/10/2010 16:37, Markus Läll wrote:
> Hi John,
>
> from what I gather this is because Show instance for "Array a b",
> which you are overlapping, is defined in a module without the
> OverlappingInstances declaration. Check the last few paragraphs of
> this from the GHC's user's guide:
>
> http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/type-class-extensions.html#instance-overlap
>
>
> Markus Läll
>
> On Sun, Oct 24, 2010 at 4:54 PM, John Smith<[email protected]> wrote:
>> Following is a simplification of some code which I have written. I have an
>> overlapping Show instance for A, which is more specific than the general
>> instance for arrays, so I would expect it to be acceptable as an overlapping
>> instance. Nevertheless, I get the following compiler error:
>>
>> Overlapping instances for Show A
>> arising from a use of `show' at 13:17-22
>> Matching instances:
>> instance (Ix a, Show a, Show b) => Show (Array a b)
>> -- Defined in GHC.Arr
>> instance [overlap ok] Show A -- Defined at 9:9-14
>> In the expression: show a
>> In the definition of `show': show (B a) = show a
>> In the instance declaration for `Show B'
>> Compilation failed.
>>
>> I've tried UndecidableInstances and IncoherentInstances, but they don't seem
>> to help. What am I doing wrong?
>>
>> Many thanks in advance for any assistance.
>>
>> -John
>>
>> {-# LANGUAGE TypeSynonymInstances, OverlappingInstances #-}
>>
>> import Data.Array
>>
>> type A = Array Int Bool
>>
>> data B = B A
>>
>> instance Show A where
>> show a = "foo"
>>
>> instance Show B where
>> show (B a) = show a
------------------------------
Message: 2
Date: Mon, 25 Oct 2010 16:58:28 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] running and
understanding a lifting program
To: Patrick Browne <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Patrick,
I found this program interesting and decided to spend a bit of time on
it, even though I'm still a newbie.
I did a few things to simplify the code, here are some comments:
1) I chose to rename the arithmetic functions in the Number class
instead of trying to overload the "real" ones, I'm not that good at
Haskell yet...
2) The program had some errors, namely I think the definition of the
Point type should be:
data Point a = Point a a
to allow for different types of Points.
3) The Points class seemed useless in the end, I simply defined the
dist function at the top level.
4) If you import Control.Monad, it makes functions (and therefore
"Changing v") into a Monad (maybe my terminology is off here...) and
allows you to use the general liftM and liftM2 lifting functions
instead of defining your own.
Here's the complete program:
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
data Point a = Point { x ::a, y :: a }
type Time = Float
-- Functor Changing, which adds time parameter t to its input value.
-- For example, Changing Float indicates a changing floating number
-- (i.e. a function of time).
type Changing v = Time -> v
-- Lifting functions
lift1 op a = \t -> op (a t)
lift2 op a b = \t -> op (a t) (b t)
class Number a where
add, sub, mul :: a -> a -> a
square, squareRoot :: a -> a
square a = a `mul` a
instance Number Float where
add = (+)
sub = (-)
mul = (*)
squareRoot = sqrt
instance Number (Changing Float) where
add = lift2 add
sub = lift2 sub
mul = lift2 mul
squareRoot = lift1 squareRoot
-- The distance operation is defined as follow
dist :: Number a => Point a -> Point a -> a
dist a b = squareRoot $ square((x a) `sub` (x b)) `add` square ((y a)
`sub` (y b))
-- Running the code
-- If p1 and p2 are two 2D static points,
-- their distance d is calculated as follows:
p1, p2 :: Point Float
p1 = Point 3.4 5.5
p2 = Point 4.5 4.5
-- distance between p1 and p2 --> 1.55
d = dist p1 p2
-- For 2D moving points mp1 and mp2, their distance md,
-- which is a function of time, is calculated as follows:
mp1, mp2 :: Point (Changing Float)
mp1 = Point (\t -> 4.0 + 0.5 * t) (\t -> 4.0 - 0.5 * t)
mp2 = Point (\t -> 0.0 + 1.0 * t) (\t -> 0.0 - 1.0 * t)
-- distance between mp1 and mp2
md = dist mp1 mp2
-- distance md for time 2 ----> 5.83
>
>
> This message has been scanned for content and viruses by the DIT Information
> Services E-Mail Scanning Service, and is believed to be clean.
> http://www.dit.ie
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
Patrick
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 3
Date: Tue, 26 Oct 2010 00:09:47 +0300
From: Markus L?ll <[email protected]>
Subject: Re: [Haskell-beginners] Re: Overlapping Instances
To: John Smith <[email protected]>, [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I think the IncoherentInstances doesn't solve the problem. It does
also not seem to be the problem (just) of Show B -- the overlap-error
arises whenever you actually try to use show on "Array Int Bool".
Like, when you specifiy the type of a as:
> a = show $ listArray (1,3 :: Int) $ repeat True
even when the Show B instance is commented out, you get the same error...
I can't exactly tell, because I'm quite new for Haskell myself, but it
might be, that TypeSynonymInstances works only for those cases, where:
- no Show instance for that type exists, or
- a Show instance for that type exists, but the module where it was
defined also uses the OverlappingInstances flag (try it with a custom
type!)
The end of the GHC docs section also says, that this choice of how
things happen is open for dispute, but it seems it has not came up
recently enouugh to change it.
With the two ways to implement it, right now we have the promise, that
when you define an instance for a type you created, it cannot be
overlapped by others (unless wrapped in a newtype). But when the
system is changed, the user user of some module gets to create more
specific instances for types in that module.
--
Markus Läll
On Mon, Oct 25, 2010 at 11:00 PM, John Smith <[email protected]> wrote:
> Thank you. Why does this code succeed for a, but stills fails on instance
> Show B? Do they not both invoke the same Show A?
>
> {-# LANGUAGE TypeSynonymInstances, OverlappingInstances, IncoherentInstances
> #-}
>
> import Data.Array
>
> type A = Array Int Bool
>
> data B = B A
>
> instance Show A where
> show a = "foo"
>
> instance Show B where
> show (B a) = show a
>
> a = show $ listArray (1,3) $ repeat True
>
> On 24/10/2010 16:37, Markus Läll wrote:
>>
>> Hi John,
>>
>> from what I gather this is because Show instance for "Array a b",
>> which you are overlapping, is defined in a module without the
>> OverlappingInstances declaration. Check the last few paragraphs of
>> this from the GHC's user's guide:
>>
>>
>> http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/type-class-extensions.html#instance-overlap
>>
>>
>> Markus Läll
>>
>> On Sun, Oct 24, 2010 at 4:54 PM, John Smith<[email protected]>
>> wrote:
>>>
>>> Following is a simplification of some code which I have written. I have
>>> an
>>> overlapping Show instance for A, which is more specific than the general
>>> instance for arrays, so I would expect it to be acceptable as an
>>> overlapping
>>> instance. Nevertheless, I get the following compiler error:
>>>
>>> Overlapping instances for Show A
>>> arising from a use of `show' at 13:17-22
>>> Matching instances:
>>> instance (Ix a, Show a, Show b) => Show (Array a b)
>>> -- Defined in GHC.Arr
>>> instance [overlap ok] Show A -- Defined at 9:9-14
>>> In the expression: show a
>>> In the definition of `show': show (B a) = show a
>>> In the instance declaration for `Show B'
>>> Compilation failed.
>>>
>>> I've tried UndecidableInstances and IncoherentInstances, but they don't
>>> seem
>>> to help. What am I doing wrong?
>>>
>>> Many thanks in advance for any assistance.
>>>
>>> -John
>>>
>>> {-# LANGUAGE TypeSynonymInstances, OverlappingInstances #-}
>>>
>>> import Data.Array
>>>
>>> type A = Array Int Bool
>>>
>>> data B = B A
>>>
>>> instance Show A where
>>> show a = "foo"
>>>
>>> instance Show B where
>>> show (B a) = show a
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 4
Date: Mon, 25 Oct 2010 23:39:23 +0200
From: J?rgen Doser <[email protected]>
Subject: Re: [Haskell-beginners] Re: Overlapping Instances
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="UTF-8"
El lun, 25-10-2010 a las 22:00 +0200, John Smith escribió:
> Thank you. Why does this code succeed for a, but stills fails on instance
> Show B? Do they not both invoke the same Show A?
No, a doesn't use the instance for Array Int Bool. The (1,3) get
defaulted to Integers. The following code will complain, though:
> {-# LANGUAGE TypeSynonymInstances, OverlappingInstances, IncoherentInstances
> #-}
>
> import Data.Array
>
> type A = Array Int Bool
>
> data B = B A
>
> instance Show A where
> show a = "foo"
>
> a = show $ listArray (1::Int,3) $ repeat True
------------------------------
Message: 5
Date: Mon, 25 Oct 2010 18:30:28 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] running and
understanding a lifting program
To: Patrick Browne <[email protected]>
Cc: beginners <[email protected]>, Haskell Cafe
<[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Patrick,
On Mon, Oct 25, 2010 at 6:12 PM, Patrick Browne <[email protected]> wrote:
> Patrick,
> Thanks for taking the time to get the program running.
> It seems fine, but I cannot get the *md* to print out, probably missing
> the Show class somewhere.
"md" is a function to which you have to give a Time value, basically
the Time at which you want to evaluate the Changing points' positions.
Try "md 2" in ghci, it should give you the expected value.
Patrick
>
> Thanks again,
> Pat
>
>
> Patrick LeBoutillier wrote:
>> Patrick,
>>
>> I found this program interesting and decided to spend a bit of time on
>> it, even though I'm still a newbie.
>> I did a few things to simplify the code, here are some comments:
>>
>> 1) I chose to rename the arithmetic functions in the Number class
>> instead of trying to overload the "real" ones, I'm not that good at
>> Haskell yet...
>>
>> 2) The program had some errors, namely I think the definition of the
>> Point type should be:
>>
>> data Point a = Point a a
>>
>> to allow for different types of Points.
>>
>> 3) The Points class seemed useless in the end, I simply defined the
>> dist function at the top level.
>>
>> 4) If you import Control.Monad, it makes functions (and therefore
>> "Changing v") into a Monad (maybe my terminology is off here...) and
>> allows you to use the general liftM and liftM2 lifting functions
>> instead of defining your own.
>>
>>
>> Here's the complete program:
>>
>> {-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
>>
>> data Point a = Point { x ::a, y :: a }
>> type Time = Float
>>
>> -- Functor Changing, which adds time parameter t to its input value.
>> -- For example, Changing Float indicates a changing floating number
>> -- (i.e. a function of time).
>> type Changing v = Time -> v
>>
>> -- Lifting functions
>> lift1 op a = \t -> op (a t)
>> lift2 op a b = \t -> op (a t) (b t)
>>
>> class Number a where
>> add, sub, mul :: a -> a -> a
>> square, squareRoot :: a -> a
>> square a = a `mul` a
>>
>> instance Number Float where
>> add = (+)
>> sub = (-)
>> mul = (*)
>> squareRoot = sqrt
>>
>> instance Number (Changing Float) where
>> add = lift2 add
>> sub = lift2 sub
>> mul = lift2 mul
>> squareRoot = lift1 squareRoot
>>
>> -- The distance operation is defined as follow
>> dist :: Number a => Point a -> Point a -> a
>> dist a b = squareRoot $ square((x a) `sub` (x b)) `add` square ((y a)
>> `sub` (y b))
>>
>> -- Running the code
>> -- If p1 and p2 are two 2D static points,
>> -- their distance d is calculated as follows:
>> p1, p2 :: Point Float
>> p1 = Point 3.4 5.5
>> p2 = Point 4.5 4.5
>>
>> -- distance between p1 and p2 --> 1.55
>> d = dist p1 p2
>>
>> -- For 2D moving points mp1 and mp2, their distance md,
>> -- which is a function of time, is calculated as follows:
>> mp1, mp2 :: Point (Changing Float)
>> mp1 = Point (\t -> 4.0 + 0.5 * t) (\t -> 4.0 - 0.5 * t)
>> mp2 = Point (\t -> 0.0 + 1.0 * t) (\t -> 0.0 - 1.0 * t)
>> -- distance between mp1 and mp2
>> md = dist mp1 mp2
>> -- distance md for time 2 ----> 5.83
>>
>>
>>>
>>> This message has been scanned for content and viruses by the DIT
>>> Information Services E-Mail Scanning Service, and is believed to be clean.
>>> http://www.dit.ie
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>
>>
>> Patrick
>>
>
>
> This message has been scanned for content and viruses by the DIT Information
> Services E-Mail Scanning Service, and is believed to be clean.
> http://www.dit.ie
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 6
Date: Tue, 26 Oct 2010 15:36:58 +0900
From: "Sok H. Chang" <[email protected]>
Subject: [Haskell-beginners] Please, help me for myFirstCode
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Hello, everyone.
I need help for my first code.
I write this code for extracting one sentence from text file.
The code is
-----------------------------------------------------------------------------------------------------------------------
import System
import System.Random
main = do myHeader <- readFile "C:\\Documents and Settings\\myHeader.txt"
putStrLn $ myExtractSentence myHeader
myExtractSentence file = ary !! ranNoInt
where
ranNoInt =<< ranNo -- *mark
ranNo = randomRIO (0, n-1)
n = length ary
ary = lines file
----------------------------------------------------------------------------------------------------------------------
There are two questions.
1) I've got following error message.
Haskell>ghc --make Test01.hs -o Test
[1 of 1] Compiling Main ( Test01.hs, Test01.o )
Test01.hs:10:9: parse error (possibly incorrect indentation)
How can fix it?
2) Is it OK at "*mark" line?
Thank you.
S. CHANG
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101026/488cad53/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 28, Issue 38
*****************************************