[Haskell-cafe] Data constructor synonyms

2011-03-18 Thread Grigory Sarnitskiy
Hello! Assume I have a type MyType with the constructor GeneralConstructor:

data MyType = GeneralConstructor [Double]

but I also want to have a separate name for special case of this constructor:

SpecialConstructor Double

so SpecialConstructor a = GeneralConstructor (a:[]) that is

SpecialConstructor 5 was exactly the same as GeneralConstructor [5].

And for example instead of writing GeneralConstructor [0] I would like to use 
constructor Zero. It's all just for convenience.

How can I achieve this? Well, of course I always can use sed to replace 
SpecialConstructor 5 with  GeneralConstructor [5] in program sources, but it's 
not convenient.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data constructor synonyms

2011-03-18 Thread Roel van Dijk
Remember that constructors are functions, except that you can't
pattern match against them.

 data MyType = GeneralConstructor [Double]

 -- GeneralConstructor :: [Double] - MyType

Note the lower case character, just a plain function:
 specialConstructor :: Double - MyType
 specialConstructor a = GeneralConstructor (a:[])

 zero :: MyType
 zero = GeneralConstructor [0]

The downside is that you can't pattern-match against these functions.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data constructor synonyms

2011-03-18 Thread Grigory Sarnitskiy
18.03.2011, 14:22, Roel van Dijk vandijk.r...@gmail.com;:

  Remember that constructors are functions, except that you can't
  pattern match against them.

..

  The downside is that you can't pattern-match against these functions.

The thing is that I need pattern matching, just functions won't do.

Anyway, a new question arose.  If I have already declared a type, can I add new 
constructors to it from other modules?

Maybe there are some GHC extensions to solve both these problems.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data constructor synonyms

2011-03-18 Thread Stephen Tetley
On 18 March 2011 13:31, Grigory Sarnitskiy sargrig...@ya.ru wrote:

 Anyway, a new question arose.  If I have already declared a type, can I add 
 new constructors to it from other modules?

 Maybe there are some GHC extensions to solve both these problems.

no can do.

There are ways to encode extensible types (e.g the finally tagless
sytle), but on balance you are better to design extensibility for
functions - easy to add more functions - than make your code much more
complicated so it can be extensible for types.

Parser combinators and pretty print combinators are great and largely
simple examples of extensibility with functions.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data constructor synonyms

2011-03-18 Thread Andres Loeh
  Remember that constructors are functions, except that you can't
  pattern match against them.

 ..

  The downside is that you can't pattern-match against these functions.

 The thing is that I need pattern matching, just functions won't do.

It's only a preprocessor, but Conor's she allows pattern synonyms:

http://personal.cis.strath.ac.uk/~conor/pub/she

 Anyway, a new question arose.  If I have already declared a type, can I add 
 new constructors to it from other modules?

Again, not within Haskell itself. she also has a feature that allows
something like this. There are various other techniques or proposals.
For example:

http://www.cs.ru.nl/~wouters/Publications/DataTypesALaCarte.pdf
http://people.cs.uu.nl/andres/OpenDatatypes.html

Cheers,
  Andres

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data constructor synonyms

2011-03-18 Thread Yves Parès
You are better to use simple typeclasses. It depends on what you are trying
to do, but when I want an open type, I use classes + type families.


2011/3/18 Stephen Tetley stephen.tet...@gmail.com

 On 18 March 2011 13:31, Grigory Sarnitskiy sargrig...@ya.ru wrote:

  Anyway, a new question arose.  If I have already declared a type, can I
 add new constructors to it from other modules?
 
  Maybe there are some GHC extensions to solve both these problems.

 no can do.

 There are ways to encode extensible types (e.g the finally tagless
 sytle), but on balance you are better to design extensibility for
 functions - easy to add more functions - than make your code much more
 complicated so it can be extensible for types.

 Parser combinators and pretty print combinators are great and largely
 simple examples of extensibility with functions.

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe