
Hi, please write to the whole list, not just me. There are a lot of people around who can help you. MH wrote:
Rendel do you mind to explain to me how Container a = Many a (Container [a]) prevents user from creating an empty list? I did try the following: let a = Many "string" a :: Container [Char] -> Container [Char] let b = Many [] b :: forall a. Container [a] -> Container [a]
but I can not understand what is the difference.
You cannot prevent users from creating an empty list, because lists are already defined in the Haskell prelude, and you cannot change that definition. But you can define your own datatype Container which prevents users from creating empty containers. Your datatype data Container a = Many a (Container [a]) is quite complicated, because you have (Container a) on the left hand side of the equation, but (Container [a]) on the right hand side. Is that on purpose? Anyway, your Container has the property that a (Container a) is never empty, because it always contains at least one value of type a, namely the first parameter of Many. To see that, we can write a function which extracts that element: getContainerElement :: Container a -> a getContainerElement (Many element something) = element It is not possible to write such a function for ordinary lists without calling error or entering a nonterminating loop: getListElement :: [a] -> a getListElement (element : something) = element getListElement [] = error "no element around :(" So ordinary lists can be empty, but your Containers can't. Tillmann

I actually meant
data Container a = Many a(Container a)
but here is what I don't understand (fyi, I am a beginner) how can you
construct this container? I can do
let a = Many "somestring" - and I will get back a function but I can not do
let a = Many 'a' "somestring" - because the second param is not
(Container a) type.
let a = Many 'a' (Container ['a','a']) - doesn't work either because
Container is a type not a constructor (right or I am missing
something?).
So I have two questions:
1. When I do
let b = Many "somestring" , I get
:t b
b :: Container[Char] -> Container[Char]
what is it and why I am allowed to pass just one parameter to Many
(and how can I use it)?
2. How can you construct that container?
data Container a = Many a(Container a)
let a = ????
On Fri, Jun 5, 2009 at 4:01 PM, Tillmann Rendel
Hi,
please write to the whole list, not just me. There are a lot of people around who can help you.
MH wrote:
Rendel do you mind to explain to me how Container a = Many a (Container [a]) prevents user from creating an empty list? I did try the following: let a = Many "string" a :: Container [Char] -> Container [Char] let b = Many [] b :: forall a. Container [a] -> Container [a]
but I can not understand what is the difference.
You cannot prevent users from creating an empty list, because lists are already defined in the Haskell prelude, and you cannot change that definition. But you can define your own datatype Container which prevents users from creating empty containers.
Your datatype
data Container a = Many a (Container [a])
is quite complicated, because you have (Container a) on the left hand side of the equation, but (Container [a]) on the right hand side. Is that on purpose?
Anyway, your Container has the property that a (Container a) is never empty, because it always contains at least one value of type a, namely the first parameter of Many. To see that, we can write a function which extracts that element:
getContainerElement :: Container a -> a getContainerElement (Many element something) = element
It is not possible to write such a function for ordinary lists without calling error or entering a nonterminating loop:
getListElement :: [a] -> a getListElement (element : something) = element getListElement [] = error "no element around :("
So ordinary lists can be empty, but your Containers can't.
Tillmann

MH
data Container a = Many a(Container a)
but here is what I don't understand (fyi, I am a beginner) how can you construct this container? I can do
let a = Many "somestring" - and I will get back a function but I can not do let a = Many 'a' "somestring" - because the second param is not (Container a) type.
Right, so you need to pass a 'Container a' for that parameter.
let a = Many 'a' (Container ['a','a']) - doesn't work either because Container is a type not a constructor (right or I am missing something?).
It doesn't work because the Container parameter must be of the same type. The parameters here are Char (due to the 'a' as the first parameter to Many), and [Char] (due to the ['a','a'] given as parameter to the Container parameter. These are not the same, so it fails.
So I have two questions: 1. When I do let b = Many "somestring" , I get :t b b :: Container[Char] -> Container[Char] what is it and why I am allowed to pass just one parameter to Many (and how can I use it)?
This is just partial application. Pass one more parameter to build the whole thing.
2. How can you construct that container? data Container a = Many a(Container a) let a = ????
Well you need a 'Container a' to do it. So you can do for instance: let a = Container 'x' a or let b = Conainer 'y' undefined -k -- If I haven't seen further, it is by standing in the footprints of giants

On Fri, Jun 5, 2009 at 2:58 PM, MH
I actually meant
data Container a = Many a(Container a)
but here is what I don't understand (fyi, I am a beginner) how can you construct this container? I can do
I think I saw the above definition described as a coalgebra or codata: http://blog.sigfpe.com/2007/07/data-and-codata.html Basically, that list has to always be infinite.
let a = Many "somestring" - and I will get back a function but I can not do let a = Many 'a' "somestring" - because the second param is not (Container a) type. let a = Many 'a' (Container ['a','a']) - doesn't work either because Container is a type not a constructor (right or I am missing something?).
So I have two questions: 1. When I do let b = Many "somestring" , I get :t b b :: Container[Char] -> Container[Char] what is it and why I am allowed to pass just one parameter to Many (and how can I use it)?
2. How can you construct that container? data Container a = Many a(Container a) let a = ????
Check that link above for answers to some of your questions. I think that basically you want a different type: data NonEmptyList a = JustOne a | MoreThanOne a (NonEmptyList a) This is just the normal list definition with a small change, if you think of the normal list as being defined in this (pseudo) code: data [a] = [] | a : [a] I've just made it so that the "empty" list has at least one. I hope that helps, Jason

On Fri, Jun 5, 2009 at 4:13 PM, Jason Dagit
On Fri, Jun 5, 2009 at 2:58 PM, MH
wrote: I actually meant
data Container a = Many a(Container a)
but here is what I don't understand (fyi, I am a beginner) how can you construct this container? I can do
I think I saw the above definition described as a coalgebra or codata: http://blog.sigfpe.com/2007/07/data-and-codata.html
Basically, that list has to always be infinite.
let a = Many "somestring" - and I will get back a function but I can not do let a = Many 'a' "somestring" - because the second param is not (Container a) type. let a = Many 'a' (Container ['a','a']) - doesn't work either because Container is a type not a constructor (right or I am missing something?).
So I have two questions: 1. When I do let b = Many "somestring" , I get :t b b :: Container[Char] -> Container[Char] what is it and why I am allowed to pass just one parameter to Many (and how can I use it)?
2. How can you construct that container? data Container a = Many a(Container a) let a = ????
Check that link above for answers to some of your questions.
I think that basically you want a different type: data NonEmptyList a = JustOne a | MoreThanOne a (NonEmptyList a)
I am still not sure why so much attention is being given to the recursive version. I find the most elegant and useful to be this simple one: data NonEmpty a = NonEmpty a [a] With the obligatory conversions: toList (NonEmpty x xs) = x:xs fromList [] = Nothing fromList (x:xs) = Just (NonEmpty x xs) Luke
participants (5)
-
Jason Dagit
-
Ketil Malde
-
Luke Palmer
-
MH
-
Tillmann Rendel