An operator symbol starting with a colon is a constructor
Asked Answered
P

2

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.

Postgraduate answered 22/1, 2015 at 11:55 Comment(1)
An lowercase alphanumeric identifier is to an uppercase identifier like a (non-:) symbolic operator to a symbol starting with a colon.Process
U
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.

Unshapen answered 22/1, 2015 at 11:58 Comment(3)
thank you. I don't understand two last rows in your code. Can you expand it for me?Postgraduate
Try out the example in ghci.Unshapen
Thanks, but I didn't understand advantage of such behavior yet.Postgraduate
S
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 :.

Symphonia answered 22/1, 2015 at 12:24 Comment(8)
I want to try it. How can I enable the TypeOperators extension?Postgraduate
@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.Symphonia
I add it and rename Symbolic to Symbolic#. But ghci doesn't load it.Postgraduate
@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 = ....Symphonia
{-# LANGUAGE TypeOperators #-} data (:-) n = Constant n | Variable String | (:-) n :+ (:-) n | (:-) n :* (:-*) n deriving (Show)Postgraduate
this compiles fine but don't working: λ: let n = (:+) (:-* 10) (:-* 20) <interactive>:77:15: Not in scope: data constructor :-*' Perhaps you meant :*' (line 6)Postgraduate
@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).Symphonia
@sepp2k, surely you meant data constructors? With TypeOperators data a +++ b = a :*** b is totally allowed, while data a +++ b = a *** b isn't.Ninetta

© 2022 - 2024 — McMap. All rights reserved.