5Examples are mostly adapted from the Idris tutorial.
10A variable is always defined by defining its type on one line, and its
11value on the next line, using the syntax
30In Idris, types are first class values. So a type declaration is the
31same as just declaration of a variable whose type is ``Type``. In Idris,
32variables that denote a type must begin with a capital letter. Example:
39a more interesting example:
49Idris provides two kinds of syntax for defining data types. The first,
50Haskell style syntax, defines a regular algebraic data type. For example
54 data Either a b = Left a | Right b
60 data List a = Nil | (::) a (List a)
62The second, more general kind of data type, is defined using Agda or
63GADT style syntax. This syntax defines a data type that is parameterised
64by some values (in the ``Vect`` example, a value of type ``Nat`` and a
65value of type ``Type``).
69 data Vect : Nat -> Type -> Type where
71 (::) : (x : a) -> (xs : Vect n a) -> Vect (S n) a
87Equality and Relational
88~~~~~~~~~~~~~~~~~~~~~~~
116 if <test> then <true> else <false>
135Named functions are defined in the same way as variables, with the type
136followed by the definition.
140 <id> : <argument type> -> <return type>
150Functions can also have multiple inputs, for example
154 makeHello : String -> String -> String
155 makeHello first last = "hello, my name is " ++ first ++ " " ++ last
157Functions can also have named arguments. This is required if you want to
158annotate parameters in a docstring. The following shows the same
159``makeHello`` function as above, but with named parameters which are
160also annotated in the docstring
164 ||| Makes a string introducing a person
165 ||| @first The person's first name
166 ||| @last The person's last name
167 makeHello : (first : String) -> (last : String) -> String
168 makeHello first last = "hello, my name is " ++ first ++ " " ++ last
170Like Haskell, Idris functions can be defined by pattern matching. For
175 sum : List Int -> Int
177 sum (x :: xs) = x + (sum xs)
179Similarly case analysis looks like
183 answerString : Bool -> String
184 answerString False = "Wrong answer"
185 answerString True = "Correct answer"
190Dependent functions are functions where the type of the return value
191depends on the input value. In order to define a dependent function,
192named parameters must be used, since the parameter will appear in the
193return type. For example, consider
197 zeros : (n : Nat) -> Vect n Int
199 zeros (S k) = 0 :: (zeros k)
201In this example, the return type is ``Vect n Int`` which is an
202expression which depends on the input parameter ``n``. ### Anonymous
203Arguments in anonymous functions are separated by comma.
244 %specialise [<name list>]
256 ||| Docstring (goes before definition)
258Multi line String literals
259~~~~~~~~~~~~~~~~~~~~~~~~~~
278 %access <accessibility>
280 %logging <level 0--11>
281 %dynamic <list of libs>
282 %name <list of names>
283 %error_handlers <list of names>
284 %language <extension>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list