Type variables in constructors?

Can a function be used in a data constructor? How:

data Something = (a->b) Something1 Something2 
+4
source share
2 answers

Yes of course you can. The only important thing is that you (always) need a name for your data constructor:

 data <name> <para0> <param1> ... = <constructor> <arg0> <arg1> <arg2> ... 

So, for our example, it becomes

 data Something ab = Constructor (a -> b) Something1 Something2 
+12
source

There are some rules for naming a constructor.

  • Start with a capital letter.
  • May contain underscores, single quotes, letters, and numbers.
  • Constructors can be operator names if they begin with the ':' character.

But of course, you can have functions in the data definition, for example

 data Something ab = Something (a->b) ab 
+5
source

Source: https://habr.com/ru/post/1434466/


All Articles