aboutsummaryrefslogtreecommitdiff
path: root/src/interp/pf2atree.boot
blob: 10fb2217784a7ea194f545847955dce114b9d07c (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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
-- Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
-- All rights reserved.
-- Copyright (C) 2007-2012, Gabriel Dos Reis.
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are
-- met:
--
--     - Redistributions of source code must retain the above copyright
--       notice, this list of conditions and the following disclaimer.
--
--     - Redistributions in binary form must reproduce the above copyright
--       notice, this list of conditions and the following disclaimer in
--       the documentation and/or other materials provided with the
--       distribution.
--
--     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
--       names of its contributors may be used to endorse or promote products
--       derived from this software without specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
-- IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
-- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
-- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


-- not hooked in yet

-- BB parser tree to interpreter vectorized attributed trees.
-- Used to interface the BB parser
-- technology to the interpreter.  The input is a parseTree and the
-- output is an interpreter attributed tree.

$useParserSrcPos := true
$transferParserSrcPos := true

pf2Sexpr pf == packageTran (pf2Sex1)(pf)

pf2Atree pf ==
    (intUnsetQuiet)()

    $insideRule:        local := false
    $insideApplication: local := false
    $insideSEQ:         local := false

    -- we set the following because we will be using some things
    -- within pf2sex.boot and they are in the spadcomp package.

    ($insideRule):        local := false
    ($insideApplication): local := false
    ($insideSEQ):         local := false

    pf2Atree1 pf

pf2Atree1 pf ==
    -- some simple things that are really just S-expressions

    (pfNothing?)(pf) =>
        mkAtree1WithSrcPos(pf2Sexpr(pf), pf)
    (pfSymbol?) pf =>
        mkAtree1WithSrcPos(pf2Sexpr(pf), pf)
    (pfLiteral?)(pf) =>
        mkAtree1WithSrcPos(pf2Sexpr(pf), pf)
    (pfId?) pf =>
        mkAtree1WithSrcPos(pf2Sexpr(pf), pf)

    -- Now some compound forms

    (pfApplication?)(pf) =>
        pfApplication2Atree pf

    (pfTuple?)(pf) =>
        [mkAtreeNodeWithSrcPos("tuple",pf),
            :[pf2Atree1 x for x in (pf0TupleParts)(pf)]]

    (pfIf?)(pf) =>
        condPf      := (pfIfCond)(pf)
        condPart    := pf2Atree1 condPf
        thenPart    := pf2Atree1 (pfIfThen)(pf)
        elsePart    := pf2Atree1 (pfIfElse)(pf)
        ifPart      := mkAtreeNodeWithSrcPos("IF", pf)
        thenPart = "%noBranch" =>
            [ifPart, [mkAtreeNodeWithSrcPos("not", condPf), condPart],
                elsePart, thenPart]
        [ifPart, condPart, thenPart, elsePart]

    (pfTagged?)(pf) =>
        tag := (pfTaggedTag)(pf)
        tagPart :=
            (pfTuple?) tag =>
                ["tuple", :[pf2Sexpr(arg) for arg in (pf0TupleParts)(tag)]]
            pf2Sexpr(tag)
        [mkAtreeNodeWithSrcPos("Declare",pf), tagPart,
            pf2Sexpr((pfTaggedExpr)(pf))]

    (pfCoerceto?)(pf) =>
        [mkAtreeNodeWithSrcPos("COERCE",pf),
            pf2Atree1 (pfCoercetoExpr)(pf),
                pf2Sexpr((pfCoercetoType)(pf))]

    (pfPretend?)(pf) =>
        [mkAtreeNodeWithSrcPos("pretend",pf),
            pf2Atree1 (pfPretendExpr)(pf),
                pf2Sexpr((pfPretendType)(pf))]

    (pfFromdom?)(pf) =>
        op := packageTran (opTran)(pf2Sexpr((pfFromdomWhat)(pf)))
        if op = "braceFromCurly" then op := "SEQ"         -- ??

        op = 0 =>
            -- 0$Foo => Zero()$Foo
            [mkAtreeNodeWithSrcPos("Dollar",pf),
                pf2Sexpr((pfFromdomDomain)(pf)),
                    [mkAtreeNodeWithSrcPos("Zero",pf)]]
        op = 1 =>
            -- 1$Foo => One()$Foo
            [mkAtreeNodeWithSrcPos("Dollar",pf),
                pf2Sexpr((pfFromdomDomain)(pf)),
                    [mkAtreeNodeWithSrcPos("One",pf)]]
        integer? op =>
            -- n$Foo => n * One()$Foo
            [mkAtreeNodeWithSrcPos("*",pf),
                mkAtree1WithSrcPos(op,pf),
                    [mkAtreeNodeWithSrcPos("Dollar",pf),
                        pf2Sexpr((pfFromdomDomain)(pf)),
                            [mkAtreeNodeWithSrcPos("One",pf)]]]

        [mkAtreeNodeWithSrcPos("Dollar",pf),
            pf2Sexpr((pfFromdomDomain)(pf)),
                mkAtreeNodeWithSrcPos(op,pf)]

    (pfSequence?)(pf) =>
        pfSequence2Atree pf

    (pfExit?)(pf) =>
        $insideSEQ =>
            [mkAtreeNodeWithSrcPos("exit",pf),
                pf2Atree1 (pfExitCond)(pf),
                    pf2Atree1 (pfExitExpr)(pf)]
        [mkAtreeNodeWithSrcPos("IF",pf),
             pf2Atree1 (pfExitCond)(pf),
                pf2Atree1 (pfExitExpr)(pf), "%noBranch"]

    (pfLoop?)(pf) =>
        [mkAtreeNodeWithSrcPos("REPEAT",pf),
            :loopIters2Atree  (pf0LoopIterators)(pf)]

    (pfCollect?)(pf) =>
        pfCollect2Atree(pf)

    (pfForin?)(pf) =>
        ["IN", :[pf2Atree1 x for x in (pf0ForinLhs)(pf)],
            pf2Atree1 (pfForinWhole)(pf)]

    (pfWhile?)(pf) =>
        ["WHILE", pf2Atree1((pfWhileCond)(pf))]

    (pfSuchthat?)(pf) =>
        $insideRule = 'left =>
            keyedSystemError('"S2GE0017", ['"pf2Atree1: pfSuchThat"])
        ["SUCHTHAT", pf2Atree1 (pfSuchthatCond)(pf)]

    (pfDo?)(pf) =>
        pf2Atree1 (pfDoBody)(pf)

