Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the dot equals and dot colon syntax mean in Haskell?

Tags:

haskell

What does the dot equals (.=) and dot colon (.:) syntax mean in this example taken from the Aeson JSON library?

instance ToJSON Coord where
  toJSON (Coord xV yV) = object [ "x" .= xV,
                              "y" .= yV ]

-- A FromJSON instance allows us to decode a value from JSON.  This
-- should match the format used by the ToJSON instance.

instance FromJSON Coord where
  parseJSON (Object v) = Coord <$>
                         v .: "x" <*>
                         v .: "y"
  parseJSON _          = empty

Full example on Github: https://github.com/bos/aeson/blob/master/examples/Simplest.hs

like image 578
Rob Young Avatar asked Apr 12 '14 07:04

Rob Young


People also ask

What does the dot mean in Haskell?

The dot ( . ) is the Haskell operator for function composition. Function composition comes from mathematics but actually, it can be really useful to make music. Haskell was originally designed by mathematicians and computer magicians.

What does Colon do in Haskell?

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 .

How do I use the dot operator in Haskell?

Dot operator in Haskell is completely similar to mathematics composition: f{g(x)} where g() is a function and its output used as an input of another function, that is, f(). The result of . (dot) operator is another function (or lambada) that you can use and call it.

What does the period mean in Haskell?

In general terms, where f and g are functions, (f . g) x means the same as f (g x). In other words, the period is used to take the result from the function on the right, feed it as a parameter to the function on the left, and return a new function that represents this computation."


1 Answers

The comments are all correct. You can Hoogle for these function and find the correct haddock documentation, which shows these operators are functions defined in a library and not some integral part of the Haskell language. In general, a starting . is used by libraries to define infix functions because many other desirable symbols are either invalid for functions (ex :.) or the singular character is reserved syntax in Haskell (ex =, :).

(.=) is used to help create JSON objects while .: is for parsing JSON. Normally with Aeson you have a one-to-one match between some data structure and some set of JSON messages, so for example:

{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import Data.Text
import Control.Applicative ((<$>), (<*>))
import Control.Monad (mzero)
data ServerMsg = ServerMsg.
                   { msgId :: Int
                   , msg   :: Text
                   } deriving (Show)
instance ToJSON ServerMsg where
    toJSON d = object [ "msgId" .= msgId d
                      , "msg"   .= msg d
                      ]
instance FromJSON ServerMsg where
    parseJSON (Object o) =
              ServerMsg <$> o .: "msgId" <*> o .: "msg"
    parseJSON _ = mzero    


testJSON :: Value
testJSON = toJSON (ServerMsg 398242 "Hello user3526171")

testParse :: Result ServerMsg
testParse = fromJSON testJSON

Now you can load this module in GHCi and play around easily:

*Main> testJSON
Object fromList [("msg",String "Hello user3526171"),("msgId",Number 398242.0)]
*Main> testParse 
Success (ServerMsg {msgId = 398242, msg = "Hello user3526171"})
like image 133
Thomas M. DuBuisson Avatar answered Sep 19 '22 14:09

Thomas M. DuBuisson