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: 'case of' question (Manfred Lotz)
2. Re: Would you shed light on "undefined values" please?
(Ertugrul Soeylemez)
3. Re: Would you shed light on "undefined values" please?
(Heinrich Apfelmus)
4. About repeat function (divyanshu ranjan)
5. Re: About repeat function (Christopher Done)
6. Re: About repeat function (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Fri, 24 Jun 2011 05:23:24 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] 'case of' question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, 23 Jun 2011 11:32:50 -0400
Brandon Allbery <[email protected]> wrote:
> On Thu, Jun 23, 2011 at 11:29, Brandon Allbery <[email protected]>
> wrote:
> > On Thu, Jun 23, 2011 at 11:02, Manfred Lotz <[email protected]>
> > wrote:
> >>
> >> The only idea I got is:
> >>
> >> processX x
> >> ?| x `elem` [ 1, 5, 76 ] = doThis
> >> ?| x `elem` [ 112, 21 ] = doThat
> >> ?| otherwise = doOtherwise
> >
> > That's the correct way to do it.? The trick is to remember that the
> > above is really a "case of":
>
> (with the cat off my arm so I'm not whacking the wrong keys...)
>
:-)
> > case x of
> > _ | x `elem` [1, 5, 76] -> doThis
> > | x `elem` [112, 21] -> doThat
> > | otherwise -> doSomethingElse
>
Thanks. That is interesting.
--
Manfred
------------------------------
Message: 2
Date: Fri, 24 Jun 2011 08:13:26 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] Would you shed light on "undefined
values" please?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Brandon Allbery <[email protected]> wrote:
> The reason _|_ is significant is that a type that admits it as a value
> can be computed lazily. If your type does not allow for _|_ then it
> is strict;
That's actually not quite right. Strictness and bottom are only loosely
related in that strictness is defined in terms of bottom. You get a
concept of strictness once you have functions (value constructors with
arguments are functions), otherwise it doesn't make much sense. They
are related in that some expressions can yield defined values in
nonstrict semantics, where they would give you bottom in strict ones,
namely in this specific case:
f ? ? ?
In strict semantics this is impossible, because this is definitely a
nonstrict function.
Bottom arises, as soon as a language is total. This is related to the
halting problem. Bottom is the value, which is never computed in the
sense that it never results. Only nontotal languages (like Agda) can
prevent bottom values from appearing.
In other words, every type in Haskell is lifted and there is no way
around that. The type of 'undefined' proves that. In fact you cannot
express the undefined value in Agda. On the other hand in Haskell it's
trivial to express:
undefined :: a
undefined = undefined
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
------------------------------
Message: 3
Date: Fri, 24 Jun 2011 09:59:58 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: Re: [Haskell-beginners] Would you shed light on "undefined
values" please?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Costello, Roger L. wrote:
> In this book [1] the author defines the term "bottom":
>
> In order that we can say that, without exception, every
> syntactically well-formed expression denotes a value,
> it is convenient to introduce a special symbol (upside
> down T), pronounced 'bottom', to stand for the undefined
> value of a particular type. In particular, the value of
> infinity is the undefined value (bottom) of type Integer,
> and 1/0 is the undefined value (bottom) of type Float.
> Hence we can assert that 1/0 = bottom.
>
> He defines infinity as this:
>
> infinity :: Integer
> infinity = infinity + 1
>
> The author says this when discussing the Bool datatype:
>
> It follows that there are not two but three Boolean
> values, namely False, True, and bottom. In fact, every
> datatype declaration introduces an extra anonymous
> value, the undefined value of the datatype.
>
> What is the undefined value (bottom) of type Bool?
>
> What is the undefined value (bottom) of type String?
>
> If I create my own datatype:
>
> data MyBool = F | T
>
> What is the undefined value (bottom) of type MyBool?
>
> I am not clear why "bottom" is an important concept. Would you explain please?
Bottom, _|_, is a little trick to do precisely what Bird said, namely
"make every expression denote a value".
As you know, in Haskell, you can reason about programs by comparing
their values. For instance, we have
4+5 = 10-1 = length [0..8] = sum (replicate 3 3)
because all these expressions denote the value 9, a number. But there
are expressions that should be numbers, yet aren't. Examples:
sum [1..]
let loop = 2-loop in loop
What value do they have? Certainly not 1 or 5 or some other number. We
do want them to have a value, though, so we have to invent one: _|_ .
More precisely, every expression that does not terminate denotes the
value _|_.
(Actually, we have to invent one for every type: _|_ :: String, _|_ ::
Bool, _|_ :: MyBool, etc. because we can only compare values of the same
type.)
The rules for calculating with _|_ are presented here:
http://en.wikibooks.org/wiki/Haskell/Denotational_semantics
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 4
Date: Fri, 24 Jun 2011 14:01:26 +0530
From: divyanshu ranjan <[email protected]>
Subject: [Haskell-beginners] About repeat function
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
As i was going through Learn You a Haskell for Great Good , i came across
following repeat function implementation :
repeat' :: x - > [x]
repeat' x = x : repeat' x
My question is why it produces list in first place. 1:2:3 is not a list but
1:2:3:[] is. How comes haskell know that the things which we are adding will
eventually added to the beginning of empty list given things are infinite
so you can specify the end which is [] . Then why doing 1:3:4 is not
acceptable ?
Thanks
Divyanshu Ranjan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110624/8efa47d4/attachment-0001.htm>
------------------------------
Message: 5
Date: Fri, 24 Jun 2011 10:56:33 +0200
From: Christopher Done <[email protected]>
Subject: Re: [Haskell-beginners] About repeat function
To: divyanshu ranjan <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On 24 June 2011 10:31, divyanshu ranjan <[email protected]> wrote:
> My question is why it produces list in first place. 1:2:3 is not a list but
> 1:2:3:[] is. How comes haskell know that the things which we are adding will
> eventually added to the beginning of ?empty list given things are infinite
> so you can specify the end which is [] . Then why doing 1:3:4 is not
> acceptable ?
It's quite simple if you consider that it's just based on types of
expressions and nothing more.
Consider the type of the (:) function:
(:) :: x -> [x] -> [x]
It takes x and list of x. So we can write:
() : []
And the expression () : [] has type [()]
Therefore we can also take that and put it on the right-hand side again:
() : (() : [])
See how () and (() : []) fit into the x and [x] places in the (:) function?
Continuing:
() : (() : (() : []))
And so on. The (:) function is right-associative (meaning x : b : c :
d means x : (b : (c : d))). So we don't need the parentheses, the
following is equivalent to the previous:
() : () : () : []
The a:b:c syntax isn't special in any way. But you should always
implicitly read it as (a:(b:c)), remembering that everything on the
left-hand side of a : is type x, and everything on right-hand side is
[x].
repeat' x has type [x], so you can write x : repeat x
E.g. consider manually evaluating repeat () on paper, we'd write out:
repeat ()
() : repeat ()
() : () : repeat ()
() : () : () : repeat ()
And so on. It will go on forever if you keep evaluating the end
expression. Haskell is lazy, so we don't have to evaluate the
right-hand side if we don't want use it. But the type of the
expressions, you should observe, are always correct.
The reason 1:3:4 is not acceptable is because 4 is not of type [x],
whereas 1:3:repeat 4 would be because repeat 4 is of type [x].
Ciao!
------------------------------
Message: 6
Date: Fri, 24 Jun 2011 11:08:40 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] About repeat function
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Friday 24 June 2011, 10:31:26, divyanshu ranjan wrote:
> As i was going through Learn You a Haskell for Great Good , i came
> across following repeat function implementation :
> repeat' :: x - > [x]
> repeat' x = x : repeat' x
> My question is why it produces list in first place. 1:2:3 is not a list
> but 1:2:3:[] is. How comes haskell know that the things which we are
> adding will eventually added to the beginning of empty list given
> things are infinite
Well, since things are infinite here, nothing will ever be consed to an
empty list. In this case, everything is consed to a nonempty list.
In general, a list needs not end with [], it could also end with _|_
(bottom, undefined), 1:2:3:_|_ is a valid list, it will lead to an
error/nontermination if you ever ask what comes after the 3, but if you
never do that, it will work (e.g. (1:2:3:_|_) !! 2 ~> 3).
The construction of repeat' goes
repeat' x = x : something
(where something = repeat' x, but all we need of that for the moment is
that is has the correct type), so by
(:) :: a -> [a] -> [a],
repeat' x produces a list (if the something is a list, but something is
repeat' x, which is a list, provided that something is a list, which ...
So the type checker can't prove that it is a list in a finite number of
deduction steps; however, it can prove that it can't be anything other than
a list, and the assumption that it is a list is consistent, so it is
accepted.), the first element of which is x.
As long as you don't check whether the resulting list contains more than
one element, nothing further is done. If you ask for further elements, the
definition is entered again, each time delivering a further list element.
You might look at it as a limit process,
step 0: unknown
step 1: x:unknown
step 2: x:x:unknown
step 3: x:x:x:unknown
...
An equivalent (but possibly more efficient) definition is
repeat'' x = let result = x : result in result
> so you can specify the end which is [] .
It is not for infinite lists, you could say that infinite lists end in _|_
or that they have no end, whichever you prefer (but if you say they end in
_|_, be aware that you can never reach that end).
> Then why doing 1:3:4 is not acceptable ?
Because it's finite, so it has a reachable end.
The end is 4, which (in general) is not a list, but a number.
(If you have e.g. an instance Num [Int] where ... in scope, the 4 at the
end is a valid list, whatever fromInteger does for that instance.)
>
> Thanks
> Divyanshu Ranjan
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 60
*****************************************