haskell-style.lhs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. \nonstopmode
  2. \documentclass[14pt]{scrartcl} % KOMA-Script article
  3. %include polycode.fmt
  4. %% %format . = "."
  5. \usepackage{hyperref}
  6. \title{Agda's Style Guide to Haskell Programming}
  7. \author{Andreas Abel}
  8. \date{December 2013}
  9. \newcommand{\hidden}[1]{}
  10. \begin{document}
  11. \maketitle
  12. \section{Introduction}
  13. Agda style!
  14. This is a style guide to Haskell programming. It more or less
  15. reflects the predominant style in the source code of Agda. Try to
  16. adhere to it if you contribute to Agda! Agda is going be around for
  17. much longer (hopefully), so the code base must be readable and
  18. maintainable.
  19. \section{Mandatory style}
  20. \subsection{Type signatures}
  21. Besides their role in type checking and error location, type
  22. signatures are an always up-to-date rudimentary documentation of a
  23. function.
  24. Each top-level function must come with a type signature. If you are
  25. too lazy to write it yourself, let ghci figure it out for you and
  26. paste it in. It might be too general so specalize it appropriately if
  27. necessary.
  28. \subsection{Haddock comments}
  29. Provide Haddock comments to at least
  30. \begin{itemize}
  31. \item top-level functions,
  32. \item type and data type declarations,
  33. \item each constructor of a data type,
  34. \item each field of a record.
  35. \end{itemize}
  36. TODO: examples.
  37. Agda TODO: add missing Haddock comments.
  38. \subsection{Indentation}
  39. Uniformly indent your code. There are two evils:
  40. \begin{itemize}
  41. \item Too little indentation: This makes it too hard for the human
  42. reader to see the structure. \textbf{1 space indentation is too
  43. little!}
  44. \item Too much indentation. You might not care if you have a 27 inch
  45. display and a line length of 256 characters. However, we care! A
  46. line should usually fit within 80-100 characters.
  47. \end{itemize}
  48. Recommended indentation is \emph{2 characters}. Upto 4 characters are
  49. tolerated.
  50. \subsubsection{Indenting and haddocking |data|}
  51. Bad:
  52. \begin{code}
  53. data MyDataType = MyConstructor1
  54. | MyConstructor2
  55. | MyConstructor3
  56. deriving (Typable, Show)
  57. \end{code}
  58. Good:
  59. \begin{code}
  60. -- | @MyDataType@ is awesome!
  61. data MyDataType
  62. = MyConstructor1 -- ^ Cool thing.
  63. | MyConstructor2 -- ^ Great stuff.
  64. | MyConstructor3 -- ^ Terrific, or?
  65. deriving (Typable, Show)
  66. \end{code}
  67. Also good:
  68. \begin{code}
  69. -- | @MyDataType@ is awesome!
  70. data MyDataType
  71. = MyConstructor1
  72. -- ^ Cool thing.
  73. | MyConstructor2
  74. -- ^ Great stuff.
  75. | MyConstructor3
  76. -- ^ Terrific.
  77. deriving (Typable, Show)
  78. \end{code}
  79. \begin{code}
  80. \end{code}
  81. \hidden{
  82. \begin{code}
  83. \end{code}
  84. }
  85. \section{Module structure}
  86. A module consists of the following parts, in the following order:
  87. \begin{itemize}
  88. \item GHC language extensions, one per line, in alphabetical
  89. order. Use only language extensions supported by GHC 6.12 and
  90. later.
  91. \item A haddock module comment.
  92. \item The module, possibly with export list.
  93. \item Imports. First the external imports, then the \verb+Agda.*+
  94. modules.
  95. \begin{itemize}
  96. \item |Data.Map| is imported qualified as |Map|, |Data.Set| as
  97. |Set|.
  98. \item The qualifiers for Agda modules are, if used:
  99. |A| for |Agda.Syntax.Abstract|,
  100. |C| for |Agda.Syntax.Concrete|,
  101. |Common| for |Agda.Syntax.Common|,
  102. |I| for |Agda.Syntax.Internal|,
  103. |TCM| for |Agda.TypeChecking.Monad|.
  104. \end{itemize}
  105. \end{itemize}
  106. \begin{code}
  107. {-# LANGUAGE FlexibleInstances #-}
  108. {-# LANGUAGE TupleSections #-}
  109. -- | A longer comment explaining this modules intention.
  110. --
  111. -- This module intents to explain the Agda module structure.
  112. -- Bla bla...
  113. module Agda.Module.Structure where
  114. import Control.Monad.Error
  115. import Control.Monad.State
  116. import Control.Bla
  117. import Data.Function
  118. import Data.Map (Map)
  119. import qualified Data.Map as Map
  120. import Data.Set (Set)
  121. import qualified Data.Set as Set
  122. import Data.Bla
  123. import Agda.Interaction.Bla
  124. import Agda.Syntax.Abstract as A
  125. import Agda.Syntax.Common as Common
  126. import Agda.Syntax.Concrete as C
  127. import Agda.Syntax.Internal as I
  128. import Agda.Syntax.Bla
  129. import Agda.TypeChecking.Monad as TCM
  130. import Agda.TypeChecking.Monad.Bla
  131. import Agda.TypeChecking.Positivity
  132. import Agda.TypeChecking.Bla
  133. import Agda.Termination.Bla
  134. import Agda.Utils.Maybe
  135. import Agda.Utils.Bla
  136. import Agda.Utils.Impossible
  137. ---------------------------------------------------------------------------
  138. -- * Group 1
  139. ---------------------------------------------------------------------------
  140. ... definitions ...
  141. ---------------------------------------------------------------------------
  142. -- * Group 2
  143. ---------------------------------------------------------------------------
  144. ... definitions ...
  145. ...
  146. \end{code}
  147. \end{document}