Poperton
Poperton

Reputation: 2127

How to make a multiline comment in Haskell?

How do I comment a 'block' of code in Haskell? Example:

myReverse' :: [a] -> [a]
myReverse' [x] = [x]
myReverse' (x:xs) = myReverse xs ++ [x]

Upvotes: 21

Views: 18841

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

An important remark regarding this is that some comment blocks have a special meaning for Haskell compilers. These are wrapped in a block comment such that compilers that do not interpret this, will simply ignore these.

As the Haskell wiki says on the {- and -} keywords:

Everything between "{-" followed by a space and "-}" is a block comment.

For example a compiler pragma is written between {-# … #-}. We can use this to enable extra features that are not standard Haskell:

{-# LANGUAGE OverloadedStrings #-}

or we can use this to specify rewrite rules for a function if the parameters satisfy certain properties:

{-# RULES
  "map/map"    forall f g xs.  map f (map g xs) = map (f.g) xs
#-}

When we want to specify documentation for a module for example, we can make use of {-| … |-} to specify a block for documentation, for example:

You can also use Haskell's nested-comment style for documentation annotations, which is sometimes more convenient when using multi-line comments:

{-|
  The 'square' function squares an integer.
  It takes one argument, of type 'Int'.
-}
square :: Int -> Int
square x = x * x

Liquid Haskell makes use of {-@ … @-} to specify static properties that Liquid Haskell then will try to proof (or come up with a counter example):

{-@ type Even = {v:Int | v mod 2 = 0} @-}

{-@ zero'' :: Even @-}
zero''     :: Int
zero''     =  0

Such block comments can be meaningless if the compiler does not support pragma's, if you do not work with Haddock, or do not use Liquid Haskell, etc. But these comment blocks are thus often used to implement extra features that are not specified in the (official) Haskell documentation.

Upvotes: 9

Poperton
Poperton

Reputation: 2127

{-
myReverse' :: [a] -> [a]
myReverse' [x] = [x]
myReverse' (x:xs) = myReverse xs ++ [x]
-}

Upvotes: 39

Related Questions