on dff4fcd help sign in
Idris-dev/docs/reference/syntax-guide.rst
408 lines 7.9 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
161Operators
162---------
164Arithmetic
165~~~~~~~~~~
167::
169 x + y
170 x - y
171 x * y
172 x / y
173 (x * y) + (a / b)
175Equality and Relational
176~~~~~~~~~~~~~~~~~~~~~~~
178::
180 x == y
181 x /= y
182 x >= y
183 x > y
184 x <= y
185 x < y
187Conditional
188~~~~~~~~~~~
190::
192 x && y
193 x || y
194 not x
196Conditionals
197------------
199If Then Else
200~~~~~~~~~~~~
202::
204 if <test> then <true> else <false>
206Case Expressions
207~~~~~~~~~~~~~~~~
209::
211 case <test> of
212 <case 1> => <expr>
213 <case 2> => <expr>
214 ...
215 otherwise => <expr>
217Functions
218---------
220Named
221~~~~~
223Named functions are defined in the same way as variables, with the type
224followed by the definition.
226::
228 <id> : <argument type> -> <return type>
229 <id> arg = <expr>
231Example
233.. code:: idris
235 plusOne : Int -> Int
236 plusOne x = x + 1
238Functions can also have multiple inputs, for example
240.. code:: idris
242 makeHello : String -> String -> String
243 makeHello first last = "hello, my name is " ++ first ++ " " ++ last
245Functions can also have named arguments. This is required if you want to
246annotate parameters in a docstring. The following shows the same
247``makeHello`` function as above, but with named parameters which are
248also annotated in the docstring
250.. code:: idris
252 ||| Makes a string introducing a person
253 ||| @first The person's first name
254 ||| @last The person's last name
255 makeHello : (first : String) -> (last : String) -> String
256 makeHello first last = "hello, my name is " ++ first ++ " " ++ last
258Like Haskell, Idris functions can be defined by pattern matching. For
259example
261.. code:: idris
263 sum : List Int -> Int
264 sum [] = 0
265 sum (x :: xs) = x + (sum xs)
267Similarly case analysis looks like
269.. code:: idris
271 answerString : Bool -> String
272 answerString False = "Wrong answer"
273 answerString True = "Correct answer"
275Dependent Functions
276~~~~~~~~~~~~~~~~~~~
278Dependent functions are functions where the type of the return value
279depends on the input value. In order to define a dependent function,
280named parameters must be used, since the parameter will appear in the
281return type. For example, consider
283.. code:: idris
285 zeros : (n : Nat) -> Vect n Int
286 zeros Z = []
287 zeros (S k) = 0 :: (zeros k)
289In this example, the return type is ``Vect n Int`` which is an
290expression which depends on the input parameter ``n``.
292Anonymous
293~~~~~~~~~
295Arguments in anonymous functions are separated by comma.
297::
299 (\x => <expr>)
300 (\x, y => <expr>)
302Modifiers
303~~~~~~~~~
305Visibility
306^^^^^^^^^^
308::
310 public export
311 export
312 private
314Totality
315^^^^^^^^
317::
319 total
320 implicit
321 partial
322 covering
324Options
325^^^^^^^
327::
329 %export
330 %hint
331 %no_implicit
332 %error_handler
333 %error_reverse
334 %assert_total
335 %reflection
336 %specialise [<name list>]
338Misc
339----
341.. _syntax-qualified-names:
343Qualified Names
344~~~~~~~~~~~~~~~
346If multiple declarations with the same name are visible, using the name can
347result in an ambiguous situation. The compiler will attempt to resolve the
348ambiguity using the types involved. If it's unable --- for example, because
349the declarations with the same name also have the same type signatures --- the
350situation can be cleared up using a *qualified name*.
352A qualified name has the symbol's namespace prefixed, separated by a ``.``:
354.. code-block:: idris
356 Data.Vect.length
358This would specifically reference a ``length`` declaration from ``Data.Vect``.
360Qualified names can be written using two different shorthands:
3621. Names in modules that are :ref:`imported <syntax-imports>` using an alias
363 can be qualified by the alias.
3652. The name can be qualified by the *shortest unique suffix* of the
366 namespace in question. For example, the ``length`` case above can likely
367 be shortened to ``Vect.length``.
369.. _syntax-comments:
371Comments
372~~~~~~~~
374::
376 -- Single Line
377 {- Multiline -}
378 ||| Docstring (goes before definition)
380Multi line String literals
381~~~~~~~~~~~~~~~~~~~~~~~~~~
383::
385 foo = """
386 this is a
387 string literal"""
389.. _syntax-directives:
391Directives
392----------
394::
396 %lib <path>
397 %link <path>
398 %flag <path>
399 %include <path>
400 %hide <function>
401 %freeze <name>
402 %access <accessibility>
403 %default <totality>
404 %logging <level 0--11>
405 %dynamic <list of libs>
406 %name <list of names>
407 %error_handlers <list of names>
408 %language <extension>