-
Notifications
You must be signed in to change notification settings - Fork 45
/
i-funsel.boot
1762 lines (1605 loc) · 59.1 KB
/
i-funsel.boot
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
-- 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.
)package "BOOT"
)if false
New Selection of Modemaps
selection of applicable modemaps is done in two steps:
first it tries to find a modemap inside an argument domain, and if
this fails, by evaluation of pattern modemaps
the result is a list of functions with signatures, which have the
following form:
[sig,elt,cond] where
sig is the signature gained by evaluating the modemap condition
elt is the slot number to get the implementation
cond are runtime checks which are the results of evaluating the
modemap condition
the following flags are used:
$Coerce is NIL, if function selection is done which requires exact
matches (e.g. for coercion functions)
if $SubDom is true, then runtime checks have to be compiled
)endif
DEFPARAMETER($constructorExposureList, '(Boolean Integer String))
sayFunctionSelection(op,args,target,dc,func) ==
$abbreviateTypes : local := true
startTimingProcess 'debug
fsig := formatSignatureArgs args
if not LISTP fsig then fsig := LIST fsig
if func then func := bright ['"by ",func]
sayMSG concat ['%l,:bright '"Function Selection for",op,:func,'%l,
'" Arguments:",:bright fsig]
if target then sayMSG concat ['" Target type:",
:bright prefix2String target]
if dc then sayMSG concat ['" From: ",
:bright prefix2String dc]
stopTimingProcess 'debug
sayFunctionSelectionResult(op,args,mmS) ==
$abbreviateTypes : local := true
startTimingProcess 'debug
if mmS then printMms mmS
else sayMSG concat ['" -> no function",:bright op,
'"found for arguments",:bright formatSignatureArgs args]
stopTimingProcess 'debug
selectMms(op,args,$declaredMode) ==
-- selects applicable modemaps for node op and arguments args
-- if there is no local modemap, and it is not a package call, then
-- the cached function selectMms1 is called
startTimingProcess 'modemaps
n:= getUnname op
val := getValue op
opMode := objMode val
-- see if we have a functional parameter
((isSharpVarWithNum(n) and opMode) or (val and opMode)) and
opMode is ['Mapping,:ta] =>
imp :=
val => wrapped2Quote objVal val
n
[[['local,:ta], imp , NIL]]
((isSharpVarWithNum(n) and opMode) or (val and opMode)) and
opMode is ['Variable,f] =>
emptyAtree op
op.0 := f
selectMms(op,args,$declaredMode)
isSharpVarWithNum(n) and opMode is ['FunctionCalled,f] =>
op.0 := f
selectMms(op,args,$declaredMode)
types1 := getOpArgTypes(n,args)
numArgs := #args
member($EmptyMode,types1) => NIL
tar := getTarget op
dc := getAtree(op,'dollar)
null dc and val and objMode(val) = $AnonymousFunction =>
tree := mkAtree objValUnwrap getValue op
putTarget(tree,['Mapping,tar,:types1])
bottomUp tree
val := getValue tree
[[['local,:rest objMode val], wrapped2Quote objVal val, NIL]]
if (n = 'map) and (first types1 = $AnonymousFunction)
then
tree := mkAtree objValUnwrap getValue first args
ut :=
tar => underDomainOf tar
NIL
ua := [underDomainOf x for x in rest types1]
member(NIL,ua) => NIL
putTarget(tree,['Mapping,ut,:ua])
bottomUp tree
val := getValue tree
types1 := [objMode val,:rest types1]
RPLACA(args,tree)
if numArgs = 1 and (n = "numer" or n = "denom") and
isEqualOrSubDomain(first types1,$Integer) and null dc then
dc := ['Fraction, $Integer]
putAtree(op, 'dollar, dc)
if $reportBottomUpFlag then sayFunctionSelection(n,types1,tar,dc,NIL)
identType := 'Variable
for x in types1 while not $declaredMode repeat
not EQCAR(x,identType) => $declaredMode:= x
types2 := [altTypeOf(x,y,$declaredMode) for x in types1 for y in args]
mmS:=
dc => selectDollarMms(dc,n,types1,types2)
if n = "/" and tar = $Integer then
tar := $RationalNumber
putTarget(op,tar)
-- now to speed up some standard selections
if not tar then
tar := defaultTarget(op,n,#types1,types1)
if tar and $reportBottomUpFlag then
sayMSG concat ['" Default target type:",
:bright prefix2String tar]
selectLocalMms(op,n,types1,tar) or
(VECTORP op and selectMms1(n,tar,types1,types2,'T))
if $reportBottomUpFlag then sayFunctionSelectionResult(n,types1,mmS)
stopTimingProcess 'modemaps
mmS
-- selectMms1 is in clammed.boot
selectMms2(op,tar,args1,args2,$Coerce) ==
-- decides whether to find functions from a domain or package
-- or by general modemap evaluation
or/[STRINGP arg for arg in args1] => NIL
if tar = $EmptyMode then tar := NIL
nargs := #args1
mmS := NIL
mmS :=
-- special case map for the time being
$Coerce and (op = 'map) and (2 = nargs) and
(first(args1) is ['Variable,fun]) =>
null (ud := underDomainOf CADR args1) => NIL
if tar then ut := underDomainOf(tar)
else ut := nil
null (mapMms := selectMms1(fun,ut,[ud],[NIL],true)) => NIL
mapMm := CDAAR mapMms
selectMms1(op,tar,[['Mapping,:mapMm],CADR args1],
[NIL,CADR args2],$Coerce)
$Coerce and (op = 'map) and (2 = nargs) and
(first(args1) is ['FunctionCalled,fun]) =>
null (ud := underDomainOf CADR args1) => NIL
if tar then ut := underDomainOf(tar)
else ut := nil
funNode := mkAtreeNode fun
transferPropsToNode(fun,funNode)
null (mapMms := selectLocalMms(funNode,fun,[ud],NIL)) => NIL
mapMm := CDAAR mapMms
selectMms1(op,tar,[['Mapping,:mapMm],CADR args1],
[NIL,CADR args2],$Coerce)
-- get the argument domains and the target
a := nil
for x in args1 repeat if x then a := cons(x,a)
for x in args2 repeat if x then a := cons(x,a)
if tar and not isPartialMode tar then a := cons(tar,a)
-- for typically homogeneous functions, throw in resolve too
if op in '(_= _+ _* _- ) then
r := resolveTypeList a
if r ~= nil then a := cons(r,a)
if tar and not isPartialMode tar then
if xx := underDomainOf(tar) then a := cons(xx,a)
for x in args1 repeat
PAIRP(x) and first(x) in '(List Vector Stream FiniteSet Array) =>
xx := underDomainOf(x) => a := cons(xx,a)
-- now extend this list with those from the arguments to
-- any Unions, Mapping or Records
a' := nil
a := nreverse REMDUP a
for x in a repeat
null x => 'iterate
x is ['Union,:l] =>
-- check if we have a tagged union
l and first l is [":",:.] =>
for [.,.,t] in l repeat
a' := cons(t,a')
a' := append(reverse l,a')
x is ['Mapping,:l] => a' := append(reverse l,a')
x is ['Record,:l] =>
a' := append(reverse [CADDR s for s in l],a')
x is ['FunctionCalled,name] =>
(xm := get(name,'mode,$e)) and not isPartialMode xm =>
a' := cons(xm,a')
a := append(a,REMDUP a')
a := [x for x in a | PAIRP(x)]
-- step 1. see if we have one without coercing
a' := a
while a repeat
x := first a
a := rest a
ATOM x => 'iterate
mmS := append(mmS, findFunctionInDomain(op,x,tar,args1,args2,NIL,NIL))
-- step 2. if we didn't get one, trying coercing (if we are
-- suppose to)
if null(mmS) and $Coerce then
a := a'
while a repeat
x := first a
a := rest a
ATOM x => 'iterate
mmS := append(mmS,
findFunctionInDomain(op,x,tar,args1,args2,$Coerce,NIL))
mmS or selectMmsGen(op,tar,args1,args2)
mmS and orderMms(op, mmS,args1,args2,tar)
isAVariableType t ==
t is ['Variable,.] or t = $Symbol or t is ['OrderedVariableList,.]
defaultTarget(opNode,op,nargs,args) ==
-- this is for efficiency. Chooses standard targets for operations
-- when no target exists.
target := nil
nargs = 0 =>
op = 'nil =>
putTarget(opNode, target := '(List (None)))
target
op = 'true or op = 'false =>
putTarget(opNode, target := $Boolean)
target
op = 'pi =>
putTarget(opNode, target := ['Pi])
target
op = 'infinity =>
putTarget(opNode, target := ['OnePointCompletion, $Integer])
target
member(op, '(plusInfinity minusInfinity)) =>
putTarget(opNode, target := ['OrderedCompletion, $Integer])
target
target
a1 := first args
ATOM a1 => target
a1f := QCAR a1
nargs = 1 =>
op = 'kernel =>
putTarget(opNode, target := ['Kernel, ['Expression, $Integer]])
target
op = 'list =>
putTarget(opNode, target := ['List, a1])
target
target
a2 := CADR args
nargs >= 2 and op = "draw" and a1 is ['FunctionCalled,sym] and a2 is ['Segment,.] =>
-- this clears up some confusion over 2D and 3D graphics
symNode := mkAtreeNode sym
transferPropsToNode(sym,symNode)
nargs >= 3 and CADDR args is ['Segment,.] =>
selectLocalMms(symNode,sym,[$DoubleFloat, $DoubleFloat],NIL)
putTarget(opNode, target := '(ThreeDimensionalViewport))
target
(mms := selectLocalMms(symNode,sym,[$DoubleFloat],NIL)) =>
[.,targ,:.] := CAAR mms
targ = $DoubleFloat =>
putTarget(opNode, target := '(TwoDimensionalViewport))
target
targ = ['Point, $DoubleFloat] =>
putTarget(opNode, target := '(ThreeDimensionalViewport))
target
target
target
nargs >= 2 and op = "makeObject" and a1 is ['FunctionCalled,sym] and a2 is ['Segment,.] =>
-- we won't actually bother to put a target on makeObject
-- this is just to figure out what the first arg is
symNode := mkAtreeNode sym
transferPropsToNode(sym,symNode)
nargs >= 3 and CADDR args is ['Segment,.] =>
selectLocalMms(symNode,sym,[$DoubleFloat, $DoubleFloat],NIL)
target
selectLocalMms(symNode,sym,[$DoubleFloat],NIL)
target
nargs = 2 =>
op = "elt" =>
a1 = '(BasicOperator) and a2 is ['List, ['OrderedVariableList, .]] =>
['Expression, $Integer]
target
op = "eval" =>
a1 is ['Expression,b1] and a2 is ['Equation, ['Polynomial,b2]] =>
target :=
canCoerce(b2, a1) => a1
t := resolveTT(b1, b2)
(not t) or (t = $Any) => nil
resolveTT(a1, t)
if target then putTarget(opNode, target)
target
a1 is ['Equation, .] and a2 is ['Equation, .] =>
target := resolveTT(a1, a2)
if target and not (target = $Any) then putTarget(opNode,target)
else target := nil
target
a1 is ['Equation, .] and a2 is ['List, a2e] and a2e is ['Equation, .] =>
target := resolveTT(a1, a2e)
if target and not (target = $Any) then putTarget(opNode,target)
else target := nil
target
a2 is ['Equation, a2e] or a2 is ['List, ['Equation, a2e]] =>
target := resolveTT(a1, a2e)
if target and not (target = $Any) then putTarget(opNode,target)
else target := nil
target
op = "**" or op = "^" =>
a2 = $Integer =>
if (target := resolveTCat(a1,'(Field))) then
putTarget(opNode,target)
target
a1 = '(AlgebraicNumber) and (a2 = $Float or a2 = $DoubleFloat) =>
target := ['Expression, a2]
putTarget(opNode,target)
target
a1 = '(AlgebraicNumber) and a2 is ['Complex, a3] and (a3 = $Float or a3 = $DoubleFloat) =>
target := ['Expression, a3]
putTarget(opNode,target)
target
((a2 = $RationalNumber) and
(typeIsASmallInteger(a1) or isEqualOrSubDomain(a1,$Integer))) =>
putTarget(opNode, target := '(AlgebraicNumber))
target
((a2 = $RationalNumber) and (isAVariableType(a1)
or a1 is ['Polynomial, .])) =>
putTarget(opNode, target := defaultTargetFE a1)
target
isAVariableType(a1) and (a2 = $PositiveInteger or a2 = $NonNegativeInteger) =>
putTarget(opNode, target := '(Polynomial (Integer)))
target
isAVariableType(a2) =>
putTarget(opNode, target := defaultTargetFE a1)
target
a2 is ['Polynomial, D] =>
(a1 = a2) or isAVariableType(a1)
or (a1 = D)
or ((a1 is [=$QuotientField, D1]) and (D1 = a1)) =>
putTarget(opNode, target := defaultTargetFE a2)
target
target
target
op = '_/ =>
isEqualOrSubDomain(a1, $Integer) and isEqualOrSubDomain(a2, $Integer) =>
putTarget(opNode, target := $RationalNumber)
target
a1 = a2 =>
if (target := resolveTCat(first args, '(Field))) then
putTarget(opNode,target)
target
a1 is ['Variable,.] and a2 is ['Variable,.] =>
putTarget(opNode,target := mkRationalFunction '(Integer))
target
isEqualOrSubDomain(a1,$Integer) and a2 is ['Variable,.] =>
putTarget(opNode,target := mkRationalFunction '(Integer))
target
a1 is ['Variable,.] and
a2 is ['Polynomial,D] =>
putTarget(opNode,target := mkRationalFunction D)
target
target
a2 is ['Variable,.] and
a1 is ['Polynomial,D] =>
putTarget(opNode,target := mkRationalFunction D)
target
target
a2 is ['Polynomial,D] and (a1 = D) =>
putTarget(opNode,target := mkRationalFunction D)
target
target
a3 := CADDR args
nargs = 3 =>
op = "eval" =>
a3 is ['List, a3e] =>
target := resolveTT(a1, a3e)
if not (target = $Any) then putTarget(opNode,target)
else target := nil
target
target := resolveTT(a1, a3)
if not (target = $Any) then putTarget(opNode,target)
else target := nil
target
target
mkRationalFunction D == ['Fraction, ['Polynomial, D]]
defaultTargetFE(a,:options) ==
a is ['Variable,.] or a = $RationalNumber or MEMQ(QCAR a,
[QCAR $Symbol,
'Pi]) or typeIsASmallInteger(a) or isEqualOrSubDomain(a, $Integer) or
a = '(AlgebraicNumber) =>
IFCAR options => [$FunctionalExpression, ['Complex, $Integer]]
[$FunctionalExpression, $Integer]
a is ['Complex,uD] => defaultTargetFE(uD, true)
a is [D, uD] and MEMQ(D, '(Polynomial Fraction)) =>
defaultTargetFE(uD, IFCAR options)
a is [=$FunctionalExpression,.] => a
IFCAR options => [$FunctionalExpression, ['Complex, a]]
[$FunctionalExpression, a]
altTypeOf(type,val,$declaredMode) ==
(EQCAR(type,'Symbol) or EQCAR(type,'Variable)) and
(a := getMinimalVarMode(objValUnwrap getValue(val),$declaredMode)) =>
a
type is ['OrderedVariableList,vl] and
INTEGERP(val1 := objValUnwrap getValue(val)) and
(a := getMinimalVarMode(vl.(val1 - 1),$declaredMode)) =>
a
type = $PositiveInteger => $Integer
type = $NonNegativeInteger => $Integer
type = '(List (PositiveInteger)) => '(List (Integer))
NIL
getOpArgTypes(opname, args) ==
l := getOpArgTypes1(opname, args)
[f(a,opname) for a in l] where
f(x,op) ==
x is ['FunctionCalled,g] and op ~= 'name =>
m := get(g,'mode,$e) =>
m is ['Mapping,:.] => m
x
x
x
getOpArgTypes1(opname, args) ==
null args => NIL
-- special cases first
opname = 'coef and args is [b,n] =>
[first getModeSet b, first getModeSetUseSubdomain n]
opname = 'monom and args is [d,c] =>
[first getModeSetUseSubdomain d, first getModeSet c]
opname = 'monom and args is [v,d,c] =>
[first getModeSet v, first getModeSetUseSubdomain d, first getModeSet c]
(opname = 'cons) and (2 = #args) and (CADR(args) = 'nil) =>
ms := [first getModeSet x for x in args]
if CADR(ms) = '(List (None)) then
ms := [first ms,['List,first ms]]
ms
nargs := #args
v := argCouldBelongToSubdomain(opname,nargs)
mss := NIL
for i in 0..(nargs-1) for x in args repeat
ms :=
v.i = 0 => first getModeSet x
first getModeSetUseSubdomain x
mss := [ms,:mss]
nreverse mss
argCouldBelongToSubdomain(op, nargs) ==
-- this returns a vector containing 0 or ^0 for each argument.
-- if ^0, this indicates that there exists a modemap for the
-- op that needs a subdomain in that position
nargs = 0 => NIL
v := GETZEROVEC nargs
isMap(op) => v
mms := getModemapsFromDatabase(op,nargs)
null mms => v
nargs:=nargs-1
-- each signature has form
-- [domain of implementation, target, arg1, arg2, ...]
for [sig,cond,:.] in mms repeat
for t in CDDR sig for i in 0..(nargs) repeat
CONTAINEDisDomain(t,cond) =>
v.i := 1 + v.i
v
CONTAINEDisDomain(symbol,cond) ==
-- looks for [isSubDomain,symbol,[domain]] in cond: returning T or NIL
-- with domain being one of PositiveInteger and NonNegativeInteger
ATOM cond => false
MEMQ(QCAR cond,'(AND OR and or)) =>
or/[CONTAINEDisDomain(symbol, u) for u in QCDR cond]
EQ(QCAR cond,'isDomain) =>
EQ(symbol,CADR cond) and PAIRP(dom:=CADDR cond) and
MEMQ(dom,'(PositiveInteger NonNegativeInteger))
false
selectDollarMms(dc,name,types1,types2) ==
-- finds functions for name in domain dc
isPartialMode dc => throwKeyedMsg("S2IF0001",NIL)
mmS := findFunctionInDomain(name,dc,NIL,types1,types2,'T,'T) =>
orderMms(name, mmS,types1,types2,NIL)
if $reportBottomUpFlag then sayMSG
["%b",'" function not found in ",prefix2String dc,"%d","%l"]
NIL
selectLocalMms(op,name,types,tar) ==
-- partial rewrite, looks now for exact local modemap
mmS:= getLocalMms(name,types,tar) => mmS
obj := getValue op
obj and (objVal obj is ['SPADMAP, :mapDef]) and
analyzeMap(op,types,mapDef,tar) and getLocalMms(name,types,tar)
-- next defn may be better, test when more time. RSS 3/11/94
-- selectLocalMms(op,name,types,tar) ==
-- mmS := getLocalMms(name,types,tar)
-- -- if no target, just return what we got
-- mmS and null tar => mmS
-- matchingMms := nil
-- for mm in mmS repeat
-- [., targ, :.] := mm
-- if tar = targ then matchingMms := cons(mm, matchingMms)
-- -- if we got some exact matchs on the target, return them
-- matchingMms => nreverse matchingMms
--
-- obj := getValue op
-- obj and (objVal obj is ['SPADMAP, :mapDef]) and
-- analyzeMap(op,types,mapDef,tar) and getLocalMms(name,types,tar)
getLocalMms(name,types,tar) ==
-- looks for exact or subsumed local modemap in $e
mmS := NIL
for (mm:=[dcSig,:.]) in get(name,'localModemap,$e) repeat
-- check format and destructure
dcSig isnt [dc,result,:args] => NIL
-- make number of args is correct
#types ~= #args => NIL
-- check for equal or subsumed arguments
subsume := (not $useIntegerSubdomain) or (tar = result) or
get(name,'recursive,$e)
acceptableArgs :=
and/[f(b,a,subsume) for a in args for b in types] where
f(x,y,subsume) ==
if subsume
then isEqualOrSubDomain(x,y)
else x = y
not acceptableArgs =>
-- interpreted maps are ok
dc = 'interpOnly and not($Coerce)=> mmS := [mm,:mmS]
NIL
mmS := [mm,:mmS]
nreverse mmS
-- Helper to avoid bad coercions (SF 2974970). See
--
-- http://groups.google.com/group/fricas-devel/browse_thread/thread/a93abc242431a6bc?hl=en#
--
-- for more info.
isApproximate(t) ==
op := first(t)
member(op, ["Float", "DoubleFloat"]) => true
member(op, ["Complex", "Expression", "List", "Polynomial",
"Matrix", "Vector"]) => isApproximate(first(rest(t)))
false
mmCost(name, sig,cond,tar,args1,args2) ==
cost := mmCost0(name, sig,cond,tar,args1,args2)
res := CADR sig
res = $PositiveInteger => cost - 2
res = $NonNegativeInteger => cost - 1
res = $DoubleFloat => cost + 1
cost
mmCost0(name, sig,cond,tar,args1,args2) ==
sigArgs := CDDR sig
n:=
null cond => 1
not (or/cond) => 1
0
-- try to favor homogeneous multiplication
--if name = "*" and 2 = #sigArgs and first sigArgs ~= first rest sigArgs then n := n + 1
-- because of obscure problem in evalMm, sometimes we will have extra
-- modemaps with the wrong number of arguments if we want to the one
-- with no arguments and the name is overloaded. Thus check for this.
nargs := #args1
if args1 then
for x1 in args1 for x2 in args2 for x3 in sigArgs repeat
n := n +
isEqualOrSubDomain(x1,x3) => 0
topcon := first deconstructT x1
topcon2 := first deconstructT x3
topcon = topcon2 => 3
first topcon2 = 'Mapping => 2
4
if isApproximate(x1) ~= isApproximate(x3) then
n := n + 10*nargs
else if sigArgs then n := n + 100000000000
res := CADR sig
res=tar => 10000*n
10000*n + 1000*domainDepth(res) + hitListOfTarget(res)
orderMms(name, mmS,args1,args2,tar) ==
-- it counts the number of necessary coercions of the argument types
-- if this isn't enough, it compares the target types
mmS and null rest mmS => mmS
mS:= NIL
N:= NIL
for mm in MSORT mmS repeat
[sig,.,cond]:= mm
b:= 'T
p:= CONS(m := mmCost(name, sig,cond,tar,args1,args2),mm)
mS:=
null mS => list p
m < CAAR mS => CONS(p,mS)
S:= mS
until b repeat
b := null rest S or m < CAADR S =>
RPLACD(S, CONS(p, rest S))
S := rest S
mS
mmS and [rest p for p in mS]
domainDepth(d) ==
-- computes the depth of lisp structure d
atom d => 0
MAX(domainDepth(first d) + 1, domainDepth(rest d))
hitListOfTarget(t) ==
-- assigns a number between 1 and 998 to a type t
-- want to make it hard to go to Polynomial Pi
t = '(Polynomial (Pi)) => 90000
EQ(first t, 'Polynomial) => 300
EQ(first t, 'List) => 400
EQ(first t, 'Matrix) => 910
EQ(first t, 'UniversalSegment) => 501
EQ(first t, 'Union) => 999
EQ(first t, 'Expression) => 1600
500
isOpInDomain(opName,dom,nargs) ==
-- returns true only if there is an op in the given domain with
-- the given number of arguments
mmList := ASSQ(opName, getOperationAlistFromLisplib first dom)
mmList := subCopy(mmList,constructSubst dom)
null mmList => NIL
gotOne := NIL
nargs := nargs + 1
for mm in rest mmList while not gotOne repeat
nargs = #first mm => gotOne := [mm, :gotOne]
gotOne
findCommonSigInDomain(opName,dom,nargs) ==
-- this looks at all signatures in dom with given opName and nargs
-- number of arguments. If no matches, returns NIL. Otherwise returns
-- a "signature" where a type position is non-NIL only if all
-- signatures shares that type .
first(dom) in '(Union Record Mapping) => NIL
mmList := ASSQ(opName, getOperationAlistFromLisplib first dom)
mmList := subCopy(mmList,constructSubst dom)
null mmList => NIL
gotOne := NIL
nargs := nargs + 1
vec := NIL
for mm in rest mmList repeat
nargs = #first mm =>
null vec => vec := LIST2VEC first mm
for i in 0.. for x in first mm repeat
if vec.i and vec.i ~= x then vec.i := NIL
VEC2LIST vec
findUniqueOpInDomain(op,opName,dom) ==
-- return function named op in domain dom if unique, choose one if not
mmList := ASSQ(opName, getOperationAlistFromLisplib first dom)
mmList := subCopy(mmList,constructSubst dom)
null mmList =>
throwKeyedMsg("S2IS0021",[opName,dom])
mmList := rest mmList -- ignore the operator name
-- use evaluation type context to narrow down the candidate set
if target := getTarget op then
mmList := [mm for mm in mmList | mm is [=rest target,:.]]
null mmList => throwKeyedMsg("S2IS0061",[opName,target,dom])
if #mmList > 1 then
mm := selectMostGeneralMm mmList
sayKeyedMsg("S2IS0022", [opName, dom, ['Mapping, :first mm]])
else mm := first mmList
[sig,slot,:.] := mm
fun :=
--+
$genValue =>
compiledLookupCheck(opName,sig,evalDomain dom)
NRTcompileEvalForm(opName, sig, evalDomain dom)
NULL(fun) or NULL(PAIRP(fun)) => NIL
first fun = function(Undef) => throwKeyedMsg("S2IS0023", [opName, dom])
binVal :=
$genValue => wrap fun
fun
putValue(op,objNew(binVal,m:=['Mapping,:sig]))
putModeSet(op,[m])
selectMostGeneralMm mmList ==
-- selects the modemap in mmList with arguments all the other
-- argument types can be coerced to
-- also selects function with #args closest to 2
min := 100
mml := mmList
while mml repeat
[mm,:mml] := mml
sz := #first mm
if (met := ABS(sz - 3)) < min then
min := met
fsz := sz
mmList := [mm for mm in mmList | (#first mm) = fsz]
mml := rest mmList
genMm := first mmList
while mml repeat
[mm,:mml] := mml
and/[canCoerceFrom(genMmArg,mmArg) for mmArg in CDAR mm
for genMmArg in CDAR genMm] => genMm := mm
genMm
findFunctionInDomain(op,dc,tar,args1,args2,$Coerce,$SubDom) ==
-- looks for a modemap for op with signature args1 -> tar
-- in the domain of computation dc
-- tar may be NIL (= unknown)
null isLegitimateMode(tar, nil, nil) => nil
dcName := first dc
member(dcName,'(Union Record Mapping Enumeration)) =>
-- First cut code that ignores args2, $Coerce and $SubDom
-- When domains no longer have to have Set, the hard coded 6 and 7
-- should go.
op = '_= =>
#args1 ~= 2 or args1.0 ~= dc or args1.1 ~= dc => NIL
tar and tar ~= '(Boolean) => NIL
[[[dc, '(Boolean), dc, dc], ['(Boolean),'$,'$], [NIL, NIL]]]
op = 'coerce =>
dcName='Enumeration and (args1.0=$Symbol or tar=dc)=>
[[[dc, dc, $Symbol], ['$,$Symbol], [NIL, NIL]]]
args1.0 ~= dc => NIL
tar and tar ~= $OutputForm => NIL
[[[dc, $OutputForm, dc], [$OutputForm, '$], [NIL, NIL]]]
member(dcName,'(Record Union)) =>
findFunctionInCategory(op,dc,tar,args1,args2,$Coerce,$SubDom)
NIL
fun:= NIL
( p := ASSQ(op,getOperationAlistFromLisplib dcName) ) and
SL := constructSubst dc
-- if the arglist is homogeneous, first look for homogeneous
-- functions. If we don't find any, look at remaining ones
if isHomogeneousList args1 then
q := NIL
r := NIL
for mm in rest p repeat
-- CDAR of mm is the signature argument list
if isHomogeneousList CDAR mm then q := [mm,:q]
else r := [mm,:r]
q := allOrMatchingMms(q,args1,tar,dc)
for mm in q repeat
fun:= nconc(fun,findFunctionInDomain1(mm,op,tar,args1,args2,SL))
r := reverse r
else r := rest p
r := allOrMatchingMms(r,args1,tar,dc)
if not fun then -- consider remaining modemaps
for mm in r repeat
fun:= nconc(fun,findFunctionInDomain1(mm,op,tar,args1,args2,SL))
if not fun and $reportBottomUpFlag then
sayMSG concat
['" -> no appropriate",:bright op,'"found in",
:bright prefix2String dc]
fun
allOrMatchingMms(mms,args1,tar,dc) ==
-- if there are exact matches on the arg types, return them
-- otherwise return the original list
null mms or null rest mms => mms
x := NIL
for mm in mms repeat
[sig,:.] := mm
[res,:args] := MSUBSTQ(dc,"$",sig)
args ~= args1 => nil
x := CONS(mm,x)
if x then x
else mms
isHomogeneousList y ==
y is [x] => true
y and rest y =>
z := first y
"and"/[x = z for x in rest y]
NIL
findFunctionInDomain1(omm,op,tar,args1,args2,SL) ==
dc := rest (dollarPair := ASSQ('$, SL))
-- need to drop '$ from SL
mm:= subCopy(omm, SL)
-- tests whether modemap mm is appropriate for the function
-- defined by op, target type tar and argument types args
$RTC:local:= NIL
-- $RTC is a list of run-time checks to be performed
[sig,slot,cond,y] := mm
[osig,:.] := omm
osig := subCopy(osig, SUBSTQ(CONS('$,'$), dollarPair, SL))
if CONTAINED('_#, sig) or CONTAINED('construct, sig) then
sig := [replaceSharpCalls t for t in sig]
matchMmCond cond and matchMmSig(mm,tar,args1,args2) and
EQ(y,'Subsumed) and
-- hmmmm: do Union check in following because (as in DP)
-- Unions are subsumed by total modemaps which are in the
-- mm list in findFunctionInDomain.
y := 'ELT -- if subsumed fails try it again
not $SubDom and first sig isnt ['Union, :.] and slot is [tar, :args] and
(f := findFunctionInDomain(op,dc,tar,args,args,NIL,NIL)) => f
EQ(y,'ELT) => [[CONS(dc,sig),osig,nreverse $RTC]]
EQ(y,'CONST) => [[CONS(dc,sig),osig,nreverse $RTC]]
EQ(y,'ASCONST) => [[CONS(dc,sig),osig,nreverse $RTC]]
y is ['XLAM,:.] => [[CONS(dc,sig),y,nreverse $RTC]]
sayKeyedMsg("S2IF0006",[y])
NIL
--------------------> NEW DEFINITION (override in xrun.boot.pamphlet)
findFunctionInCategory(op,dc,tar,args1,args2,$Coerce,$SubDom) ==
-- looks for a modemap for op with signature args1 -> tar
-- in the domain of computation dc
-- tar may be NIL (= unknown)
dcName := first dc
not MEMQ(dcName,'(Record Union Enumeration)) => NIL
fun:= NIL
-- cat := constructorCategory dc
makeFunc := GET(dcName, "makeFunctionList") or
systemErrorHere '"findFunctionInCategory"
[funlist,.] := FUNCALL(makeFunc,"$",dc,$CategoryFrame)
-- get list of implementations and remove sharps
maxargs := -1
impls := nil
for [a,b,d] in funlist repeat
not EQ(a,op) => nil
d is ['XLAM,xargs,:.] =>
if PAIRP(xargs) then maxargs := MAX(maxargs,#xargs)
else maxargs := MAX(maxargs,1)
impls := cons([b,nil,true,d],impls)
impls := cons([b,d,true,d],impls)
impls := NREVERSE impls
if maxargs ~= -1 then
SL:= NIL
for i in 1..maxargs repeat
impls := SUBSTQ(GENSYM(),INTERNL('"#",STRINGIMAGE i),impls)
impls and
SL:= constructSubst dc
for mm in impls repeat
fun:= nconc(fun,findFunctionInDomain1(mm,op,tar,args1,args2,SL))
if not fun and $reportBottomUpFlag then
sayMSG concat
['" -> no appropriate",:bright op,'"found in",
:bright prefix2String dc]
fun
matchMmCond(cond) ==
-- tests the condition, which comes with a modemap
-- cond is 'T or a list, but I hate to test for 'T (ALBI)
$domPvar: local := nil
atom cond or
cond is ['AND,:conds] or cond is ['and,:conds] =>
and/[matchMmCond c for c in conds]
cond is ['OR,:conds] or cond is ['or,:conds] =>
or/[matchMmCond c for c in conds]
cond is ['has,dom,x] =>
hasCaty(dom,x,NIL) ~= 'failed
cond is ['not,cond1] => not matchMmCond cond1
keyedSystemError("S2GE0016",
['"matchMmCond",'"unknown form of condition"])
matchMmSig(mm,tar,args1,args2) ==
-- matches the modemap signature against args1 -> tar
-- if necessary, runtime checks are created for subdomains
-- then the modemap condition is evaluated
[sig,:.]:= mm
if CONTAINED('_#, sig) then
sig := [replaceSharpCalls COPY t for t in sig]
null args1 => matchMmSigTar(tar, first sig)
a := rest sig
arg:= NIL
for i in 1.. while args1 and args2 and a until not b repeat
x1 := first args1
args1 := rest args1
x2 := first args2
args2 := rest args2
x := first a
a := rest a
rtc:= NIL
if x is ['SubDomain,y,:.] then x:= y
b := isEqualOrSubDomain(x1,x) or
(STRINGP(x) and (x1 is ['Variable,v]) and (x = PNAME v)) or
$SubDom and isSubDomain(x,x1) => rtc:= 'T
$Coerce => x2=x or canCoerceFrom(x1,x)
x1 is ['Variable,:.] and x = '(Symbol)
$RTC:= CONS(rtc,$RTC)
null args1 and null a and b and matchMmSigTar(tar, first sig)
matchMmSigTar(t1,t2) ==
-- t1 is a target type specified by :: or by a declared variable
-- t2 is the target of a modemap signature
null t1 or
isEqualOrSubDomain(t2,t1) => true
if t2 is ['Union,a,b] then
if a='"failed" then return matchMmSigTar(t1, b)
if b='"failed" then return matchMmSigTar(t1, a)
$Coerce and
isPartialMode t1 => resolveTM(t2,t1)
-- I think this should be true -SCM
-- true
canCoerceFrom(t2,t1)
constructSubst(d) ==
-- constructs a substitution which substitutes d for $
-- and the arguments of d for #1, #2 ..
SL:= list CONS('$,d)
for x in rest d for v in $FormalMapVariableList repeat
SL:= CONS(CONS(v,x),SL)
SL
filterModemapsFromPackages(mms, names, op) ==
-- mms is a list of modemaps
-- names is a list of domain constructors
-- this returns a 2-list containing those modemaps that have one
-- of the names in the package source of the modemap and all the
-- rest of the modemaps in the second element.
good := NIL
bad := NIL
-- hack to speed up factorization choices for mpolys and to overcome
-- some poor naming of packages
mpolys := '("Polynomial" "MultivariatePolynomial"
"DistributedMultivariatePolynomial"
"HomogeneousDistributedMultivariatePolynomial")
mpacks := '("MFactorize" "MRationalFactorize")
for mm in mms repeat
isFreeFunctionFromMm(mm) => bad := cons(mm, bad)
type := getDomainFromMm mm
null type => bad := cons(mm,bad)
if PAIRP type then type := first type
GETDATABASE(type,'CONSTRUCTORKIND) = 'category => bad := cons(mm,bad)
name := object2String type
found := nil
for n in names while not found repeat
STRPOS(n,name,0,NIL) => found := true
-- hack, hack