|
| 1 | +.. _sect-documenting: |
| 2 | + |
| 3 | +********************** |
| 4 | +Documenting Idris Code |
| 5 | +********************** |
| 6 | + |
| 7 | +Idris documentation comes in two major forms: comments, which exist |
| 8 | +for a reader’s edification and are ignored by the compiler, and inline |
| 9 | +API documentation, which the compiler parses and stores for future |
| 10 | +reference. To consult the documentation for a declaration ``f``, write |
| 11 | +``:doc f`` at the REPL or use the appropriate command in your editor |
| 12 | +(``C-c C-d`` in Emacs, ``<LocalLeader>h`` in Vim). |
| 13 | + |
| 14 | +Comments |
| 15 | +======== |
| 16 | + |
| 17 | +Use comments to explain why code is written the way that it |
| 18 | +is. Idris’s comment syntax is the same as that of Haskell: lines |
| 19 | +beginning with ``--`` are comments, and regions bracketed by ``{-`` |
| 20 | +and ``-}`` are comments even if they extend across multiple |
| 21 | +lines. These can be used to comment out lines of code or provide |
| 22 | +simple documentation for the readers of Idris code. |
| 23 | + |
| 24 | +Inline Documentation |
| 25 | +==================== |
| 26 | + |
| 27 | +Idris also supports a comprehensive and rich inline syntax for Idris |
| 28 | +code to be generated. This syntax also allows for named parameters and |
| 29 | +variables within type signatures to be individually annotated using a |
| 30 | +syntax similar to Javadoc parameter annotations. |
| 31 | + |
| 32 | +Documentation always comes before the declaration being documented. |
| 33 | +Inline documentation applies to either top-level declarations or to |
| 34 | +constructors. Documentation for specific arguments to constructors, type |
| 35 | +constructors, or functions can be associated with these arguments using |
| 36 | +their names. |
| 37 | + |
| 38 | +The inline documentation for a declaration is an unbroken string of |
| 39 | +lines, each of which begins with ``|||`` (three pipe symbols). The |
| 40 | +first paragraph of the documentation is taken to be an overview, and |
| 41 | +in some contexts, only this overview will be shown. After the |
| 42 | +documentation for the declaration as a whole, it is possible to |
| 43 | +associate documentation with specific named parameters, which can |
| 44 | +either be explicitly name or the results of converting free variables |
| 45 | +to implicit parameters. Annotations are the same as with Javadoc |
| 46 | +annotations, that is for the named parameter ``(n : T)``, the |
| 47 | +corresponding annotation is ``||| @ n Some description`` that is |
| 48 | +placed before the declaration. |
| 49 | + |
| 50 | +Documentation is written in Markdown, though not all contexts will |
| 51 | +display all possible formatting (for example, images are not displayed |
| 52 | +when viewing documentation in the REPL, and only some terminals render |
| 53 | +italics correctly). A comprehensive set of examples is given below. |
| 54 | + |
| 55 | +.. code-block:: idris |
| 56 | +
|
| 57 | +
|
| 58 | + ||| Modules can also be documented. |
| 59 | + module Docs |
| 60 | +
|
| 61 | + ||| Add some numbers. |
| 62 | + ||| |
| 63 | + ||| Addition is really great. This paragraph is not part of the overview. |
| 64 | + ||| Still the same paragraph. |
| 65 | + ||| |
| 66 | + ||| You can even provide examples which are inlined in the documentation: |
| 67 | + ||| ```idris example |
| 68 | + ||| add 4 5 |
| 69 | + ||| ``` |
| 70 | + ||| |
| 71 | + ||| Lists are also nifty: |
| 72 | + ||| * Really nifty! |
| 73 | + ||| * Yep! |
| 74 | + ||| * The name `add` is a **bold** choice |
| 75 | + ||| @ n is the recursive param |
| 76 | + ||| @ m is not |
| 77 | + add : (n, m : Nat) -> Nat |
| 78 | + add Z m = m |
| 79 | + add (S n) m = S (add n m) |
| 80 | +
|
| 81 | +
|
| 82 | + ||| Append some vectors |
| 83 | + ||| @ a the contents of the vectors |
| 84 | + ||| @ xs the first vector (recursive param) |
| 85 | + ||| @ ys the second vector (not analysed) |
| 86 | + appendV : (xs : Vect n a) -> (ys : Vect m a) -> Vect (add n m) a |
| 87 | + appendV [] ys = ys |
| 88 | + appendV (x::xs) ys = x :: appendV xs ys |
| 89 | +
|
| 90 | + ||| Here's a simple datatype |
| 91 | + data Ty = |
| 92 | + ||| Unit |
| 93 | + UNIT | |
| 94 | + ||| Functions |
| 95 | + ARR Ty Ty |
| 96 | +
|
| 97 | + ||| Points to a place in a typing context |
| 98 | + data Elem : Vect n Ty -> Ty -> Type where |
| 99 | + Here : {ts : Vect n Ty} -> Elem (t::ts) t |
| 100 | + There : {ts : Vect n Ty} -> Elem ts t -> Elem (t'::ts) t |
| 101 | +
|
| 102 | + ||| A more interesting datatype |
| 103 | + ||| @ n the number of free variables |
| 104 | + ||| @ ctxt a typing context for the free variables |
| 105 | + ||| @ ty the type of the term |
| 106 | + data Term : (ctxt : Vect n Ty) -> (ty : Ty) -> Type where |
| 107 | +
|
| 108 | + ||| The constructor of the unit type |
| 109 | + ||| More comment |
| 110 | + ||| @ ctxt the typing context |
| 111 | + UnitCon : {ctxt : Vect n Ty} -> Term ctxt UNIT |
| 112 | +
|
| 113 | + ||| Function application |
| 114 | + ||| @ f the function to apply |
| 115 | + ||| @ x the argument |
| 116 | + App : {ctxt : Vect n Ty} -> (f : Term ctxt (ARR t1 t2)) -> (x : Term ctxt t1) -> Term ctxt t2 |
| 117 | +
|
| 118 | + ||| Lambda |
| 119 | + ||| @ body the function body |
| 120 | + Lam : {ctxt : Vect n Ty} -> (body : Term (t1::ctxt) t2) -> Term ctxt (ARR t1 t2) |
| 121 | +
|
| 122 | + ||| Variables |
| 123 | + ||| @ i de Bruijn index |
| 124 | + Var : {ctxt : Vect n Ty} -> (i : Elem ctxt t) -> Term ctxt t |
| 125 | +
|
| 126 | + ||| We can document records, including their fields and constructors |
| 127 | + record Yummy where |
| 128 | + ||| Make a yummy |
| 129 | + constructor MkYummy |
| 130 | + ||| What to eat |
| 131 | + food : String |
0 commit comments