This repository was archived by the owner on Nov 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathappx_a
264 lines (166 loc) · 9.4 KB
/
appx_a
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
Introduction to Gofer APPENDIX A: SUMMARY OF GRAMMAR
APPENDIX A: SUMMARY OF GRAMMAR
This section gives a summary of the grammar for the language used by
Gofer. The non-terminals <interp> and <module> describe the syntax of
expressions that can be entered into the Gofer interpreter and that of
files of definitions that can be loaded into Gofer respectively.
The following notational conventions are used in the Grammar which is
specified using a variant of BNF:
o <angle brackets> are used to distinguish names of nonterminals from
keywords.
o vertical | bars are used to separate alternatives.
o {braces} enclose items which may be repeated zero or more times.
o [brackets] are used for optional items.
o (parentheses) are used for grouping.
o "quotes" surround characters which might otherwise be confused with
the notations introduced above.
The following terminal symbols are used but not defined by the grammar:
VARID identifier beginning with lower case letter as described in
section 6.
CONID like VARID, but beginning with upper case letter.
VAROP operator symbol not beginning with a colon, as described in
section 6.
CONOP constructor function operator, like VAROP, but beginning
with a colon character.
INTEGER integer constant, as described in section 7.3.
FLOAT floating point constant, as described in section 7.4.
CHAR character constant, as described in section 7.5.
STRING string constant, as described in section 7.7.
Top-level grammar
-----------------
<module> ::= "{" <topdecls> "}" module
<interp> ::= <exp> [<where>] top-level expression
<topdecls> ::= <topdecls>; <topdecls> multiple declarations
| data <typeLhs> = <constrs> datatype declaration
| type <typeLhs> = <type> synonym declaration
| infixl [<digit>] <op> {, <op>} fixity declarations
| infixr [<digit>] <op> {, <op>}
| infix [<digit>] <op> {, <op>}
| primitive <prims> :: <type> primitive bindings
| <class> class declaration
| <inst> instance declaration
| <decls> value declarations
93
Introduction to Gofer APPENDIX A: SUMMARY OF GRAMMAR
<typeLhs> ::= CONID {VARID} type declaration lhs
<constrs> ::= <constrs> "|" <constrs> multiple constructors
| <type> CONOP <type> infix constructor
| CONID {<type>} constructor, n>=0
<prims> ::= <prims>, <prims> multiple bindings
| <var> <string> primitive binding
Type expressions
----------------
<sigType> ::= [<context> => ] <type> [qualified] type
<context> ::= "(" [<pred> {, <pred>}] ")" general form
| <pred> singleton context
<pred> ::= CONID <type> {<type>} predicate
<type> ::= <ctype> [ -> <type> ] function type
<ctype> ::= CONID {<atype>} datatype or synonym
| <atype>
<atype> ::= VARID type variable
| "(" ")" unit type
| "(" <type> ")" parenthesised type
| "(" <type>,<type> {,<type>} ")" tuple type
| "[" <type> "]" list type
Class and instance declarations
-------------------------------
<class> ::= class [<context> =>] <pred> [<cbody>]
<cbody> ::= where "{" <cdecls> "}" class body
<cdecls> ::= <cdecls>; <cdecls> multiple declarations
| <var> {, <var>} :: <type> member functions
| <fun> <rhs> [<where>] default bindings
<inst> ::= instance [<context> =>] <pred> [<ibody>]
<ibody> ::= where "{" <idecls> "}" instance body
<idecls> ::= <idecls>; <idecls> multiple declarations
| <fun> <rhs> [<where>] member definition
Value declarations
------------------
<decls> ::= <decls>; <decls> multiple declarations
| <var> {, <var>} :: <sigType> type declaration
| <fun> <rhs> [<where>] function binding
| <pat> <rhs> [<where>] pattern binding
<rhs> ::= = <exp> simple right hand side
| <gdRhs> {<gdRhs>} guarded right hand sides
<gdRhs> ::= "|" <exp> = <exp> guarded right hand side
<where> ::= where "{" <decls> "}" local definitions
94
Introduction to Gofer APPENDIX A: SUMMARY OF GRAMMAR
<fun> ::= <var> function of arity 0
| <pat> <varop> <pat> infix operator
| "(" <pat> <varop> ")" section-like notation
| "(" <varop> <pat> ")"
| <fun> <apat> function with argument
| "(" <fun> ")" parenthesised lhs
Expressions
-----------
<exp> ::= \ <apat> {<apat>} -> <exp> lambda expression
| let "{" <decls> "}" in <exp> local definition
| if <exp> then <exp> else <exp> conditional expression
| case <exp> of "{" <alts> "}" case expression
| <opExp> :: <sigType> typed expression
| <opExp>
<opExp> ::= <opExp> <op> <opExp> operator application
| <pfxExp>
<pfxExp> ::= - <appExp> negation
| <appExp>
<appExp> ::= <appExp> <atomic> function application
| <atomic>
<atomic> ::= <var> variable
| <conid> constructor
| INTEGER integer literal
| FLOAT floating point literal
| CHAR character literal
| STRING string literal
| "(" ")" unit element
| "(" <exp> ")" parenthesised expr.
| (<exp> <op>) sections
| (<op> <exp>)
| "[" <list> "]" list expression
| "(" <exp>, <exp> {, <exp>} ")" tuple
<list> ::= [ <exp> {, <exp>} ] enumerated list
| <exp> "|" <quals> list comprehension
| <exp> .. arithmetic sequence
| <exp>, <exp> ..
| <exp> .. <exp>
| <exp>, <exp> .. <exp>
<quals> ::= <quals>, <quals> multiple qualifiers
| <pat> <- <exp> generator
| <pat> = <exp> local definition
| <exp> boolean guard
<alts> ::= <alts>; <alts> multiple alternatives
| <pat> <altRhs> [<where>] alternative
<altRhs> ::= -> <exp> single alternative
| <gdAlt> {<gdAlt>} guarded alternatives
<gdAlt> ::= "|" <exp> -> <exp> guarded alternative
Patterns
--------
<pat> ::= <pat> <conop> <pat> operator application
95
Introduction to Gofer APPENDIX A: SUMMARY OF GRAMMAR
| <var> + <integer> (n+k) pattern
| <appPat>
<appPat> ::= <appPat> <apat> application
| <apat>
<apat> ::= <var> variable
| <var> @ <pat> as pattern
| ~ <pat> irrefutable pattern
| _ wildcard
| <conid> constructor
| INTEGER integer literal
| CHAR character literal
| STRING string literal
| "(" ")" unit element
| "(" <pat> ")" parenthesised expr.
| (<pat> <conop>) sections
| (<conop> <pat>)
| "[" [ <pat> {, <pat>} ] "]" list
| "(" <pat>, <pat> {, <pat>} ")" tuple
Variables and operators
-----------------------
<var> ::= <varid> | "(" - ")" variable
<op> ::= <varop> | <conop> | - operator
<varid> ::= VARID | "(" VAROP ")" variable identifier
<varop> ::= VAROP | ` VARID ` variable operator
<conid> ::= CONID | "(" CONOP ")" constructor identifier
<conop> ::= CONOP | ` CONID ` constructor operator
96