File size: 20,076 Bytes
d916065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
.. Copyright (C) 2001-2023 NLTK Project
.. For license information, see LICENSE.TXT

================================
 Discourse Representation Theory
================================

    >>> from nltk.sem import logic
    >>> from nltk.inference import TableauProver

Overview
========

A DRS can be created with the ``DRS()`` constructor. This takes two arguments: a list of
discourse referents and list of conditions. .

    >>> from nltk.sem.drt import *
    >>> dexpr = DrtExpression.fromstring
    >>> man_x = dexpr('man(x)')
    >>> walk_x = dexpr('walk(x)')
    >>> x = dexpr('x')
    >>> print(DRS([x], [man_x, walk_x]))
    ([x],[man(x), walk(x)])

The ``parse()`` method can also be applied directly to DRS
expressions, which allows them to be specified more
easily.

    >>> drs1 = dexpr('([x],[man(x),walk(x)])')
    >>> print(drs1)
    ([x],[man(x), walk(x)])

DRSs can be *merged* using the ``+`` operator.

    >>> drs2 = dexpr('([y],[woman(y),stop(y)])')
    >>> drs3 = drs1 + drs2
    >>> print(drs3)
    (([x],[man(x), walk(x)]) + ([y],[woman(y), stop(y)]))
    >>> print(drs3.simplify())
    ([x,y],[man(x), walk(x), woman(y), stop(y)])

We can embed DRSs as components of an ``implies`` condition.

    >>> s = '([], [(%s -> %s)])' % (drs1, drs2)
    >>> print(dexpr(s))
    ([],[(([x],[man(x), walk(x)]) -> ([y],[woman(y), stop(y)]))])



The ``fol()`` method converts DRSs into FOL formulae.



    >>> print(dexpr(r'([x],[man(x), walks(x)])').fol())

    exists x.(man(x) & walks(x))

    >>> print(dexpr(r'([],[(([x],[man(x)]) -> ([],[walks(x)]))])').fol())

    all x.(man(x) -> walks(x))



In order to visualize a DRS, the ``pretty_format()`` method can be used.



    >>> print(drs3.pretty_format())

      _________     __________

     | x       |   | y        |

    (|---------| + |----------|)

     | man(x)  |   | woman(y) |

     | walk(x) |   | stop(y)  |

     |_________|   |__________|





Parse to semantics

------------------



..

    >>> logic._counter._value = 0



DRSs can be used for building compositional semantics in a feature

based grammar. To specify that we want to use DRSs, the appropriate

logic parser needs be passed as a parameter to ``load_earley()``



    >>> from nltk.parse import load_parser

    >>> from nltk.sem.drt import DrtParser

    >>> parser = load_parser('grammars/book_grammars/drt.fcfg', trace=0, logic_parser=DrtParser())

    >>> for tree in parser.parse('a dog barks'.split()):

    ...     print(tree.label()['SEM'].simplify())

    ...

    ([x],[dog(x), bark(x)])



Alternatively, a ``FeatStructReader`` can be passed with the ``logic_parser`` set on it



    >>> from nltk.featstruct import FeatStructReader

    >>> from nltk.grammar import FeatStructNonterminal

    >>> parser = load_parser('grammars/book_grammars/drt.fcfg', trace=0, fstruct_reader=FeatStructReader(fdict_class=FeatStructNonterminal, logic_parser=DrtParser()))

    >>> for tree in parser.parse('every girl chases a dog'.split()):

    ...     print(tree.label()['SEM'].simplify().normalize())

    ...

    ([],[(([z1],[girl(z1)]) -> ([z2],[dog(z2), chase(z1,z2)]))])







Unit Tests

==========



Parser

