-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplicitconcepts.nim
More file actions
290 lines (254 loc) · 8.96 KB
/
explicitconcepts.nim
File metadata and controls
290 lines (254 loc) · 8.96 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#[
Explicit Concepts.
Copyright 2017 Gerd Mathar
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]#
## This module provides explicit concepts and an ``implements``-relation
## between concepts and implementing types.
##
## Motivation
## ==========
##
## A type satisfies a regular concept if it matches the requirements defined
## by the concept. This is an implicit match, the creator of the type did
## not declare intent to make the type satisfy the concept. While this is often
## useful, in other cases, stating an ``implements``-relation between the
## type and the concept in the source code to make the intention clear produces
## more readable and safer code: An explicit concept is only satisfied by a
## type if such an ``implements``-relation exists.
##
## Use
## ====
##
## .. code-block:: nim
## implements C, ExC: X
## Defines ``implements``-relations for an existing type and existing concepts:
## ``X`` implements concepts ``C`` and ``ExC``.
##
## .. code-block:: nim
## explicit:
## type
## ExD = concept d
## d.x is float
## Defines an explicit concept ``ExD``.
##
## .. code-block:: nim
## implements ExD:
## type
## Xx = object
## x: float
##
## Y = object
## x: float
##
## type
## Y2 = object
## x: float
##
## echo(Y is ExD) # -> true
## echo(Y2 is ExD) # -> false
## Defines ``implements``-relations for new types: ``Xx`` and ``Y``
## implement concept ``ExD``. Note that, despite the fact that it fulfills
## the requirement in the body of ``ExD``, ``Y2`` does not satisfy ``ExD``
## because ``ExD`` is explicit and there is no ``implements``-relation
## defined between the two.
##
## The ``implements``-relation between a type and a concept automatically
## extends to aliases of the two and to derivatives of the type, but not to
## distinct aliases of the two and to refinements of the concept.
##
## For details see the source files in the ``tests`` directory.
import strutils, macros
const
magic = "9F08B7C91364CDF2"
concVarTyName = "T" & magic
type
ConceptId = string
ConceptCompanion[id: static[ConceptId]] = distinct auto
ConceptInfo = tuple[tdef, cdef: NimNode]
template typeDefId(td: untyped): string = td.lineInfo
proc toId(ci: ConceptInfo): ConceptId =
# TODO: probably include cdef itself for generic instantiations.
ci.tdef.typeDefId
proc conceptInfo(c: NimNode): ConceptInfo
proc conceptInfo(ci: ConceptInfo): ConceptInfo =
let
ti = getImpl(ci.cdef[0].symbol)
if nnkNilLit == ti.kind:
error("no type implementation found.", ci.cdef[0])
if not ti.findChild(nnkBracketExpr == it.kind).isNil:
error("generic concepts are not yet supported.", ci.cdef[0])
case ti[2].kind
of nnkSym:
# type alias: resolve original type definition instead.
ti[2].conceptInfo
of nnkDistinctTy:
# distinct type: keep the distinct def, resolve original type.
(ti, ti[2][0].conceptInfo.cdef)
of nnkTypeClassTy:
# actual concept definition: return it.
(ti, ti)
else:
# not even a concept.
(ci.tdef, nil)
proc conceptInfo(c: NimNode): ConceptInfo =
# Returns a tuple containing the resolved actual symbol and definition nodes
# of the passed concept symbol node.
let cd = c.symbol.getImpl
(cd, cd).conceptInfo
proc id(c: NimNode): ConceptId =
let ci = c.conceptInfo
if ci.cdef.isNil:
error($c.symbol & " is not a concept.", c)
ci.toId
template flagProcDef(t: typed, cid: ConceptId): untyped =
let m = magic
# The existence of this proc signals an ``implemements``-relation.
proc `impl m`*(Ty: typedesc[t], Co: typedesc[ConceptCompanion[cid]])
{.compileTime.} = discard
template flagProcCall(t: typed, cid: ConceptId): untyped =
let m = magic
`impl m`(t, ConceptCompanion[cid])
macro checkImplements*(t, c: typed): untyped =
## Produces the code to check wether there is an ``implemements``-relation
## between the type and the concept bound to the passed symbol nodes ``t``
## and ``c``, respectively.
newStmtList(newCall("compiles", getAst flagProcCall(t, c.id)))
template procDef(t: typed, cid: ConceptId, warn: bool, cName: string): untyped =
when compiles(flagProcCall(t, cid)):
when warn:
{.warning: "redundant implements-relation for " &
cName & " is ignored.".}
else:
flagProcDef(t, cid)
proc baseConcepts(cDef: NimNode): NimNode =
cDef.findChild(nnkTypeClassTy == it.kind)
.findChild(nnkOfInherit == it.kind)
proc isExplicit(ci: ConceptInfo): bool =
result = false
if ci.cdef.isNil:
error($ci.tdef[0].symbol & " is not a concept.", ci.tdef[0])
let args = ci.cdef.last[0]
for arg in args:
result = nnkTypeOfExpr == arg.kind and concVarTyName == $arg[0].ident
if result:
break
proc isExplicit(c: NimNode): bool =
c.conceptInfo.isExplicit
proc implBy(c, t, stmts: NimNode, warn: bool = true) =
let ci = c.conceptInfo
if ci.cdef.isNil:
error($c.symbol & " is not a concept.", c)
let bases = baseConcepts(ci.cdef)
if not bases.isNil:
for b in bases:
if b.isExplicit:
implBy(b, t, stmts, false)
stmts.add getAst procDef(t, ci.toId, warn, c.repr)
if not ci.isExplicit:
if warn:
warning(("$# is implicit, the implements-relation with $# will not " &
"be checked.") % [c.repr, t.repr])
macro implementedBy(c, t: typed): untyped =
## Establishes an ``implements``-relation between type ``t`` and concept
## ``c`` and all of its base concepts.
result = newStmtList()
implBy(c, t, result)
#echo result.repr
proc implStmts(args, t: NimNode): seq[NimNode] {.compileTime.} =
result = @[]
for c in args:
if nnkStmtList == c.kind:
break
let msg = "$# does not satisfy $#." % [t.repr, c.repr]
result.add quote do:
implementedBy(`c`, `t`)
when not(`t` is `c`):
{.warning: `msg`.}
macro implements*(args: varargs[untyped]): untyped =
## Establishes an ``implements``-relation between concepts given
## as leading arguments and an existing type or the types defined in type
## sections given as a trailing block argument.
result = newStmtList()
args.expectKind(nnkArglist)
if args.len == 0 or nnkStmtList == args[0].kind:
error("implemented concepts expected.", args)
let stmts = args.findChild(nnkStmtList == it.kind)
if isNil(stmts) or stmts.len == 0:
error("implementing type or type sections expected.", args)
if stmts.len == 1 and nnkTypeSection != stmts[0].kind:
result.add implStmts(args, stmts[0])
else:
for ts in stmts:
ts.expectKind(nnkTypeSection)
result.add ts
for td in ts:
result.add implStmts(args, td[0])
#echo result.repr
template refinedConc(ty, base: untyped): untyped =
type ty = concept c of base
c is base # TODO: seems to be necessary?
proc identNode(typeDef: NimNode): NimNode =
case typeDef[0].kind
of nnkPragmaExpr:
typeDef[0].identNode # this is not a nnkTypeDef node, but the recursion works
else:
typeDef[0].basename
proc expl(typeSects: NimNode): NimNode =
## Makes the concepts defined in the type sections passed as a block argument
## explicit.
typeSects.expectKind(nnkStmtList)
if typeSects.len == 0:
error("concept type definitions expected.", typeSects)
result = newStmtList()
for ts in typeSects:
ts.expectKind(nnkTypeSection)
for td in ts:
var concDef: NimNode
let
ty = td[0]
tidn = td.identNode
tSpec = td.last
case tSpec.kind
of nnkIdent:
error($tidn.ident & " is an alias and cannot be explicit.", ty)
of nnkDistinctTy:
# replace distinct concept with refined one.
let
refiConc = getAst refinedConc(ty, tSpec[0].ident)
concDef = td
concDef[2] = refiConc[0][0][2]
of nnkTypeClassTy:
concDef = td
else:
error($tidn.ident & " is not a concept.", ty)
let
tc = concDef.last
concVarTy = !concVarTyName
if nnkEmpty == tc.last.kind:
# add a concept body where necessary.
tc.del tc.len-1
tc.add newStmtList()
# add a matched type variable..
tc[0].add newTree(nnkTypeOfExpr, newIdentNode concVarTy)
# ..and the check for an implements-relation.
let
tdid = td.typeDefId
implCheck = getAst flagProcCall(`concVarTy`, `tdid`)
tc.last.add implCheck
result.add newTree(nnkTypeSection, concDef)
macro explicit*(typeSects: untyped): untyped =
result = expl typeSects
#echo result.repr
macro explicit*(typeSects, post: untyped): untyped =
result = expl typeSects
for stmt in post:
result.add stmt