
I wonder, is there an equivalent of the 'type' keyword for constructors? An example: -- create a pseudo-C pointer type -- which can point to a value or a -- null. type Pointer a = Maybe a -- int a = 3; -- int *pa = &a; ampersand :: t -> Pointer t ampersand a = Just a -- int b = *pa. star :: Pointer a -> a star (Just a) = a -- note this function behaves -- in an 'authentic' fashion ;-) To really complete the illusion it would be nice to replace the names Just and Nothing with PointerTo and Null. Then the constructors would really mean something. Is there a solution? -- Dougal Stanton dougal@dougalstanton.net // http://www.dougalstanton.net

On 13/11/2007, Henning Thielemann
On Tue, 13 Nov 2007, Dougal Stanton wrote:
-- int a = 3; -- int *pa = &a; ampersand :: t -> Pointer t ampersand a = Just a
What's bad about using 'ampersand' function as replacement for the constructor 'Just'?
I also wanted to use it in pattern matching but have the advantage of all the stuff already written for Maybe. (Note this was an example I made up on the spur of the moment anyway.) I'd often thought it would be nice when adapting standard data structures for specific programs. Like, data Tree a = Leaf a | Branch a [Tree a] data FS a = File a | Folder a [FS a] Once you've got one it would be nice to repurpose it with more appropriate names. Just an idea! :-) D. -- Dougal Stanton dougal@dougalstanton.net // http://www.dougalstanton.net

On Tue, 13 Nov 2007, Dougal Stanton wrote:
On 13/11/2007, Henning Thielemann
wrote: On Tue, 13 Nov 2007, Dougal Stanton wrote:
-- int a = 3; -- int *pa = &a; ampersand :: t -> Pointer t ampersand a = Just a
What's bad about using 'ampersand' function as replacement for the constructor 'Just'?
I also wanted to use it in pattern matching but have the advantage of all the stuff already written for Maybe.
No problem, write a function like 'maybe' to inspect the data. Instead of 'f m' with f :: Maybe T -> S f (Just x) = g x f Nothing = h you write maybe h g m

Henning Thielemann wrote:
On Tue, 13 Nov 2007, Dougal Stanton wrote:
On 13/11/2007, Henning Thielemann
wrote: On Tue, 13 Nov 2007, Dougal Stanton wrote:
-- int a = 3; -- int *pa = &a; ampersand :: t -> Pointer t ampersand a = Just a What's bad about using 'ampersand' function as replacement for the constructor 'Just'? I also wanted to use it in pattern matching but have the advantage of all the stuff already written for Maybe.
No problem, write a function like 'maybe' to inspect the data.
Instead of 'f m' with f :: Maybe T -> S f (Just x) = g x f Nothing = h
Yes. It is a problem. Do you write all your code using higher-order functions, never matching explicitly on constructors? I don't. Matching explicitly on constructors is an elegant and easy-to-read way to write programs. It's annoying to have to choose between (a) nicely named constructors and (b) being able to re-use library functions defined for Maybe. Jules

On Wed, 14 Nov 2007, Jules Bean wrote:
Henning Thielemann wrote:
No problem, write a function like 'maybe' to inspect the data.
Instead of 'f m' with f :: Maybe T -> S f (Just x) = g x f Nothing = h
Yes. It is a problem.
Do you write all your code using higher-order functions, never matching explicitly on constructors? I don't.
I do it more and more, because it let me switch the underlying data structure more easily.

On Tue, Nov 13, 2007 at 11:44:30PM +0100, Henning Thielemann wrote:
On Tue, 13 Nov 2007, Dougal Stanton wrote:
-- int a = 3; -- int *pa = &a; ampersand :: t -> Pointer t ampersand a = Just a
What's bad about using 'ampersand' function as replacement for the constructor 'Just'?
It wouldn't work in pattern matching. -- David Roundy Department of Physics Oregon State University

Dougal Stanton wrote:
I wonder, is there an equivalent of the 'type' keyword for constructors? An example:
-- create a pseudo-C pointer type -- which can point to a value or a -- null. type Pointer a = Maybe a
-- int a = 3; -- int *pa = &a; ampersand :: t -> Pointer t ampersand a = Just a
-- int b = *pa. star :: Pointer a -> a star (Just a) = a -- note this function behaves -- in an 'authentic' fashion ;-)
To really complete the illusion it would be nice to replace the names Just and Nothing with PointerTo and Null. Then the constructors would really mean something. Is there a solution?
The thing you want is called "views". See http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns#Relatedwork for more. Regards, apfelmus
participants (5)
-
apfelmus
-
David Roundy
-
Dougal Stanton
-
Henning Thielemann
-
Jules Bean