summaryrefslogtreecommitdiff
path: root/miralib/manual/7
blob: bd9e10218ee95b1f0eace2dbf2fd2e634f520ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
_E_x_p_r_e_s_s_i_o_n_s

In Miranda an expression denotes a  value.   Expressions  occur  in  two
contexts,  but  have  the same(*) syntax in both situations.  First, the
basic mode of operation of the Miranda command interpreter  is  that  it
evaluates   expressions   typed   at  the  terminal  (these  are  called
`command-level expressions').   Second,  expressions  are  an  important
syntactic  component  of  _s_c_r_i_p_t_s  (because  scripts  are collections of
definitions, and the right hand sides of  definitions  are  composed  of
expressions).

Expressions typed at the terminal are volatile - only by being made part
of a script can an expression be saved for future use.

An  expression  is  either  simple,  or  a  function  application, or an
operator expression, or an operator.

_A_ _s_i_m_p_l_e_ _e_x_p_r_e_s_s_i_o_n_ _i_s_ _o_n_e_ _o_f_ _t_h_e_ _f_o_l_l_o_w_i_n_g:

Identifier: (see separate manual entry) these are of two kinds based  on
the case of the initial letter
 - variable e.g. `x' or `map'
 - constructor e.g. `X' or `True'

Literal, e.g. 136 or "fruit" (see separate manual entry)

An operator section (see separate manual entry)

The keyword `_s_h_o_w' or `_r_e_a_d_v_a_l_s' (see separate manual entries)

A list, such as `[1,5,7,9]' or a `dotdot' list or a  list  comprehension
(see manual entry on iterative expressions).

A tuple, such as `(True,"green",37)'.  Tuples differ from lists in  that
they  can  have  components  of mixed type.  They are always enclosed in
round parentheses.  The empty tuple, which has no components, is written
`()'.   Otherwise,  a tuple must have at least two components - there is
no concept of a one-tuple.  Tuples  cannot  be  subscripted,  but  their
components  can  be  extracted  by  pattern matching.  Since there is no
concept of a one-tuple, the use of parentheses  for  grouping  does  not
conflict with their use for tuple formation.

Any expression enclosed in parentheses is a simple expression.

_F_u_n_c_t_i_o_n_ _a_p_p_l_i_c_a_t_i_o_n
 is denoted by juxtaposition, and is left associative, so e.g.
	f a b

denotes a curried function application  of  two  arguments.   (So  f  is
really  a  function  of  one argument whose result is another function -
thus `f a b' is actually parsed as `(f a) b' -  the  advantage  of  this
arrangement  is  that  `f  a'  has  a  meaning in its own right, it is a
partially applied version of f.)

_O_p_e_r_a_t_o_r_ _e_x_p_r_e_s_s_i_o_n_s
	e.g. `5*x-17'
There  are  a  variety of infix and prefix operators, of various binding
powers (see manual entry on operators).  Note that function  application
is more binding than any operator.

An  operator  on  its  own  can be used as the name of the corresponding
function, as shown in the following examples
	arith2_ops = [+,-,*,/,div,mod,^]
	sum = foldr (+) 0

both of which are legal definitions.  Note  that  when  an  operator  is
passed as a parameter it needs to be parenthesised, to force the correct
parse.  An ambiguity arises in the case of `-' which has two meanings as
an  operator  (infix  and prefix) - the convention is that `-' occurring
alone always refers to the infix (i.e. dyadic) case.  The name `neg'  is
provided  for  the  unary  minus  function,  as  part  of  the  standard
environment.

A formal syntax for expressions can  be  found  in  the  manual  section
called `Formal syntax of Miranda scripts'.

(*) _N_o_t_e  There  is  one  exception  to  the  rule  that  command  level
expressions  have  the  same  syntax as expressions inside scripts - the
notation `$$', meaning the last expression evaluated, is allowed only in
command level expressions.