------



    >>> print(dexpr(r'([x,y],[sees(x,y)])'))

    ([x,y],[sees(x,y)])

    >>> print(dexpr(r'([x],[man(x), walks(x)])'))

    ([x],[man(x), walks(x)])

    >>> print(dexpr(r'\x.([],[man(x), walks(x)])'))

    \x.([],[man(x), walks(x)])

    >>> print(dexpr(r'\x.\y.([],[sees(x,y)])'))

    \x y.([],[sees(x,y)])



    >>> print(dexpr(r'([x,y],[(x = y)])'))

    ([x,y],[(x = y)])

    >>> print(dexpr(r'([x,y],[(x != y)])'))

    ([x,y],[-(x = y)])



    >>> print(dexpr(r'\x.([],[walks(x)])(john)'))

    (\x.([],[walks(x)]))(john)

    >>> print(dexpr(r'\R.\x.([],[big(x,R)])(\y.([],[mouse(y)]))'))

    (\R x.([],[big(x,R)]))(\y.([],[mouse(y)]))



    >>> print(dexpr(r'(([x],[walks(x)]) + ([y],[runs(y)]))'))

    (([x],[walks(x)]) + ([y],[runs(y)]))

    >>> print(dexpr(r'(([x,y],[walks(x), jumps(y)]) + (([z],[twos(z)]) + ([w],[runs(w)])))'))

    (([x,y],[walks(x), jumps(y)]) + ([z],[twos(z)]) + ([w],[runs(w)]))

    >>> print(dexpr(r'((([],[walks(x)]) + ([],[twos(x)])) + ([],[runs(x)]))'))

    (([],[walks(x)]) + ([],[twos(x)]) + ([],[runs(x)]))

    >>> print(dexpr(r'((([],[walks(x)]) + ([],[runs(x)])) + (([],[threes(x)]) + ([],[fours(x)])))'))

    (([],[walks(x)]) + ([],[runs(x)]) + ([],[threes(x)]) + ([],[fours(x)]))



    >>> print(dexpr(r'(([],[walks(x)]) -> ([],[runs(x)]))'))

    (([],[walks(x)]) -> ([],[runs(x)]))



    >>> print(dexpr(r'([x],[PRO(x), sees(John,x)])'))

    ([x],[PRO(x), sees(John,x)])

    >>> print(dexpr(r'([x],[man(x), -([],[walks(x)])])'))

    ([x],[man(x), -([],[walks(x)])])

    >>> print(dexpr(r'([],[(([x],[man(x)]) -> ([],[walks(x)]))])'))

    ([],[(([x],[man(x)]) -> ([],[walks(x)]))])



    >>> print(dexpr(r'DRS([x],[walk(x)])'))

    ([x],[walk(x)])

    >>> print(dexpr(r'DRS([x][walk(x)])'))

    ([x],[walk(x)])

    >>> print(dexpr(r'([x][walk(x)])'))

    ([x],[walk(x)])



``simplify()``

--------------



    >>> print(dexpr(r'\x.([],[man(x), walks(x)])(john)').simplify())

    ([],[man(john), walks(john)])

    >>> print(dexpr(r'\x.\y.([z],[dog(z),sees(x,y)])(john)(mary)').simplify())

    ([z],[dog(z), sees(john,mary)])

    >>> print(dexpr(r'\R x.([],[big(x,R)])(\y.([],[mouse(y)]))').simplify())

    \x.([],[big(x,\y.([],[mouse(y)]))])



    >>> print(dexpr(r'(([x],[walks(x)]) + ([y],[runs(y)]))').simplify())

    ([x,y],[walks(x), runs(y)])

    >>> print(dexpr(r'(([x,y],[walks(x), jumps(y)]) + (([z],[twos(z)]) + ([w],[runs(w)])))').simplify())

    ([w,x,y,z],[walks(x), jumps(y), twos(z), runs(w)])

    >>> print(dexpr(r'((([],[walks(x)]) + ([],[runs(x)]) + ([],[threes(x)]) + ([],[fours(x)])))').simplify())

    ([],[walks(x), runs(x), threes(x), fours(x)])

    >>> dexpr(r'([x],[man(x)])+([x],[walks(x)])').simplify() == \

    ... dexpr(r'([x,z1],[man(x), walks(z1)])')

    True

    >>> dexpr(r'([y],[boy(y), (([x],[dog(x)]) -> ([],[chase(x,y)]))])+([x],[run(x)])').simplify() == \

    ... dexpr(r'([y,z1],[boy(y), (([x],[dog(x)]) -> ([],[chase(x,y)])), run(z1)])')

    True



    >>> dexpr(r'\Q.(([x],[john(x),walks(x)]) + Q)(([x],[PRO(x),leaves(x)]))').simplify() == \

    ... dexpr(r'([x,z1],[john(x), walks(x), PRO(z1), leaves(z1)])')

    True



    >>> logic._counter._value = 0

    >>> print(dexpr('([],[(([x],[dog(x)]) -> ([e,y],[boy(y), chase(e), subj(e,x), obj(e,y)]))])+([e,x],[PRO(x), run(e), subj(e,x)])').simplify().normalize().normalize())

    ([e02,z5],[(([z3],[dog(z3)]) -> ([e01,z4],[boy(z4), chase(e01), subj(e01,z3), obj(e01,z4)])), PRO(z5), run(e02), subj(e02,z5)])



