What does '@' mean in Haskell?
Asked Answered
A

2

11

I've tried googling but come up short. I am furthering my Haskell knowledge by reading some articles and I came across one that uses a syntax I've never seen before. An example would be:

reconstruct node@(Node a b c l r) parent@(Node b d le ri)

I've never seen these @'s before. I tried searching online for an answer but came up short. Is this simply a way to embed tags to help make things clearer, or do they have an actual impact on the code?

Anticlerical answered 19/5, 2015 at 12:55 Comment(5)
Search for "as patterns" in learnyouahaskell.com/syntax-in-functions#pattern-matching for an introduction.Induce
While traditional search engines won't return useful results when searching for special characters, Haskell's own hoogle is usually quite useful for such questions.Silden
@Silden More helpful then the answer!Asthma
Does this answer your question? Understanding the Haskell as-patternSledge
@ComicSansMS, it doesn't help when searching for syntax though.Manet
S
23

It is used in pattern matching. Now node variable will refer to the entire Node data type for the argument Node a b c l r. So instead of passing to the function as Node a b c l r, you can use node instead to pass it up.

A much simpler example to demonstrate it:

data SomeType = Leaf Int Int Int | Nil deriving Show

someFunction :: SomeType -> SomeType
someFunction leaf@(Leaf _ _ _) = leaf
someFunction Nil = Leaf 0 0 0

The someFunction can also be written as:

someFunction :: SomeType -> SomeType
someFunction (Leaf x y z) = Leaf x y z
someFunction Nil = Leaf 0 0 0

See how simpler was the first version ?

Sirree answered 19/5, 2015 at 12:59 Comment(9)
Even simpler would be someFunction leaf@Leaf{} = leaf.Aurelioaurelius
@AndrásKovács, but doesn't it require some explanations on Leaf{}?Prurient
The 2 are slightly different in ghci (in the second one it will re-apply the constructor arguments), but I'd be very surprised if that were the case in ghc compiled codeTerrify
What does @ mean in a context like Proxy @Foo?Friesland
Whew, finally figured out it needs {-# LANGUAGE TypeApplications #-}, still learning what it is though.Friesland
@Friesland - In that context, it is some sort of type cast à la C/C++. I would expect your Foo thing to be a type. Formally, this @_type_ notation allows you to give explicit type arguments to a polymorphic function. Like: let x = (read @Integer "33") setting x to be an Integer object of value 33. Details here.Forestry
@jpmarinier, no, it is not a type cast of any sort. It does indeed let you apply type arguments, but what those mean depend on the particular functions or methods involved.Manet
@Manet I agree it is only a loose analogy, as a C/C++ cast applies to a value, while this Haskell construct applies to a function, with for example the expression (read @Integer) having type String -> Integer. Please note I did not use the word cast in my answer below.Forestry
@jpmarinier, it's so loose I can't even guess what it means. foo @Int could easily be a function that takes two characters and produces an IO action returning ().Manet
F
12

Using @t as a type indicator

Besides the argument pattern matching usage described in the answer of @Sibi, in Haskell the "at" character ('@', also known as an arobase character) can be used in some contexts to force a typing decision. This is mentioned in the comments by @Josh.F.

This is not part of the default language features, and is known as the Type Application Haskell language extension. In summary, the extension allows you to give explicit type arguments to a polymorphic function such as read. In a classic .hs source file, the relevant pragma must be included:

{-#  LANGUAGE TypeApplications  #-}

Example:

$ ghci
GHCi, version 8.2.2: http://www.haskell.org/ghc/  :? for help
 λ> 
 λ> let x = (read @Integer "33")

 <interactive>:4:10: error:
    Pattern syntax in expression context: read@Integer
    Did you mean to enable TypeApplications?
 λ> 
 λ> :set -XTypeApplications
 λ>
 λ> let x = (read @Integer "33")
 λ>
 λ> :type  x
 x :: Integer
 λ> 
 λ> x
 33
 λ> 

Further details

For the read polymorphic function, the type indicator introduced by @ relates to the type of the result returned by read. But this is not generally true.

Generally speaking, you have to consider the type variables that appear in the type signature of the function at hand. For example, let's have a look at the fmap library function.

fmap :: Functor ft => (a -> b) -> ft a -> ft b

So here, we have 3 type variables, in order of appearance: ft, a, b. If we specialize fmap like this:

myFmap = fmap  @type1  @type2  @type3

then type1 will relate to ft, type2 will relate to a and type3 will relate to b. Also, there is a special dummy type indicator @_ which means: “here, any type goes”.

For example, we can force the output type of fmap to be Integer and the functor to be the plain list [], leaving the input type a unspecified:

 λ> 
 λ> myFmap = fmap  @[]  @_  @Integer
 λ> 
 λ> :type myFmap
 myFmap :: (_ -> Integer) -> [_] -> [Integer]
 λ> 

As for the read function, its type is:

read :: Read a => String -> a

So there is only room for one type indicator, and it relates to the type of the result returned by read, as displayed above.

Forestry answered 9/11, 2019 at 16:32 Comment(1)
This is exactly what I was looking - I knew about @'s use in pattern matching, but not for type indication. Thanks!Aldwin

© 2022 - 2024 — McMap. All rights reserved.