--  (pfTyped?)(pf) =>
--    type := pfTypedType pf
--    pfNothing? type => pf2Atree1 pfTypedId pf
--    [":", pf2Atree1 pfTypedId pf, pf2Atree1 pfTypedType pf]

    (pfAssign?)(pf) =>
        -- declarations on the lhs are broken out into another
        -- statement preceding the %LET of the id(s)
        lhsThings := (pf0AssignLhsItems)(pf)
        if #lhsThings = 1 and (pfTuple?)(first lhsThings) then
            lhsThings := (pf0TupleParts)(first lhsThings)
        decls := nil
        ids   := nil
        for x in lhsThings repeat
            (pfTagged?)(x) =>
                decls := [x, :decls]
                ids   := [(pfTaggedTag)(x), :ids]
            ids   := [x, :ids]
        idList := [pf2Atree1 x for x in reverse ids]
        if #idList ~= 1 then idList :=
            [mkAtreeNodeWithSrcPos("tuple",pf), :idList]
        else idList := first idList
        x := [mkAtreeNodeWithSrcPos("%LET",pf),
            idList, pf2Atree1 (pfAssignRhs)(pf)]
        decls =>
            [mkAtreeNodeWithSrcPos("SEQ",pf),
                :[pf2Atree1 decl for decl in reverse! decls], x]
        x

--  (pfDefinition?)(pf) =>
--      pfDefinition2Atree pf

