codesnippets:haddockexamples
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:haddockexamples [2021/03/12 09:46] – created f2b216 | codesnippets:haddockexamples [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Haddock examples ====== | ====== Haddock examples ====== | ||
| + | |||
| + | ===== Markups ===== | ||
| + | |||
| + | ==== Paragraphs ==== | ||
| + | |||
| + | One or more blank lines separates two paragraphs in a documentation comment. | ||
| + | |||
| + | ==== Special characters ==== | ||
| + | |||
| + | The following characters have special meanings in documentation comments: \, /, ', `, ", @, <. To insert a literal occurrence of one of these special characters, precede it with a backslash (\). | ||
| + | |||
| + | Additionally, | ||
| + | |||
| + | Furthermore, | ||
| + | |||
| + | ==== Character references ==== | ||
| + | |||
| + | Although Haskell source files may contain any character from the Unicode character set, the encoding of these characters as bytes varies between systems, so that only source files restricted to the ASCII character set are portable. Other characters may be specified in character and string literals using Haskell character escapes. To represent such characters in documentation comments, Haddock supports SGML-style numeric character references of the forms &#D; and &#xH; where D and H are decimal and hexadecimal numbers denoting a code position in Unicode (or ISO 10646). For example, the references &# | ||
| + | |||
| + | ==== Code Blocks ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | This documentation includes two blocks of code: | ||
| + | -- | ||
| + | -- @ | ||
| + | -- f x = x + x | ||
| + | -- @ | ||
| + | -- | ||
| + | -- > g x = x * 42 | ||
| + | </ | ||
| + | |||
| + | ==== Examples ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | Two examples are given below: | ||
| + | -- | ||
| + | -- >>> | ||
| + | -- 55 | ||
| + | -- | ||
| + | -- >>> | ||
| + | -- foo | ||
| + | -- bar | ||
| + | </ | ||
| + | |||
| + | ==== Properties ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | Addition is commutative: | ||
| + | -- | ||
| + | -- prop> a + b = b + a | ||
| + | </ | ||
| + | |||
| + | ==== Hyperlinked Identifiers ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | This module defines the type ' | ||
| + | </ | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | The identifier ' | ||
| + | </ | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | I don't have to escape my apostrophes; | ||
| + | </ | ||
| + | |||
| + | ==== Emphasis, bold and monospaced text ==== | ||
| + | |||
| + | Emphasis may be added by surrounding text with... | ||
| + | |||
| + | < | ||
| + | |||
| + | Other markup is valid inside emphasis. | ||
| + | |||
| + | To have a forward slash inside of emphasis, just escape it... | ||
| + | |||
| + | < | ||
| + | |||
| + | Bold (strong) text is indicated by surrounding it with... | ||
| + | |||
| + | <code Haskell> | ||
| + | |||
| + | Other markup is valid inside bold. For example ... | ||
| + | |||
| + | <code Haskell> | ||
| + | |||
| + | ...will make the emphasised text foo bold. | ||
| + | |||
| + | You don't have to escape a single underscore if you need it bold... | ||
| + | |||
| + | <code Haskell> | ||
| + | |||
| + | Monospaced (or typewriter) text is indicated by surrounding it with | ||
| + | |||
| + | < | ||
| + | |||
| + | Other markup is valid inside a monospaced span: for example... | ||
| + | |||
| + | < | ||
| + | |||
| + | ...will hyperlink the identifier f inside the code fragment | ||
| + | |||
| + | ==== Linking to modules ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | This is a reference to the " | ||
| + | </ | ||
| + | |||
| + | ==== Itemized and enumerated lists ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | This is a bulleted list: | ||
| + | -- | ||
| + | -- * first item | ||
| + | -- | ||
| + | -- * second item | ||
| + | </ | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | This is an enumerated list: | ||
| + | -- | ||
| + | -- (1) first item | ||
| + | -- | ||
| + | -- 2. second item | ||
| + | </ | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | This is an enumerated list: | ||
| + | -- | ||
| + | -- (1) first item | ||
| + | -- 2. second item | ||
| + | -- | ||
| + | -- This is a bulleted list: | ||
| + | -- | ||
| + | -- * first item | ||
| + | -- * second item | ||
| + | </ | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | | ||
| + | -- * first item | ||
| + | -- and more content for the first item | ||
| + | -- * second item | ||
| + | -- and more content for the second item | ||
| + | </ | ||
| + | |||
| + | <code Haskell> | ||
| + | {-| | ||
| + | * Beginning of list | ||
| + | This belongs to the list above! | ||
| + | |||
| + | > nested | ||
| + | > bird | ||
| + | > tracks | ||
| + | |||
| + | * Next list | ||
| + | More of the indented list. | ||
| + | |||
| + | * Deeper | ||
| + | |||
| + | @ | ||
| + | even code blocks work | ||
| + | @ | ||
| + | |||
| + | * Deeper | ||
| + | |||
| + | 1. Even deeper! | ||
| + | 2. No newline separation even in indented lists. | ||
| + | -} | ||
| + | </ | ||
| + | |||
| + | ==== Definition lists ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | This is a definition list: | ||
| + | -- | ||
| + | -- | ||
| + | -- | ||
| + | -- | ||
| + | </ | ||
| + | |||
| + | To produce output something like this: | ||
| + | |||
| + | '' | ||
| + | * The description of '' | ||
| + | '' | ||
| + | * The description of '' | ||
| + | |||
| + | ==== URLs ==== | ||
| + | |||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ==== Images ==== | ||
| + | |||
| + | < | ||
| + | << | ||
| + | </ | ||
| + | |||
| + | ==== Anchors ==== | ||
| + | |||
| + | Sometimes it is useful to be able to link to a point in the documentation which doesn' | ||
| + | |||
| + | To link to an anchor from elsewhere, use the syntax " | ||
| + | |||
| + | ==== Headings ==== | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | | ||
| + | -- = Heading level 1 with some __bold__ | ||
| + | -- Something underneath the heading. | ||
| + | -- | ||
| + | -- == / | ||
| + | -- More content. | ||
| + | -- | ||
| + | -- === Subsubheading | ||
| + | -- Even more content. | ||
| + | </ | ||
| + | |||
| + | <code Haskell> | ||
| + | -- | | ||
| + | -- = Heading level 1 with some __bold__ | ||
| + | -- Something underneath the heading. | ||
| + | -- | ||
| + | -- == / | ||
| + | -- More content. | ||
| + | -- | ||
| + | -- === Subsubheading | ||
| + | -- >>> | ||
| + | </ | ||
| + | |||
| ===== Documenting a top-level declaration ===== | ===== Documenting a top-level declaration ===== | ||
| Line 5: | Line 237: | ||
| ==== simple ==== | ==== simple ==== | ||
| - | < | + | < |
| -- |The ' | -- |The ' | ||
| square :: Int -> Int | square :: Int -> Int | ||
| Line 13: | Line 245: | ||
| ==== after the declaration ==== | ==== after the declaration ==== | ||
| - | < | + | < |
| square :: Int -> Int | square :: Int -> Int | ||
| -- ^The ' | -- ^The ' | ||
| Line 21: | Line 253: | ||
| ==== several lines ==== | ==== several lines ==== | ||
| - | < | + | < |
| -- |The ' | -- |The ' | ||
| -- It takes one argument, of type ' | -- It takes one argument, of type ' | ||
| Line 30: | Line 262: | ||
| ==== nested-comment style ==== | ==== nested-comment style ==== | ||
| - | < | + | < |
| {-| | {-| | ||
| The ' | The ' | ||
| Line 41: | Line 273: | ||
| ===== Class methods ===== | ===== Class methods ===== | ||
| - | < | + | < |
| class C a where | class C a where | ||
| -- | This is the documentation for the ' | -- | This is the documentation for the ' | ||
| Line 51: | Line 283: | ||
| ===== Constructors and record fields ===== | ===== Constructors and record fields ===== | ||
| - | < | + | < |
| data T a b | data T a b | ||
| -- | This is the documentation for the ' | -- | This is the documentation for the ' | ||
| Line 59: | Line 291: | ||
| </ | </ | ||
| - | < | + | < |
| data T a b | data T a b | ||
| = C1 a b -- ^ This is the documentation for the ' | = C1 a b -- ^ This is the documentation for the ' | ||
| Line 65: | Line 297: | ||
| </ | </ | ||
| - | < | + | < |
| data R a b = | data R a b = | ||
| C { -- | This is the documentation for the ' | C { -- | This is the documentation for the ' | ||
| Line 74: | Line 306: | ||
| </ | </ | ||
| - | < | + | < |
| data R a b = | data R a b = | ||
| C { a :: a -- ^ This is the documentation for the ' | C { a :: a -- ^ This is the documentation for the ' | ||
| Line 81: | Line 313: | ||
| </ | </ | ||
| - | < | + | < |
| data T a = A { someField :: a -- ^ Doc for someField of A | data T a = A { someField :: a -- ^ Doc for someField of A | ||
| } | } | ||
| Line 90: | Line 322: | ||
| ===== Function arguments ===== | ===== Function arguments ===== | ||
| - | < | + | < |
| f :: Int -- ^ The ' | f :: Int -- ^ The ' | ||
| -> Float -- ^ The ' | -> Float -- ^ The ' | ||
| Line 98: | Line 330: | ||
| ===== The module description ===== | ===== The module description ===== | ||
| - | < | + | < |
| {-| | {-| | ||
| Module | Module | ||
| Line 120: | Line 352: | ||
| To Haddock the export list has even more significance than just specifying the entities to be included in the documentation. It also specifies the order that entities will be listed in the generated documentation. This leaves the programmer free to implement functions in any order he/she pleases, and indeed in any module he/she pleases, but still specify the order that the functions should be documented in the export list. Indeed, many programmers already do this: the export list is often used as a kind of ad-hoc interface documentation, | To Haddock the export list has even more significance than just specifying the entities to be included in the documentation. It also specifies the order that entities will be listed in the generated documentation. This leaves the programmer free to implement functions in any order he/she pleases, and indeed in any module he/she pleases, but still specify the order that the functions should be documented in the export list. Indeed, many programmers already do this: the export list is often used as a kind of ad-hoc interface documentation, | ||
| - | < | + | < |
| module Foo ( | module Foo ( | ||
| -- * Classes | -- * Classes | ||
| Line 134: | Line 366: | ||
| </ | </ | ||
| - | < | + | Alternativerly with commas at the beginning. |
| + | |||
| + | < | ||
| module Foo ( | module Foo ( | ||
| -- * Classes | -- * Classes | ||
| Line 153: | Line 387: | ||
| If modules are imported wholly and without any hiding qualifiers, then the documentation will just contain a cross-reference to the documentation for B and C. | If modules are imported wholly and without any hiding qualifiers, then the documentation will just contain a cross-reference to the documentation for B and C. | ||
| - | < | + | < |
| module A ( | module A ( | ||
| module B, | module B, | ||
| Line 162: | Line 396: | ||
| If the modules are not completely re-exported, | If the modules are not completely re-exported, | ||
| - | < | + | < |
| module A ( | module A ( | ||
| module B, | module B, | ||
| Line 176: | Line 410: | ||
| If there is no export list in the module, then every entity will be mentioned as defined at the top level in the module. The generated documentation will retain the order in which entities are defined in the module. | If there is no export list in the module, then every entity will be mentioned as defined at the top level in the module. The generated documentation will retain the order in which entities are defined in the module. | ||
| - | < | + | < |
| module Foo where | module Foo where | ||
| </ | </ | ||
| Line 182: | Line 416: | ||
| And the module body __may__ also include section headings. | And the module body __may__ also include section headings. | ||
| - | < | + | < |
| module Foo where | module Foo where | ||
| Line 194: | Line 428: | ||
| ===== Named chunks of documentation ===== | ===== Named chunks of documentation ===== | ||
| - | < | + | To include a chunk of documentation which is not attached to any particular Haskell declaration. |
| + | |||
| + | The documentation can be included in the export list directly. | ||
| + | |||
| + | < | ||
| module Foo ( | module Foo ( | ||
| -- * A section heading | -- * A section heading | ||
| Line 203: | Line 441: | ||
| </ | </ | ||
| - | < | + | If the documentation is large and placing it inline in the export list might bloat the export list and obscure the structure, then it can be given a name and placed out of line in the body of the module. |
| + | |||
| + | < | ||
| module Foo ( | module Foo ( | ||
| -- * A section heading | -- * A section heading | ||
| Line 216: | Line 456: | ||
| </ | </ | ||
| - | < | + | ===== Hyperlinking and re-exported entities ===== |
| + | |||
| + | Haddock takes the view that each entity has a home module; that is, the module that the library designer would most like to direct the user to, to find the documentation for that entity. So, Haddock makes all links to an entity point to the home module. The one exception is when the entity is also exported by the current module: Haddock makes a local link if it can. | ||
| + | |||
| + | Haddock uses the following rules: | ||
| + | * If modules A and B both export the entity, and module A imports (directly or indirectly) module B, then B is preferred. | ||
| + | * A module with the hide attribute is never chosen as the home. | ||
| + | * A module with the not-home attribute is only chosen if there are no other modules to choose. | ||
| + | |||
| + | < | ||
| + | module A (T) where | ||
| + | data T a = C a | ||
| + | |||
| + | module B (f) where | ||
| + | import A | ||
| + | f :: T Int -> Int | ||
| + | f (C i) = i | ||
| + | |||
| + | module C (T, f) where | ||
| + | import A | ||
| + | import B | ||
| </ | </ | ||
| - | < | + | If multiple modules fit the criteria, then one is chosen at random. If no modules fit the criteria (because the candidates are all hidden), then Haddock will issue a warning for each reference to an entity without a home. |
| + | |||
| + | In the example above, module A is chosen as the home for T because it does not import any other module that exports T. The link from f's type in module B will therefore point to A.T. However, C also exports T and f, and the link from f's type in C will therefore point locally to C.T. | ||
| + | |||
| + | ===== Module Attributes ===== | ||
| + | |||
| + | Attributes are specified in a comma-separated list in an {-# OPTIONS_HADDOCK ... #-} pragma at the top of the module. | ||
| + | |||
| + | < | ||
| + | {-# OPTIONS_HADDOCK hide, prune, ignore-exports #-} | ||
| + | |||
| + | -- |Module description | ||
| + | module A where | ||
| + | ... | ||
| </ | </ | ||
| + | |||
| + | The following attributes are currently understood by Haddock: | ||
| + | * **hide**: Omit this module from the generated documentation, | ||
| + | * **prune**: Omit definitions that have no documentation annotations from the generated documentation. | ||
| + | * **ignore-exports**: | ||
| + | * **not-home**: | ||
| + | * **show-extensions**: | ||
| Line 234: | Line 514: | ||
| ===== Source ===== | ===== Source ===== | ||
| - | * [[https:// | + | ==== Haddock User Guide ==== |
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| + | |||
| ===== License ===== | ===== License ===== | ||
| Line 258: | Line 541: | ||
| </ | </ | ||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/haddockexamples.1615538760.txt.gz · Last modified: (external edit)