``fol()``

-----------



    >>> print(dexpr(r'([x,y],[sees(x,y)])').fol())

    exists x y.sees(x,y)

    >>> print(dexpr(r'([x],[man(x), walks(x)])').fol())

    exists x.(man(x) & walks(x))

    >>> print(dexpr(r'\x.([],[man(x), walks(x)])').fol())

    \x.(man(x) & walks(x))

    >>> print(dexpr(r'\x y.([],[sees(x,y)])').fol())

    \x y.sees(x,y)



    >>> print(dexpr(r'\x.([],[walks(x)])(john)').fol())

    \x.walks(x)(john)

    >>> print(dexpr(r'\R x.([],[big(x,R)])(\y.([],[mouse(y)]))').fol())

    (\R x.big(x,R))(\y.mouse(y))



    >>> print(dexpr(r'(([x],[walks(x)]) + ([y],[runs(y)]))').fol())

    (exists x.walks(x) & exists y.runs(y))



    >>> print(dexpr(r'(([],[walks(x)]) -> ([],[runs(x)]))').fol())

    (walks(x) -> runs(x))



    >>> print(dexpr(r'([x],[PRO(x), sees(John,x)])').fol())

    exists x.(PRO(x) & sees(John,x))

    >>> print(dexpr(r'([x],[man(x), -([],[walks(x)])])').fol())

    exists x.(man(x) & -walks(x))

    >>> print(dexpr(r'([],[(([x],[man(x)]) -> ([],[walks(x)]))])').fol())

    all x.(man(x) -> walks(x))



    >>> print(dexpr(r'([x],[man(x) | walks(x)])').fol())

    exists x.(man(x) | walks(x))

    >>> print(dexpr(r'P(x) + ([x],[walks(x)])').fol())

    (P(x) & exists x.walks(x))



``resolve_anaphora()``

----------------------



    >>> from nltk.sem.drt import AnaphoraResolutionException



    >>> print(resolve_anaphora(dexpr(r'([x,y,z],[dog(x), cat(y), walks(z), PRO(z)])')))

    ([x,y,z],[dog(x), cat(y), walks(z), (z = [x,y])])

    >>> print(resolve_anaphora(dexpr(r'([],[(([x],[dog(x)]) -> ([y],[walks(y), PRO(y)]))])')))

    ([],[(([x],[dog(x)]) -> ([y],[walks(y), (y = x)]))])

    >>> print(resolve_anaphora(dexpr(r'(([x,y],[]) + ([],[PRO(x)]))')).simplify())

    ([x,y],[(x = y)])

    >>> try: print(resolve_anaphora(dexpr(r'([x],[walks(x), PRO(x)])')))

    ... except AnaphoraResolutionException as e: print(e)

    Variable 'x' does not resolve to anything.

    >>> print(resolve_anaphora(dexpr('([e01,z6,z7],[boy(z6), PRO(z7), run(e01), subj(e01,z7)])')))

    ([e01,z6,z7],[boy(z6), (z7 = z6), run(e01), subj(e01,z7)])



``equiv()``:

----------------



    >>> a = dexpr(r'([x],[man(x), walks(x)])')

    >>> b = dexpr(r'([x],[walks(x), man(x)])')

    >>> print(a.equiv(b, TableauProver()))

    True





``replace()``:

--------------



    >>> a = dexpr(r'a')

    >>> w = dexpr(r'w')

    >>> x = dexpr(r'x')

    >>> y = dexpr(r'y')

    >>> z = dexpr(r'z')





replace bound

-------------



    >>> print(dexpr(r'([x],[give(x,y,z)])').replace(x.variable, a, False))

    ([x],[give(x,y,z)])

    >>> print(dexpr(r'([x],[give(x,y,z)])').replace(x.variable, a, True))

    ([a],[give(a,y,z)])



replace unbound

---------------



    >>> print(dexpr(r'([x],[give(x,y,z)])').replace(y.variable, a, False))

    ([x],[give(x,a,z)])

    >>> print(dexpr(r'([x],[give(x,y,z)])').replace(y.variable, a, True))

    ([x],[give(x,a,z)])