--  (pfLambda?)(pf) =>
--    pfLambda2Atree pf
--  (pfRestrict?)(pf) =>
--    ["@", pf2Atree1 pfRestrictExpr pf, pf2Atree1 pfRestrictType pf]

    (pfFree?)(pf) =>
        [mkAtreeNodeWithSrcPos("free",pf),
            :[pf2Atree1 item for item in (pf0FreeItems)(pf)]]
    (pfLocal?)(pf) =>
        [mkAtreeNodeWithSrcPos("local",pf),
            :[pf2Atree1 item for item in (pf0LocalItems)(pf)]]

    (pfWrong?)(pf) =>
        spadThrow()

    -- next 3 are probably be handled in pfApplication2Atree

    (pfAnd?)(pf) =>
        [mkAtreeNodeWithSrcPos("and",pf),
            pf2Atree1 (pfAndLeft)(pf),
                pf2Atree1 (pfAndRight)(pf)]
    (pfOr?)(pf) =>
        [mkAtreeNodeWithSrcPos("or",pf),
            pf2Atree1 (pfOrLeft)(pf),
                pf2Atree1 (pfOrRight)(pf)]
    (pfNot?)(pf) =>
        [mkAtreeNodeWithSrcPos("not",pf),
            pf2Atree1 (pfNotArg)(pf)]

--  (pfNovalue?)(pf) =>
--    intSetQuiet()
--    ["SEQ", pf2Atree1 pfNovalueExpr pf]
--  (pfRule?)(pf) =>
--    pfRule2Atree pf

    (pfBreak?)(pf) =>
        [mkAtreeNodeWithSrcPos("break",pf), (pfBreakFrom)(pf)]

    (pfMacro?)(pf) =>
        tree := mkAtree1WithSrcPos('(void), pf)
        putValue(tree,objNewWrap(voidValue(),$Void))
        putModeSet(tree,[$Void])
        tree

    (pfReturn?)(pf) =>
        [mkAtreeNodeWithSrcPos("return",pf),
            pf2Atree1 (pfReturnExpr)(pf)]

    (pfIterate?)(pf) =>
        [mkAtreeNodeWithSrcPos("iterate",pf)]

--  (pfWhere?)(pf) =>
--    args := [pf2Atree1 p for p in pf0WhereContext pf]
--    #args = 1 =>
--      ["where", pf2Atree1 pfWhereExpr pf, :args]
--    ["where", pf2Atree1 pfWhereExpr pf, ["SEQ", :args]]

    mkAtree1WithSrcPos(pf2Sexpr(pf), pf)

--  keyedSystemError('"S2GE0017", ['"pf2Atree1"])
--

