Is it possible to create a value constructor named "/"?

I declared a recursive data type with the following structure:

data Path = GET | POST | Slash Path String 

I would really like to rename this last constructor of values ​​to / so that I can infinit it in nice expressions, like GET /"controller"/"action" . However, if I try to do this:

 import Prelude hiding ((/)) infixr 5 / data Path = GET | POST | Path / String 

... then I get the following:

 Path.hs:4:30: parse error on input `/' 

The same three lines compile just fine if I replace / with :/ or any other special character sequence starting with :

So, is there any way to name your constructor for the values ​​of / ? I know that I can just call it Slash and then declare a separate function:

 (/) :: Path -> String -> Path (/) = Slash 

... but this will not give me pattern matching, as in:

 request :: Path -> String request path = case path of GET /"hello" -> "Hello!" GET /"goodbye" -> "Goodbye!" 
+6
syntax haskell
source share
3 answers

Short answer: None.

The long answer is: type classes, type names, and data constructors must start with a capital letter or a colon (this requires the use of a language extension). Everything else must begin with a lowercase letter or any other permitted character.

Note that type variables, which are usually string identifiers, follow the same rules and do not begin with a colon.

See also the GHC User Guide for including type statements. I think data constructors are always allowed.

Personally, in your case, I would just use (:/) . It doesn't look so bad, and after a while you are used to ignoring colons. Some people like the hind gut, especially if the data is, in a sense, "symmetrical."

+11
source share

No, you cannot do this. In pure Haskell 98, user-defined type names and constructors must be alphanumeric and capitalized; this is in section 4.1.2 of the Haskell 98 report . In the GHC, just as user-defined constructors with alphanumeric names must begin with an uppercase letter, user-defined constructors that are operators must begin with : 1 (The same is true for custom names, specific type names.) This is described in the section in section 7.4.2 of the GHC manual . I would use :/ , myself, with or without / as a synonym.


1: The reason for the "user-defined" qualification is that there are several built-in exceptions: -> , [] , () and tuple types (,) , (,,) , etc. as type names; and () and type constructors tuple (,) , (,,) , etc., as type constructors

+6
source share

I think that all constructor statements need to start with a colon (but I could be wrong).

So you can do:

 data Path = GET | POST | Path :/ String 
+2
source share

All Articles