replace unbound with bound

--------------------------



    >>> dexpr(r'([x],[give(x,y,z)])').replace(y.variable, x, False) == \

    ... dexpr('([z1],[give(z1,x,z)])')

    True

    >>> dexpr(r'([x],[give(x,y,z)])').replace(y.variable, x, True) == \

    ... dexpr('([z1],[give(z1,x,z)])')

    True



replace unbound with unbound

----------------------------



    >>> print(dexpr(r'([x],[give(x,y,z)])').replace(y.variable, z, False))

    ([x],[give(x,z,z)])

    >>> print(dexpr(r'([x],[give(x,y,z)])').replace(y.variable, z, True))

    ([x],[give(x,z,z)])





replace unbound

---------------



    >>> print(dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,z)])').replace(z.variable, a, False))

    (([x],[P(x,y,a)]) + ([y],[Q(x,y,a)]))

    >>> print(dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,z)])').replace(z.variable, a, True))

    (([x],[P(x,y,a)]) + ([y],[Q(x,y,a)]))



replace bound

-------------



    >>> print(dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,z)])').replace(x.variable, a, False))

    (([x],[P(x,y,z)]) + ([y],[Q(x,y,z)]))

    >>> print(dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,z)])').replace(x.variable, a, True))

    (([a],[P(a,y,z)]) + ([y],[Q(a,y,z)]))



replace unbound with unbound

----------------------------



    >>> print(dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,z)])').replace(z.variable, a, False))

    (([x],[P(x,y,a)]) + ([y],[Q(x,y,a)]))

    >>> print(dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,z)])').replace(z.variable, a, True))

    (([x],[P(x,y,a)]) + ([y],[Q(x,y,a)]))



replace unbound with bound on same side

---------------------------------------



    >>> dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,w)])').replace(z.variable, x, False) == \

    ... dexpr(r'(([z1],[P(z1,y,x)]) + ([y],[Q(z1,y,w)]))')

    True

    >>> dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,w)])').replace(z.variable, x, True) == \

    ... dexpr(r'(([z1],[P(z1,y,x)]) + ([y],[Q(z1,y,w)]))')

    True



replace unbound with bound on other side

----------------------------------------



    >>> dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,w)])').replace(w.variable, x, False) == \

    ... dexpr(r'(([z1],[P(z1,y,z)]) + ([y],[Q(z1,y,x)]))')

    True

    >>> dexpr(r'([x],[P(x,y,z)])+([y],[Q(x,y,w)])').replace(w.variable, x, True) == \

    ... dexpr(r'(([z1],[P(z1,y,z)]) + ([y],[Q(z1,y,x)]))')

    True



replace unbound with double bound

---------------------------------



    >>> dexpr(r'([x],[P(x,y,z)])+([x],[Q(x,y,w)])').replace(z.variable, x, False) == \

    ... dexpr(r'(([z1],[P(z1,y,x)]) + ([z1],[Q(z1,y,w)]))')

    True

    >>> dexpr(r'([x],[P(x,y,z)])+([x],[Q(x,y,w)])').replace(z.variable, x, True) == \

    ... dexpr(r'(([z1],[P(z1,y,x)]) + ([z1],[Q(z1,y,w)]))')

    True





regression tests