pfApplication2Atree pf ==
    $insideApplication:             local := true
    ($insideApplication): local := true

    opPf := (pfApplicationOp)(pf)
    op := packageTran ((opTran)(pfOp2Sex)(opPf))
    op = "->" =>
        args := (pf0TupleParts)((pfApplicationArg)(pf))
        if (pfTuple?)(first args) then
            typeList := [pf2Atree1 arg for arg in (pf0TupleParts)(first args)]
        else
            typeList := [pf2Atree1 first args]
        args := [pf2Atree1 second args, :typeList]
        [mkAtreeNodeWithSrcPos("Mapping", opPf), :args]

    (symEqual)(op, '":") and $insideRule = 'left =>
          [mkAtreeNodeWithSrcPos("multiple",opPf),
              pf2Atree (pfApplicationArg)(pf)]

    (symEqual)(op, '"?") and $insideRule = 'left =>
          [mkAtreeNodeWithSrcPos("optional",opPf),
              pf2Atree (pfApplicationArg)(pf)]

    args := (pfApplicationArg)(pf)

    (pfTuple?)(args) =>
--!     symEqual(op, '"|") and $insideRule = 'left =>
--!       pfSuchThat2Atree args
        argAtree := [pf2Atree1 arg for arg in (pf0TupleParts)(args)]

        (symEqual)(op, '">") =>
            [mkAtreeNodeWithSrcPos("<",opPf), :reverse(argAtree)]
        (symEqual)(op, '">=") =>
            [mkAtreeNodeWithSrcPos("not",opPf),
                [mkAtreeNodeWithSrcPos("<",opPf), :argAtree]]
        (symEqual)(op, '"<=") =>
            [mkAtreeNodeWithSrcPos("not",opPf),
                [mkAtreeNodeWithSrcPos("<",opPf), :reverse(argAtree)]]
        (symEqual)(op, '"AND") =>
            [mkAtreeNodeWithSrcPos("and",opPf), :argAtree]
        (symEqual)(op, '"OR") =>
            [mkAtreeNodeWithSrcPos("or",opPf), :argAtree]
        (symEqual) (op, '"Iterate") =>
            [mkAtreeNodeWithSrcPos("iterate",opPf)]
        (symEqual)(op, '"by") =>
            [mkAtreeNodeWithSrcPos("BY",opPf), :argAtree]
        (symEqual)(op, '"braceFromCurly") =>
            argAtree and getUnname first argAtree = "SEQ" => argAtree
            [mkAtreeNodeWithSrcPos("SEQ",opPf), :argAtree]
        op is [qt, realOp] and (symEqual)(qt, '"QUOTE") =>
            [mkAtreeNodeWithSrcPos("applyQuote",opPf),
                mkAtreeNodeWithSrcPos(op,opPf), :argAtree]
--!     val := (hasOptArgs?)(argSex) => [op, :val]
        -- handle package call
        (pfFromdom?)(opPf) =>
            opAtree := pf2Atree1 opPf
            [first opAtree, second opAtree, [third opAtree, :argAtree]]
        -- regular call
        [mkAtreeNodeWithSrcPos(op,opPf), :argAtree]

    op is [qt, realOp] and (symEqual)(qt, '"QUOTE") =>
       [mkAtreeNodeWithSrcPos("applyQuote",opPf),
           mkAtreeNodeWithSrcPos(op,opPf), pf2Atree1 args]
    (symEqual)(op, '"braceFromCurly") =>
        x := pf2Atree1 args
        x and getUnname x = "SEQ" => x
        [mkAtreeNodeWithSrcPos("SEQ",opPf), x]
    (symEqual)(op, '"by") =>
        [mkAtreeNodeWithSrcPos("BY",opPf), pf2Atree1 args]
    -- handle package call
    (pfFromdom?)(opPf) =>
        opAtree := pf2Atree1 opPf
        [first opAtree, second opAtree, [third opAtree, pf2Atree1 args]]
    -- regular call
    [mkAtreeNodeWithSrcPos(op,opPf), pf2Atree1 args]

--  pfDefinition2Atree pf ==
--  --! $insideApplication =>
--  --!     ["OPTARG", pf2Atree1 first pf0DefinitionLhsItems pf,
--  --!         pf2Atree1 pfDefinitionRhs pf]
--      idList := [pf2Atree1 x for x in (pf0DefinitionLhsItems)(pf)]
--      #idList ~= 1 =>
--          systemError '"lhs of definition must be a single item in the interpreter"
--      id := first idList
--      rhs := (pfDefinitionRhs)(pf)
--      [argList, :body] := pfLambdaTran rhs
--      ["DEF", (argList = 'id => id; [id, :argList]), :body]

--  pfLambdaTran pf ==
--    pfLambda? pf =>
--      argTypeList := nil
--      argList := nil
--      for arg in pf0LambdaArgs pf repeat
--        pfTyped? arg =>
--          argList := [pfCollectArgTran pfTypedId arg, :argList]
--          pfNothing? pfTypedType arg =>
--            argTypeList := [nil, :argTypeList]
--          argTypeList := [pf2Atree1 pfTypedType arg, :argTypeList]
--        systemError '"definition args should be typed"
--      argList := reverse! argList
--      retType :=
--        pfNothing? pfLambdaRets pf => nil
--        pf2Atree1 pfLambdaRets pf
--      argTypeList := [retType, :reverse! argTypeList]
--      [argList, :[argTypeList, [nil for arg in argTypeList],
--        pf2Atree1 pfLambdaBody pf]]
--    ['id, :['(()), '(()), pf2Atree1 pf]]
--
--  pfLambda2Atree pf ==
--    [argList, :body] := pfLambdaTran pf
--    ["ADEF", argList, :body]
--
--  pfCollectArgTran pf ==
--    pfCollect? pf =>
--      conds := [pf2Atree1 x for x in pfParts pfCollectIterators pf]
--      id := pf2Atree1 pfCollectBody pf
--      conds is [["|", cond]] =>
--        ["|", id, cond]
--      [id, :conds]
--    pf2Atree1 pf
--

pfSequence2Atree pf ==
    $insideSEQ:             local := true
    ($insideSEQ): local := true

    seq := pfSequence2Atree0([pf2Atree1 x for x in (pf0SequenceArgs)(pf)], pf)
    seqSex := (pfSequence2Sex0)([pf2Sexpr(x) for x in (pf0SequenceArgs)(pf)])
    seqSex is ["SEQ", :ruleList] and ruleList is [["rule", :.], :.] =>
        [mkAtreeNodeWithSrcPos("ruleset",pf),
            [mkAtreeNodeWithSrcPos("construct",pf), :rest seq]]
    seq

pfSequence2Atree0(seqList, pf) ==
    null seqList => "%noBranch"
    seqTranList := []
    while seqList ~= nil repeat
        item := first seqList
        item is [exitVal, cond, value] and getUnname(exitVal) = "exit" =>
            item := [mkAtreeNodeWithSrcPos("IF",pf), cond, value,
                pfSequence2Atree0(rest seqList, pf)]
            seqTranList := [item, :seqTranList]
            seqList := nil
        seqTranList := [item ,:seqTranList]
        seqList := rest seqList
    #seqTranList = 1 => first seqTranList
    [mkAtreeNodeWithSrcPos("SEQ",pf), :reverse! seqTranList]

--
--  float2Atree num ==
--    eIndex := findChar(char "e", num)
--    mantPart :=
--      eIndex => subSequence(num, 0, eIndex)
--      num
--    expPart := (eIndex => readLispFromString subSequence(num, eIndex+1); 0)
--    dotIndex := findChar(char ".", mantPart)
--    intPart :=
--      dotIndex => readLispFromString subSequence(mantPart, 0, dotIndex)
--      readLispFromString mantPart
--    fracPartString :=
--      dotIndex => subSequence(mantPart, dotIndex+1)
--      '"0"
--    bfForm := MAKE_-FLOAT(intPart, readLispFromString fracPartString,
--      # fracPartString, expPart)
--    [., frac, :exp] := bfForm
--    [["$elt", intNewFloat(), 'float], frac, exp, 10]
--

loopIters2Atree iterList ==
    -- could probably do a better job of getting accurate SrcPos info onto parts
    result := nil
    for iter in iterList repeat
        -- ON and UNTIL forms are no longer supported
        sex := pf2Sexpr(iter)
        sex is ['IN, var, ['SEGMENT, i, ["BY", incr]]] =>
            newIter := ["STEP", var, mkAtree1WithSrcPos(i,iter),
                mkAtree1WithSrcPos(incr, iter)]
            result := [newIter, :result]
        sex is ['IN, var, ["BY", ['SEGMENT, i, j], incr]] =>
            newIter := ["STEP", var, mkAtree1WithSrcPos(i,iter),
                mkAtree1WithSrcPos(incr, iter), mkAtree1WithSrcPos(j,iter)]
            result := [newIter, :result]
        sex is ['IN, var, ['SEGMENT, i, j]] =>
            newIter := ["STEP", var, mkAtree1WithSrcPos(i,iter),
                mkAtree1WithSrcPos(1,iter), mkAtree1WithSrcPos(j,iter)]
            result := [newIter, :result]
        sex is ['IN, var, s] =>
            newIter := ["IN", var, mkAtree1 s]
            result := [newIter, :result]
        result := [pf2Atree1(iter), :result]
    reverse! result

pfCollect2Atree pf ==
    atree := [mkAtree1WithSrcPos("COLLECT",pf),
        :loopIters2Atree (pfParts)((pfCollectIterators)(pf)),
            pf2Atree1 (pfCollectBody)(pf)]

    -- next are for what appears to a parser screw-up
    sex := ["COLLECT",
        :(loopIters2Sex)((pfParts)((pfCollectIterators)(pf))),
            pf2Sexpr (pfCollectBody)(pf)]
    sex is ["COLLECT", ["|", cond], var] and symbol? var =>
        [., [., condAtree], varAtree] := atree
        ["SUCHTHAT", varAtree, condAtree]

    atree

--
--  pfRule2Atree pf ==
--    $quotedOpList:local := nil
--    $predicateList:local := nil
--    $multiVarPredicateList:local := nil
--    lhs := pfLhsRule2Atree pfRuleLhsItems pf
--    rhs := pfRhsRule2Atree pfRuleRhs pf
--    lhs := ruleLhsTran lhs
--    rulePredicateTran
--      $quotedOpList => ["rule", lhs, rhs, ["construct", :$quotedOpList]]
--      ["rule", lhs, rhs]
--
--
--  ruleLhsTran ruleLhs ==
--    for pred in $predicateList repeat
--      [name, predLhs, :predRhs] := pred
--      vars := patternVarsOf predRhs
--      rest vars =>  -- if there is more than one patternVariable
--        ruleLhs := substitute!(predLhs, name, ruleLhs)
--        $multiVarPredicateList := [pred, :$multiVarPredicateList]
--      predicate :=
--        [., var] := predLhs
--        ["suchThat", predLhs, ["ADEF", [var],
--          '((Boolean) (Expression (Integer))), '(() ()), predRhs]]
--      ruleLhs := substitute!(predicate, name, ruleLhs)
--    ruleLhs
--
--  rulePredicateTran rule ==
--    null $multiVarPredicateList => rule
--    varList := patternVarsOf [rhs for [.,.,:rhs] in $multiVarPredicateList]
--    predBody :=
--      rest $multiVarPredicateList =>
--        ['AND, :[:pvarPredTran(rhs, varList) for [.,.,:rhs] in
--          $multiVarPredicateList]]
--      [[.,.,:rhs],:.] := $multiVarPredicateList
--      pvarPredTran(rhs, varList)
--    ['suchThat, rule,
--     ['construct, :[quote var for var in varList]],
--      ['ADEF, '(predicateVariable),
--       '((Boolean) (List (Expression (Integer)))), '(() ()),
--        predBody]]
--
--  pvarPredTran(rhs, varList) ==
--    for var in varList for i in 1.. repeat
--      rhs := substitute!(['elt, 'predicateVariable, i], var, rhs)
--    rhs
--
--  patternVarsOf expr ==
--    patternVarsOf1(expr, nil)
--
--  patternVarsOf1(expr, varList) ==
--    null expr => varList
--    expr isnt [.,:.] =>
--      not symbol? expr => varList
--      SymMemQ(expr, varList) => varList
--      [expr, :varList]
--    expr is [op, :argl] =>
--      for arg in argl repeat
--        varList := patternVarsOf1(arg, varList)
--      varList
--    varList
--
--  pfLhsRule2Atree lhs ==
--    $insideRule: local := 'left
--    ($insideRule): local := 'left
--    pf2Atree1 lhs
--
--
--  pfRhsRule2Atree rhs ==
--    $insideRule: local := 'right
--    ($insideRule): local := 'right
--    pf2Atree1 rhs
--

--  pfSuchThat2Atree args ==
--    name := gensym()
--    argList := pf0TupleParts args
--    lhsSex := pf2Atree1 first argList
--    rhsSex := pf2Atree second argList
--    $predicateList := [[name, lhsSex, :rhsSex], :$predicateList]
--    name