on 5eebc7b help sign in
Idris-dev/docs/reference/syntax-guide.rst
489 lines 10 kB history raw
1**************
2Syntax Guide
3**************
5Examples are mostly adapted from the Idris tutorial.
7Source File Structure
8---------------------
10Source files consist of:
121. An optional :ref:`syntax-module-headers`.
132. Zero or more :ref:`syntax-imports`.
143. Zero or more declarations, e.g. :ref:`syntax-variables`,
15 :ref:`syntax-data-types`, etc.
17For example:
19.. code:: idris
21 module MyModule -- module header
23 import Data.Vect -- an import
25 %default total -- a directive
27 foo : Nat -- a declaration
28 foo = 5
30.. _syntax-module-headers:
32Module Header
33~~~~~~~~~~~~~
35A file can start with a module header, introduced by the ``module`` keyword:
37.. code-block:: idris
39 module Semantics
41Module names can be hierarchical, with parts separated by ``.``:
43.. code-block:: idris
45 module Semantics.Transform
47Each file can define only a single module, which includes everything defined in
48that file.
50Like with declarations, a :ref:`docstring <syntax-comments>` can be used to
51provide documentation for a module:
53.. code-block:: idris
55 ||| Implementation of predicate transformer semantics.
56 module Semantics.Transform
58.. _syntax-imports:
60Imports
61~~~~~~~
63An ``import`` makes the names in another module available for use by the current
64module:
66.. code-block:: idris
68 import Data.Vect
70All the declarations in an imported module are available for use in the file.
71In a case where a name is ambiguous --- e.g. because it is imported from
72multiple modules, or appears in multiple visible namespaces --- the ambiguity can be resolved using :ref:`syntax-qualified-names`. (Often, the compiler can
73resolve the ambiguity for you, using the types involved.)
75Imported modules can be given aliases to make qualified names more compact:
77.. code-block:: idris
79 import Data.Vect as V
81Note that names made visible by import are not, by default, re-exported to
82users of the module being written. This can be done using ``import public``:
84.. code-block:: idris
86 import public Data.Vect
88.. _syntax-variables:
90Variables
91---------
93A variable is always defined by defining its type on one line, and its
94value on the next line, using the syntax
96::
98 <id> : <type>
99 <id> = <value>
101Examples
103.. code:: idris
105 x : Int
106 x = 100
107 hello : String
108 hello = "hello"
110Types
111-----
113In Idris, types are first class values. So a type declaration is the
114same as just declaration of a variable whose type is ``Type``. In Idris,
115variables that denote a type need not be capitalised. Example:
117.. code:: idris
119 MyIntType : Type
120 MyIntType = Int
122a more interesting example:
124.. code:: idris
126 MyListType : Type
127 MyListType = List Int
129While capitalising types is not required, the rules for generating implicit
130arguments mean it is often a good idea.
132.. _syntax-data-types:
134Data types
135~~~~~~~~~~
137Idris provides two kinds of syntax for defining data types. The first,
138Haskell style syntax, defines a regular algebraic data type. For example
140.. code:: idris
142 data Either a b = Left a | Right b
144or
146.. code:: idris
148 data List a = Nil | (::) a (List a)
150The second, more general kind of data type, is defined using Agda or
151GADT style syntax. This syntax defines a data type that is parameterised
152by some values (in the ``Vect`` example, a value of type ``Nat`` and a
153value of type ``Type``).
155.. code:: idris
157 data Vect : Nat -> Type -> Type where
158 Nil : Vect Z a
159 (::) : (x : a) -> (xs : Vect n a) -> Vect (S n) a
161The signature of type constructors may use dependent types
163.. code:: idris
165 data DPair : (a : Type) -> (a -> Type) -> Type where
166 MkDPair : {P : a -> Type} -> (x : a) -> (pf : P x) -> DPair a P
168Records
169~~~~~~~
171There is a special syntax for data types with one constructor and
172multiple fields.
174.. code:: idris
176 record A a where
177 constructor MkA
178 foo, bar : a
179 baz : Nat
181This defines a constructor as well as getter and setter function for
182each field.
184.. code:: idris
186 MkA : a -> a -> Nat -> A a
187 foo : A a -> a
188 set_foo : a -> A a -> A a
190The types of record fields may depend on the value of other fields
192.. code:: idris
194 record Collection a where
195 constructor MkCollection
196 size : Nat
197 items : Vect size a
199Setter functions are only provided for fields that do not use dependant
200types. In the example above neither ``set_size`` nor ``set_items`` are
201defined.
204Co-data
205~~~~~~~
207Inifinite data structures can be introduced with the ``codata``
208keyword.
210.. code:: idris
212 codata Stream : Type -> Type where
213 (::) a -> Stream a -> Stream a
215This is syntactic sugar for the following, which is usually preferred:
217.. code:: idris
219 data Stream : Type -> Type where
220 (::) a -> Inf (Stream a) -> Stream a
222Every occurence of the defined type in a constructor argument will be
223wrapped in the ``Inf`` type constructor. This has the effect of delaying the
224evaluation of the second argument when the data constructor is applied.
225An ``Inf`` argument is constructed using ``Delay`` (which Idris will insert
226implicitly) and evaluated using ``Force`` (again inserted implicitly).
228Furthermore, recursive calls under a ``Delay`` must be guarded by a constructor
229to pass the totality checker.
231Operators
232---------
234Arithmetic
235~~~~~~~~~~
237::
239 x + y
240 x - y
241 x * y
242 x / y
243 (x * y) + (a / b)
245Equality and Relational
246~~~~~~~~~~~~~~~~~~~~~~~
248::
250 x == y
251 x /= y
252 x >= y
253 x > y
254 x <= y
255 x < y
257Conditional
258~~~~~~~~~~~
260::
262 x && y
263 x || y
264 not x
266Conditionals
267------------
269If Then Else
270~~~~~~~~~~~~
272::
274 if <test> then <true> else <false>
276Case Expressions
277~~~~~~~~~~~~~~~~
279::
281 case <test> of
282 <case 1> => <expr>
283 <case 2> => <expr>
284 ...
285 otherwise => <expr>
287Functions
288---------
290Named
291~~~~~
293Named functions are defined in the same way as variables, with the type
294followed by the definition.
296::
298 <id> : <argument type> -> <return type>
299 <id> arg = <expr>
301Example
303.. code:: idris
305 plusOne : Int -> Int
306 plusOne x = x + 1
308Functions can also have multiple inputs, for example
310.. code:: idris
312 makeHello : String -> String -> String
313 makeHello first last = "hello, my name is " ++ first ++ " " ++ last
315Functions can also have named arguments. This is required if you want to
316annotate parameters in a docstring. The following shows the same
317``makeHello`` function as above, but with named parameters which are
318also annotated in the docstring
320.. code:: idris
322 ||| Makes a string introducing a person
323 ||| @first The person's first name
324 ||| @last The person's last name
325 makeHello : (first : String) -> (last : String) -> String
326 makeHello first last = "hello, my name is " ++ first ++ " " ++ last
328Like Haskell, Idris functions can be defined by pattern matching. For
329example
331.. code:: idris
333 sum : List Int -> Int
334 sum [] = 0
335 sum (x :: xs) = x + (sum xs)
337Similarly case analysis looks like
339.. code:: idris
341 answerString : Bool -> String
342 answerString False = "Wrong answer"
343 answerString True = "Correct answer"
345Dependent Functions
346~~~~~~~~~~~~~~~~~~~
348Dependent functions are functions where the type of the return value
349depends on the input value. In order to define a dependent function,
350named parameters must be used, since the parameter will appear in the
351return type. For example, consider
353.. code:: idris
355 zeros : (n : Nat) -> Vect n Int
356 zeros Z = []
357 zeros (S k) = 0 :: (zeros k)
359In this example, the return type is ``Vect n Int`` which is an
360expression which depends on the input parameter ``n``.
362Anonymous
363~~~~~~~~~
365Arguments in anonymous functions are separated by comma.
367::
369 (\x => <expr>)
370 (\x, y => <expr>)
372Modifiers
373~~~~~~~~~
375Visibility
376^^^^^^^^^^
378::
380 public export
381 export
382 private
384Totality
385^^^^^^^^
387::
389 total
390 partial
391 covering
393Sets explicitly to which extent pattern matching is terminating and/or
394exhaustive. A ``partial`` pattern matching makes no assumption. A ``covering``
395pattern matching ensures that pattern matching is exhaustive on its clauses.
396Furthermore, a ``total`` pattern matching enforces both exhaustivity and
397termination of the evaluation of its clauses.
399Implicit Coercion
400^^^^^^^^^^^^^^^^^
402::
404 implicit
406Options
407^^^^^^^
409::
411 %export
412 %hint
413 %no_implicit
414 %error_handler
415 %error_reverse
416 %reflection
417 %specialise [<name list>]
419Misc
420----
422.. _syntax-qualified-names:
424Qualified Names
425~~~~~~~~~~~~~~~
427If multiple declarations with the same name are visible, using the name can
428result in an ambiguous situation. The compiler will attempt to resolve the
429ambiguity using the types involved. If it's unable --- for example, because
430the declarations with the same name also have the same type signatures --- the
431situation can be cleared up using a *qualified name*.
433A qualified name has the symbol's namespace prefixed, separated by a ``.``:
435.. code-block:: idris
437 Data.Vect.length
439This would specifically reference a ``length`` declaration from ``Data.Vect``.
441Qualified names can be written using two different shorthands:
4431. Names in modules that are :ref:`imported <syntax-imports>` using an alias
444 can be qualified by the alias.
4462. The name can be qualified by the *shortest unique suffix* of the
447 namespace in question. For example, the ``length`` case above can likely
448 be shortened to ``Vect.length``.
450.. _syntax-comments:
452Comments
453~~~~~~~~
455::
457 -- Single Line
458 {- Multiline -}
459 ||| Docstring (goes before definition)
461Multi line String literals
462~~~~~~~~~~~~~~~~~~~~~~~~~~
464::
466 foo = """
467 this is a
468 string literal"""
470.. _syntax-directives:
472Directives
473----------
475::
477 %lib <path>
478 %link <path>
479 %flag <path>
480 %include <path>
481 %hide <function>
482 %freeze <name>
483 %access <accessibility>
484 %default <totality>
485 %logging <level 0--11>
486 %dynamic <list of libs>
487 %name <list of names>
488 %error_handlers <list of names>
489 %language <extension>