Data constructor synonyms

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Data constructor synonyms

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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Data constructor synonyms

Roel van Dijk-3
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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Data constructor synonyms

Grigory Sarnitskiy
18.03.2011, 14:22, "Roel van Dijk" <[hidden email]>;:

>  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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Data constructor synonyms

Stephen Tetley-2
On 18 March 2011 13:31, Grigory Sarnitskiy <[hidden email]> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Data constructor synonyms

Andres Löh-2
In reply to this post by Grigory Sarnitskiy
>>  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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Data constructor synonyms

Yves Parès
In reply to this post by Stephen Tetley-2
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 <[hidden email]>
On 18 March 2011 13:31, Grigory Sarnitskiy <[hidden email]> 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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe