What does triple colon (:::) in a data type mean in Haskell?
For example:
data Term = Var ID | Atom String | Nil | Term:::Term
Which is found in this paper https://gup.ub.gu.se/file/207634
How would it be used?
For example, I can do foo = Var "hello"
but I don't know what use Term:::Term
would be.
tricolon (plural tricolons or tricola) (rhetoric) A sentence with three clearly defined parts of equal length, usually independent clauses.
In Haskell, the colon operator is used to create lists (we'll talk more about this soon). This right-hand side says that the value of makeList is the element 1 stuck on to the beginning of the value of makeList .
$! is strict application, the difference from dispatch state1 is that state1 is guaranteed to be evaluated and not just kept as a lazy thunk. It's defined as f $! x = x `seq` f x. Forcing evaluation in this way can be important for efficiency issues, such as preventing memory leaks.
Cons is a historic name, though, originating from Lisp. :-: is another arbitrary name for the constructor, except that it can be used infix. I.e. instead of Cons 1 someList one can write 1 :-: someList .
(:::)
is the name of the data constructor. You can thus define the Term
type with:
data Term = Var ID | Atom String | Nil | (:::) Term Term
so just like you have Var
, Atom
, and Nil
as data constructors, (:::)
is a data constructor as well. This data constructor takes two parameters which both have Term
types. A list has (:)
as data constructor for example.
Data constructors can be a sequence of symbols, given these start with a colon (:
), and given it is not a reserved operator like :
, ::
, etc. This is specified in the Syntax reference of the Haskell report:
consym → ( : {symbol})⟨reservedop⟩ reservedop → .. | : | :: | = | \ | | | <- | -> | @ | ~ | =>
Would it be clearer with GADT syntax?
data Term :: Type where
Var :: ID -> Term
Atom :: String -> Term
Nil :: Term
(:::) :: Term -> Term -> Term
These signatures match the output of :kind
and :type
:
>> :k Term
Term :: *
>> :t Var
Var :: ID -> Term
>> :t Atom
Atom :: String -> Term
>> :t Nil
Nil :: Term
>> :t (:::)
(:::) :: Term -> Term -> Term
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With