6

I learn Haskell. From Haskell 2010 documentation:

  • An operator symbol starting with a colon is a constructor.
  • An operator symbol starting with any other character is an ordinary identifier.

I don't understand first phrase. I know exist data constructors and class type constructors. What constructor this case? Maybe I need a code sample.

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • 2
    An lowercase alphanumeric identifier is to an uppercase identifier like a (non-`:`) symbolic operator to a symbol starting with a colon. – Franky Jan 22 '15 at 15:14

2 Answers2

7

You can define stuff like

data Symbolic n
   = Constant n
   | Variable String
   | Symbolic n :+ Symbolic n
   | Symbolic n :* Symbolic n
  deriving (Show)

GHCi> let v = Variable; c = Constant
GHCi> c 2 :* v"a" :+ c 3
    (Constant 2 :* Variable "a") :+ Constant 3

That's what the first phrase refers to.

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
4

I know exist data constructors and class type constructors. What constructor this case?

In standard Haskell only data constructors can be symbolic and type names must be alphanumeric. If you enable the GHC extension TypeOperators, type names can be symbolic as well, allowing you to define type constructors that start with :.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • I want to try it. How can I enable the `TypeOperators` extension? – Andrey Bushman Jan 22 '15 at 12:42
  • 1
    @Bush You can add the `-XTypeOperators` flag to your call to ghc or ghci. You can also add `{-# LANGUAGE TypeOperators #-}` to the beginning of a Haskell file to get the same effect without any compiler options. – sepp2k Jan 22 '15 at 13:14
  • I add it and rename `Symbolic` to `Symbolic#`. But ghci doesn't load it. – Andrey Bushman Jan 22 '15 at 13:30
  • 1
    @Bush A name needs to either be all symbolic or all alphanumeric. And as mentioned symbolic constructor names need to start with a colon (otherwise it's a type variable). So you'd need to rename `Symbolic` to something like `:-*` or some other combination of symbols starting with a colon. Also remember that symbolic names are used infix when not enclosed in parentheses, so it'd be either `data a :-* b = ...` or `data (:-*) n = ...`. – sepp2k Jan 22 '15 at 13:37
  • {-# LANGUAGE TypeOperators #-} data (:-*) n = Constant n | Variable String | (:-*) n :+ (:-*) n | (:-*) n :* (:-*) n deriving (Show) – Andrey Bushman Jan 22 '15 at 13:57
  • this compiles fine but don't working: λ: let n = (:+) (:-* 10) (:-* 20) :77:15: Not in scope: data constructor `:-*' Perhaps you meant `:*' (line 6) – Andrey Bushman Jan 22 '15 at 13:58
  • 1
    @Bush In the code you've shown, you defined the data constructors `:+` and `:*`, but you're trying to use `:-*`, which you didn't define anywhere. Something like `let n :: (:-) Integer = Constant 1 :+ Constant 2` should work (the type signature is optional, of course, but this way the type constructor appears in the code). – sepp2k Jan 22 '15 at 14:14
  • @sepp2k, surely you meant data constructors? With TypeOperators `data a +++ b = a :*** b` is totally allowed, while `data a +++ b = a *** b` isn't. – Mirzhan Irkegulov Jun 01 '17 at 13:30