----------------



    >>> d = dexpr('([x],[A(c), ([y],[B(x,y,z,a)])->([z],[C(x,y,z,a)])])')

    >>> print(d)

    ([x],[A(c), (([y],[B(x,y,z,a)]) -> ([z],[C(x,y,z,a)]))])

    >>> print(d.pretty_format())

     ____________________________________

    | x                                  |

    |------------------------------------|

    | A(c)                               |

    |   ____________      ____________   |

    |  | y          |    | z          |  |

    | (|------------| -> |------------|) |

    |  | B(x,y,z,a) |    | C(x,y,z,a) |  |

    |  |____________|    |____________|  |

    |____________________________________|

    >>> print(str(d))

    ([x],[A(c), (([y],[B(x,y,z,a)]) -> ([z],[C(x,y,z,a)]))])

    >>> print(d.fol())

    exists x.(A(c) & all y.(B(x,y,z,a) -> exists z.C(x,y,z,a)))

    >>> print(d.replace(Variable('a'), DrtVariableExpression(Variable('r'))))

    ([x],[A(c), (([y],[B(x,y,z,r)]) -> ([z],[C(x,y,z,r)]))])

    >>> print(d.replace(Variable('x'), DrtVariableExpression(Variable('r'))))

    ([x],[A(c), (([y],[B(x,y,z,a)]) -> ([z],[C(x,y,z,a)]))])

    >>> print(d.replace(Variable('y'), DrtVariableExpression(Variable('r'))))

    ([x],[A(c), (([y],[B(x,y,z,a)]) -> ([z],[C(x,y,z,a)]))])

    >>> print(d.replace(Variable('z'), DrtVariableExpression(Variable('r'))))

    ([x],[A(c), (([y],[B(x,y,r,a)]) -> ([z],[C(x,y,z,a)]))])

    >>> print(d.replace(Variable('x'), DrtVariableExpression(Variable('r')), True))

    ([r],[A(c), (([y],[B(r,y,z,a)]) -> ([z],[C(r,y,z,a)]))])

    >>> print(d.replace(Variable('y'), DrtVariableExpression(Variable('r')), True))

    ([x],[A(c), (([r],[B(x,r,z,a)]) -> ([z],[C(x,r,z,a)]))])

    >>> print(d.replace(Variable('z'), DrtVariableExpression(Variable('r')), True))

    ([x],[A(c), (([y],[B(x,y,r,a)]) -> ([r],[C(x,y,r,a)]))])

    >>> print(d == dexpr('([l],[A(c), ([m],[B(l,m,z,a)])->([n],[C(l,m,n,a)])])'))

    True

    >>> d = dexpr('([],[([x,y],[B(x,y,h), ([a,b],[dee(x,a,g)])])->([z,w],[cee(x,y,f), ([c,d],[E(x,c,d,e)])])])')

    >>> sorted(d.free())

    [Variable('B'), Variable('E'), Variable('e'), Variable('f'), Variable('g'), Variable('h')]

    >>> sorted(d.variables())

    [Variable('B'), Variable('E'), Variable('e'), Variable('f'), Variable('g'), Variable('h')]

    >>> sorted(d.get_refs(True))

    [Variable('a'), Variable('b'), Variable('c'), Variable('d'), Variable('w'), Variable('x'), Variable('y'), Variable('z')]

    >>> sorted(d.conds[0].get_refs(False))

    [Variable('x'), Variable('y')]

    >>> print(dexpr('([x,y],[A(x,y), (x=y), ([],[B(x,y)])->([],[C(x,y)]), ([x,y],[D(x,y)])->([],[E(x,y)]), ([],[F(x,y)])->([x,y],[G(x,y)])])').eliminate_equality())

    ([x],[A(x,x), (([],[B(x,x)]) -> ([],[C(x,x)])), (([x,y],[D(x,y)]) -> ([],[E(x,y)])), (([],[F(x,x)]) -> ([x,y],[G(x,y)]))])

    >>> print(dexpr('([x,y],[A(x,y), (x=y)]) -> ([],[B(x,y)])').eliminate_equality())

    (([x],[A(x,x)]) -> ([],[B(x,x)]))

    >>> print(dexpr('([x,y],[A(x,y)]) -> ([],[B(x,y), (x=y)])').eliminate_equality())

    (([x,y],[A(x,y)]) -> ([],[B(x,x)]))

    >>> print(dexpr('([x,y],[A(x,y), (x=y), ([],[B(x,y)])])').eliminate_equality())

    ([x],[A(x,x), ([],[B(x,x)])])

    >>> print(dexpr('([x,y],[A(x,y), ([],[B(x,y), (x=y)])])').eliminate_equality())

    ([x,y],[A(x,y), ([],[B(x,x)])])

    >>> print(dexpr('([z8 z9 z10],[A(z8), z8=z10, z9=z10, B(z9), C(z10), D(z10)])').eliminate_equality())

    ([z9],[A(z9), B(z9), C(z9), D(z9)])



    >>> print(dexpr('([x,y],[A(x,y), (x=y), ([],[B(x,y)]), ([x,y],[C(x,y)])])').eliminate_equality())

    ([x],[A(x,x), ([],[B(x,x)]), ([x,y],[C(x,y)])])

    >>> print(dexpr('([x,y],[A(x,y)]) + ([],[B(x,y), (x=y)]) + ([],[C(x,y)])').eliminate_equality())

    ([x],[A(x,x), B(x,x), C(x,x)])

    >>> print(dexpr('([x,y],[B(x,y)])+([x,y],[C(x,y)])').replace(Variable('y'), DrtVariableExpression(Variable('x'))))

    (([x,y],[B(x,y)]) + ([x,y],[C(x,y)]))

    >>> print(dexpr('(([x,y],[B(x,y)])+([],[C(x,y)]))+([],[D(x,y)])').replace(Variable('y'), DrtVariableExpression(Variable('x'))))

    (([x,y],[B(x,y)]) + ([],[C(x,y)]) + ([],[D(x,y)]))

    >>> print(dexpr('(([],[B(x,y)])+([],[C(x,y)]))+([],[D(x,y)])').replace(Variable('y'), DrtVariableExpression(Variable('x'))))

    (([],[B(x,x)]) + ([],[C(x,x)]) + ([],[D(x,x)]))

    >>> print(dexpr('(([],[B(x,y), ([x,y],[A(x,y)])])+([],[C(x,y)]))+([],[D(x,y)])').replace(Variable('y'), DrtVariableExpression(Variable('x'))).normalize())

    (([],[B(z3,z1), ([z2,z3],[A(z3,z2)])]) + ([],[C(z3,z1)]) + ([],[D(z3,z1)]))





Parse errors

============



    >>> def parse_error(drtstring):

    ...     try: dexpr(drtstring)

    ...     except logic.LogicalExpressionException as e: print(e)



    >>> parse_error(r'')

    End of input found.  Expression expected.

    <BLANKLINE>

    ^

    >>> parse_error(r'(')

    End of input found.  Expression expected.

    (

     ^

    >>> parse_error(r'()')

    Unexpected token: ')'.  Expression expected.

    ()

     ^

    >>> parse_error(r'([')

    End of input found.  Expected token ']'.

    ([

      ^

    >>> parse_error(r'([,')

    ',' is an illegal variable name.  Constants may not be quantified.

    ([,

      ^

    >>> parse_error(r'([x,')

    End of input found.  Variable expected.

    ([x,

        ^

    >>> parse_error(r'([]')

    End of input found.  Expected token '['.

    ([]

       ^

    >>> parse_error(r'([][')

    End of input found.  Expected token ']'.

    ([][

        ^

    >>> parse_error(r'([][,')

    Unexpected token: ','.  Expression expected.

    ([][,

        ^

    >>> parse_error(r'([][]')

    End of input found.  Expected token ')'.

    ([][]

         ^

    >>> parse_error(r'([x][man(x)]) |')

    End of input found.  Expression expected.

    ([x][man(x)]) |

                   ^



Pretty Printing

===============



    >>> dexpr(r"([],[])").pretty_print()

     __

    |  |

    |--|

    |__|



    >>> dexpr(r"([],[([x],[big(x), dog(x)]) -> ([],[bark(x)]) -([x],[walk(x)])])").pretty_print()

     _____________________________

    |                             |

    |-----------------------------|

    |   ________      _________   |

    |  | x      |    |         |  |

    | (|--------| -> |---------|) |

    |  | big(x) |    | bark(x) |  |

    |  | dog(x) |    |_________|  |

    |  |________|                 |

    |      _________              |

    |     | x       |             |

    | __  |---------|             |

    |   | | walk(x) |             |

    |     |_________|             |

    |_____________________________|



    >>> dexpr(r"([x,y],[x=y]) + ([z],[dog(z), walk(z)])").pretty_print()

      _________     _________

     | x y     |   | z       |

    (|---------| + |---------|)

     | (x = y) |   | dog(z)  |

     |_________|   | walk(z) |

                   |_________|



    >>> dexpr(r"([],[([x],[]) | ([y],[]) | ([z],[dog(z), walk(z)])])").pretty_print()

     _______________________________

    |                               |

    |-------------------------------|

    |   ___     ___     _________   |

    |  | x |   | y |   | z       |  |

    | (|---| | |---| | |---------|) |

    |  |___|   |___|   | dog(z)  |  |

    |                  | walk(z) |  |

    |                  |_________|  |

    |_______________________________|



    >>> dexpr(r"\P.\Q.(([x],[]) + P(x) + Q(x))(\x.([],[dog(x)]))").pretty_print()

              ___                        ________

     \       | x |                 \    |        |

     /\ P Q.(|---| + P(x) + Q(x))( /\ x.|--------|)

             |___|                      | dog(x) |

                                        |________|