File: view.lisp

package info (click to toggle)
cafeobj 1.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 19,900 kB
  • sloc: lisp: 85,055; sh: 659; makefile: 437; perl: 147
file content (1423 lines) | stat: -rw-r--r-- 64,588 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*-
;;;
;;; Copyright (c) 2000-2018, Toshimi Sawada. 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.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; 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 AUTHOR 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.
;;;
(in-package :chaos)
#|==============================================================================
                                 System: CHAOS
                                 Module: deCafe
                                File: view.lisp
==============================================================================|#
#-:chaos-debug
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
#+:chaos-debug
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))

(declaim (special *modexp-abstract-module*))
(defvar *modexp-abstract-module* nil)

;;; (defvar *on-view-debug* nil)

;;; *******************************
;;; CONSTRUCTING MODMORPH FROM VIEW
;;; *******************************

;;;=============================================================================
;;; VIEWS-TO-MODMORPH : module list-of-args -> ModMorph
;;; using `module' in argument so can get the views used in defintion of module.
;;; list-of-args = ((%!arg arg-name view) ...)
;;;-----------------------------------------------------------------------------

(defun views-to-modmorph (mod args &optional (warn t))
  (let* ((morphs (mapcar #'(lambda (arg)
                             (view->modmorph mod arg))
                         args))
         (m-morph (car morphs)))
    (dolist (m (cdr morphs))
      (setq m-morph (modmorph-merge m-morph m warn)))
    m-morph))

(defun view->modmorph (mod arg)
  (let ((arg-name (%!arg-name arg))) 
    (when (and (consp arg-name)
               (cdr arg-name))
      (setq arg-name
            (cons (car arg-name)
                  ;; also local
                  (eval-modexp (cdr arg-name) t))))
    (let ((ath (find-parameterized-submodule arg-name mod))
          (vw (%!arg-view arg)))
      (when (or (modexp-is-error ath) (null ath))
        (with-output-panic-message ()
          (format t "could not find theory module specified by the argument ~s"
                  (%!arg-name arg))
          (chaos-error 'panic)))
      (convert-view-to-modmorph ath vw))))

;;; CONVERT-VIEW-TO-MODMORPH ath avw
;;; ath -- the actual theory in the parameterized module.
;;; avw -- view.
;;;

(defun convert-view-to-modmorph (ath avw)
  (unless (view-p avw)
    (break "Internal Error : convert-view-to-modmorph: illegal form; incomplete"))
  (unless (eq ath (view-src avw))
    ;; this happens when we give predefined named view as argument.
    ;; reconstruct the real view.
    (setq avw (make-real-view ath avw)))
  ;;
  (let ((modm (acons  ath
                      avw               ; needs to be the view itself
                                        ; to allow proper composition.
                      nil))
        (sortm (view-sort-maps avw))
        (opm (mapcar #'(lambda (x)
                         (if (operator-method-p (car x))
                             ;; no need to convert
                             x
                             (cons (term-head (car x))
                                   `(:replacement ,(term-subterms (car x))
                                     ,(cadr x)))))
                     (view-op-maps avw)))
        (modmorph nil))
    (setq modmorph (create-modmorph `(%map ,ath ,avw) sortm opm modm))
    ;;
    #||
    (when (int-instantiation-p (view-target avw))
      ;; target is another instantiation
      (let ((mappg (views-to-modmorph (int-instantiation-module (view-target avw))
                                      (int-instantiation-args (view-target avw))
                                      nil)))
        (setq modmorph (modmorph-merge modmorph mappg nil))))
    ||#
    modmorph
    ))

;;; MAKE-REAL-VIEW : theory view -> view'
;;; fixes real source theory module in view.
;;; reconstruction
;;;
#||
(defun make-real-view (ath avw)
  (labels ((find-sort-or-error (sort)
             (or (find-sort-in ath (sort-id sort))
               (with-output-chaos-error ('no-such-sort)
                 (format t "constructing view, no such sort ~a in " (sort-id sort))
                 (print-mod-name ath *standard-output* t t)
                 )))
           (find-method-or-error (method)
             (or (find-method-in (method-symbol ath)
                                 (mapcar #'find-sort-or-error (method-arity method))
                                 (find-sort-or-error (method-coarity method)))
                 (with-output-chaos-error ('no-such-sort)
                   (princ "constructing view:")
                   (print-next)
                   (princ "~%no such operator ")
                   (print-chaos-object method)
                   (print-next)
                   (princ "in module ")
                   (print-mod-name ath *standard-output* t t)
                   )))
           )
    (let (sort-mapping
          op-mapping
          new-view)
      (setq sort-mapping
            (mapcar #'(lambda (x)
                        (cons (find-sort-or-error (car x))
                              (cdr x)))
                    (view-sort-maps avw)))
      (setq op-mapping
            (mapcar #'(lambda (x)
                        (cons (term-head (car x))
                              `(:replacement ,(term-subterms (car x)) ,(cdr x))))
                  (mapcar #'(lambda (v)
                              (cons (let ((method (term-head (car v))))
                                      (make-term-check-op
                                       (find-method-or-error method
                                            ath
                                            (method-symbol method)
                                            (mapcar #'(lambda (s)
                                                        (find-sort-in ath
                                                                      (sort-id s)))
                                                    (method-arity method))
                                            (find-sort-in ath
                                                          (sort-id (method-coarity
                                                                    method))))
                                       (term-subterms (car v))))
                                    (cadr v)))
                          (view-op-maps avw))))
      (setq new-view (view-struct* (view-struct-name avw)))
      (setf (view-src new-view) ath)
      (setf (view-target new-view) (view-target avw))
      (setf (view-sort-maps new-view) sort-mapping)
      (setf (view-op-maps new-view) op-mapping)
      (setf (view-decl-form new-view) (view-decl-form avw))
      new-view)))
||#

(defun make-real-view (ath avw)
  (let ((form (view-decl-form avw)))
    (let ((new-view (create-view ath (view-target avw) (%view-map form) nil)))
      (setf (view-name new-view) (view-name avw))
      (setf (view-decl-form new-view) form)
      new-view)))

;;; *****************************************
;;; RESOLVING REFERENCES AND MAPPINGS IN VIEW
;;; *****************************************

;;;=============================================================================
;;; CONSTRUCT VIEW OBJECT
;;;

;;; seems over general in our case. 
;;;
(defun view-map-image-sort (mod x sort-map)
  (let ((val (find-if #'(lambda (v) (sort= x (car v))) sort-map)))
    (if val
        (cdr  val)
        (if (memq x (module-all-sorts mod))
            x
            (let ((val2 (find-sort-in mod (sort-id x))))
              (if val2 val2 x))))))

(defun view-map-image-sorts (mod l sort-map)
  (mapcar #'(lambda (x) (view-map-image-sort mod x sort-map))
          l))

;;;
;;; COMPLETE-VIEW
;;;
(defun complete-view (vw &optional arg-name mod pre-map)
  (let ((view nil)
        (source-module nil))
    ;;
    ;; construct the view object to be used for instantiation process.
    ;;
    (let ((target (if (eq 'none (%view-target vw))
                      (with-output-chaos-error ('modexp-eval)
                        (princ "target module cannot be omitted in view.")
                        )
                      (%view-target vw))))
      ;; evaluate source
      (cond (arg-name                   ;formal argument name --> theory module
             ;; resolve qualification in arg-name
             (when (and (consp arg-name) (cdr arg-name))
               ;; qualified view (arg-name . context)
               (setq arg-name (cons (car arg-name)
                                    ;; also local
                                    (eval-modexp (cdr arg-name) t)))
               (when (modexp-is-error (cdr arg-name))
                 (with-output-chaos-error ('modexp-eval)
                   (format t "error in argument name : no such module ~a." (cdr arg-name))
                   )))
             (setq source-module (find-parameterized-submodule arg-name mod)))
            (t (setq source-module (eval-modexp (%view-module vw) t))))
      (when (modexp-is-error source-module)
        (with-output-chaos-error ('modexp-eval)
          (cond (arg-name
                 (format t "no such parameter ~a" (if (consp arg-name)
                                                      (car arg-name)
                                                      arg-name))
                 (print-next)
                 (princ "in module ")
                 (print-mod-name mod))
                (t (format t "could not evaluate view source module ~a" (%view-module vw))))
          ))
      ;;
      (when (modexp-is-?name? target)
        (setq target (?name-name target)))
      ;; target can be a predefined view name.
      (when (stringp target)
        (setq view (find-view-in-env (normalize-modexp target)))
        (when (and view (view-is-inconsistent view))
          ;; reconstruct from the scratch
          ;; view will be updated by side-effect.
          (eval-modexp (view-decl-form view))
          ;; set dependency relation
          (add-depend-relation mod :view view)))
      ;;
      (if view
          view
          ;; we construct a brand new view object.
          (progn
            (setq view (create-view source-module
                                    target
                                    (%view-map vw)
                                    pre-map))
            (mark-view-as-consistent view)
            (setf (view-decl-form view) vw)
            view)))))

;;;
;;; CREATE-VIEW
;;;
(defun principal-sort (module)
  (or (module-principal-sort module)
      (let ((sorts (module-all-sorts module)))
        (if (null (cdr sorts))
            (car sorts)
            (if (not (module-is-hard-wired module))
                (progn
                  (setq sorts
                        (remove-if #'(lambda (x) (module-is-hard-wired (sort-module x)))
                                   sorts))
                  (if (null (cdr sorts))
                      (car sorts)
                      nil))
                nil)))))

#||
(defun create-view (th mod rename-map)
  (let ((vw-map (if (%is-rmap rename-map)
                    (%rmap-map rename-map)
                    rename-map)))
    ;;
    ;; eval source & target module
    ;;
    (let ((src-mod (eval-modexp th t))   ; should already evaluated but..
          (dst-mod (eval-modexp mod t))) ; really evaluate the target.
      (when (or (modexp-is-error src-mod) (modexp-is-error dst-mod))
        ;; error, ivalid args as modules
        (with-output-chaos-error ('modexp-eval)
          (princ "Cannot evaluate module in view: ")
          (when (modexp-is-error src-mod)
            (princ "view source = ")
            (print-modexp th))
          (when (modexp-is-error dst-mod)
            (when (modexp-is-error src-mod)
              (princ ", and "))
            (princ "view target = ")
            (print-modexp mod))
          ))
      ;;
      ;; compute mappings.
      ;;
      (let ((sort-maps (cadr (assq '%ren-sort vw-map)))
            (op-maps   (cadr (assq '%ren-op vw-map)))
            (hsort-maps (cadr (assq '%ren-hsort vw-map)))
            (bop-maps (cadr (assq '%ren-bop vw-map)))
            (vars (cadr (assq '%vars vw-map)))
            (pr (principal-sort src-mod))
                                        ; NOW we support. May/'97
            )
        ;;
        ;; ** compute sort mappings ---------------------------------
        ;;
        (let ((sort-mapping (compute-sort-mappings src-mod
                                                   dst-mod
                                                   sort-maps
                                                   hsort-maps)))
          ;; make sort mappings with completing sort maps not explicitly
          ;; specified.
          (dolist (s (module-all-sorts src-mod))
            (unless (*find-sort-in-map sort-mapping s)
              ;; map is not specified.
              (let ((val (find-sort-in dst-mod (sort-id s) t)))
                (when (eq s pr)         ; is this the principal sort?
                  (let ((pval (principal-sort dst-mod)))
                    (when pval
                      (setq val pval))))
                ;; find compatible same name sort ---
                ;; should check types,i.e., visible & hidden.
                (if val
                    (unless (sort= s val)
                      (push (cons s val) sort-mapping))
                    (with-output-chaos-warning ()
                      (princ "view incomplete for sort ")
                      (print-chaos-object s)
                      (print-next)
                      (princ "view from ")
                      (print-chaos-object src-mod)
                      (princ " to ")
                      (print-chaos-object dst-mod)
                      (print-next)
                      (princ "!! MAY BE HARMFUL !!")
                      ;; (chaos-error 'modexp-eval)
                      )
                    )
                )))
          ;;
          ;; ** compute operator mapping ------------------------------------
          ;;
          (let* ((src-vars
                  (mapcan #'(lambda (x)
                              (let ((sort (find-sort-in src-mod
                                                        (cadr x))))
                                (unless sort
                                  (with-output-chaos-error ('modexp-eval)
                                    (format t "sort ~a not found in view variable"
                                            (cadr x))
                                    ))
                                (mapcar #'(lambda (y)
                                            (make-variable-term sort
                                                                (if (stringp y)
                                                                    (intern y)
                                                                    y)))
                                        (car x))))
                          vars))
                 (dst-vars
                  (mapcar #'(lambda (x)
                              (let ((val (cdr (assq (variable-sort x)
                                                    sort-mapping))))
                                (if val
                                    (make-variable-term val (variable-name x))
                                    x)))
                          src-vars))
                 ;;
                 (op-mapping (compute-op-mappings src-mod
                                                  dst-mod
                                                  sort-mapping
                                                  op-maps
                                                  bop-maps
                                                  src-vars
                                                  dst-vars
                                                  )))
            ;; now op-mapping is in form of ( op-map ... ),
            ;; where op-map is
            ;;       (source-pattern target-pattern)
            ;; this will be converted to operator map of modmorhp:
            ;;       (method :replacement List[psuedo-var] target-pattern)
            ;;    >> *NOTE* we do not use ':simple-map for view.
            ;; we are now about to complete method mapping.
            ;;    >> *NOTE* handle constants last.
            ;; make method map with the same name iff not mapped already.
            ;;
            (dolist (oinfo (module-all-operators src-mod))
              (dolist (method (opinfo-methods oinfo))
                (block next-method
                  (let ((target-method nil))
                    (when (and
                           (or (method-is-user-defined-error-method method)
                               (not (method-is-error-method method)))
                           (let ((xmod (method-module method)))
                             (not
                              (memq xmod *kernel-hard-wired-builtin-modules*))
                             ))
                      (unless (*find-method-in-map op-mapping method)
                        ;;
                        ;; not in the map
                        ;;
                        (when *on-view-debug*
                          (format t "~%[create-view] :")
                          (format t "~&-find non-specified method ")
                          (print-method method)
                          (format t "~&-try finding in module ")
                          (print-mod-name dst-mod))
                        ;; find in destination module with
                        ;; name, arity and coarity mapped by sort-map.
                        (unless (sort= (method-coarity method) *sort-id-sort*)
                          (setq target-method
                                (find-method-in dst-mod
                                                (method-symbol method)
                                                (view-map-image-sorts
                                                 dst-mod
                                                 (method-arity method)
                                                 sort-mapping)
                                                (view-map-image-sort
                                                 dst-mod
                                                 (method-coarity method)
                                                 sort-mapping)))
                          (unless target-method
                            ;; failed to find.
                            ;; we continue, but may cause big problem later
                            ;; so gives a warning.
                            (with-output-chaos-warning ()
                              (princ "view incomplete for operator ")
                              (print-method method)
                              (princ " of ")
                              (print-mod-name src-mod)
                              (print-next)
                              (princ "view to ")
                              (print-mod-name dst-mod)
                              (print-next)
                              (princ "!! MAY CAUSE PANIC LATER !!"))
                            (return-from next-method nil)
                            )

                          ;;
                          ;; found the target-method in dst-mod,
                          ;; i.e., with the same name & has proper rank.
                          ;;
                          (unless (eq method target-method)
                            ;; construct map iff they are different object.
                            (let ((vars (make-psuedo-vars-from-sorts
                                         (method-arity method))))
                              (when *on-view-debug*
                                (format t "~%-*creating new op-mapping ")
                                (format t "~& from ")
                                (print-method method)
                                (format t "~& to ")
                                (print-method target-method))
                              (push (list (make-term-check-op method vars src-mod)
                                          (make-term-check-op target-method
                                                              vars
                                                              dst-mod))
                                    op-mapping)
                              )
                            ;;
                            (let ((thy1 (method-theory method
                                                       (module-opinfo-table src-mod)))
                                  (thy2 (method-theory target-method
                                                       (module-opinfo-table dst-mod))))
                              (let ((z1 (car (theory-zero thy1)))
                                    (z2 (car (theory-zero thy2))))
                                (when (and z1
                                           (not (*find-method-in-map op-mapping
                                                                     (term-head z1)))
                                           z2
                                           (term-is-constant? z1)
                                           (term-is-constant? z2))
                                  (push (list z1 z2)
                                        op-mapping))))))))))))
            ;;
            ;; make SortId op mappings
            ;;
            (let ((s-op nil)
                  (t-op nil)
                  (ignore-these (list *cosmos* *universal-sort*
                                      *huniversal-sort* *bottom-sort*)))
              (dolist (s-map sort-mapping)
                (let ((source (car s-map))
                      (target (cdr s-map)))
                  (break)
                  (unless (or (err-sort-p source)
                              (memq source ignore-these)
                              (memq target ignore-these))
                    (setq s-op (find-method-in src-mod
                                               (string (sort-id source))
                                               nil
                                               *sort-id-sort*))
                    (unless (*find-method-in-map op-mapping s-op)
                      (setq t-op (find-method-in dst-mod
                                                 (string (sort-id target))
                                                 nil
                                                 *sort-id-sort*))
                      (push (list (make-term-check-op s-op nil src-mod)
                                  (make-term-check-op t-op nil dst-mod))
                            op-mapping))))))
            
            ;;
            ;; final result
            ;;
            (let ((view (view-struct* :anon-view)))
              (initialize-view view)
              (setf (view-src view) src-mod
                    (view-target view) dst-mod
                    (view-sort-maps view) sort-mapping
                    (view-op-maps view) op-mapping)
              ;;
              (when *on-view-debug*
                (format t "~%[generated view]:")
                (format t "~& source = ") (print-chaos-object src-mod)
                (format t "~& target = ") (print-chaos-object dst-mod)
                (format t "~& sort-map = ")
                (dolist (smap sort-mapping)
                  (terpri)
                  (print-chaos-object smap))
                (format t "~& op-map =")
                (dolist (opmap op-mapping)
                  (terpri)
                  (print-term-head (car opmap))
                  (princ " --> ")
                  (princ (cdr opmap))
                  ;; (print-chaos-object opmap)
                  ))
              ;;
              view)
            ))))))

||#

(defun create-view (th mod rename-map pre-map)
  (let ((vw-map (if (%is-rmap rename-map)
                    (%rmap-map rename-map)
                    rename-map)))
    ;;
    ;; eval source & target module
    ;;
    (let ((src-mod (eval-modexp th t))   ; should already evaluated but..
          (dst-mod (eval-modexp mod t))) ; really evaluate the target.
      (when (or (modexp-is-error src-mod) (modexp-is-error dst-mod))
        ;; error, ivalid args as modules
        (with-output-chaos-error ('modexp-eval)
          (princ "Cannot evaluate module in view: ")
          (when (modexp-is-error src-mod)
            (princ "view source = ")
            (print-modexp th))
          (when (modexp-is-error dst-mod)
            (when (modexp-is-error src-mod)
              (princ ", and "))
            (princ "view target = ")
            (print-modexp mod))
          ))
      ;;
      ;; compute mappings.
      ;;
      (let ((pre-sort-mapping nil)
            (pre-op-mapping nil)
            )
        ;;
        (when pre-map
          (setq pre-sort-mapping (modmorph-sort pre-map))
          (setq pre-op-mapping (modmorph-op pre-map)))
        #||
        (when (int-instantiation-p dst-mod)
          (let ((mappg (views-to-modmorph (int-instantiation-module dst-mod)
                                          (int-instantiation-args dst-mod))))
            (setq pre-sort-mapping (modmorph-sort mappg))
            (setq pre-op-mapping (modmorph-op mappg))))
        ||#
        ;;

        (let ((sort-maps (cadr (assq '%ren-sort vw-map)))
              (op-maps   (cadr (assq '%ren-op vw-map)))
              (hsort-maps (cadr (assq '%ren-hsort vw-map)))
              (bop-maps (cadr (assq '%ren-bop vw-map)))
              (vars (cadr (assq '%vars vw-map)))
              (pr (principal-sort src-mod))
                                        ; NOW we support. May/'97
              )
          ;;
          ;; ** compute sort mappings ---------------------------------
          ;;
          (let ((sort-mapping (compute-sort-mappings src-mod
                                                     dst-mod
                                                     sort-maps
                                                     hsort-maps)))
            #||
            (when pre-sort-mapping
              (setq sort-mapping
                    (modmorph-merge-assoc sort-mapping pre-sort-mapping nil)))
            ||#
            ;; make sort mappings with completing sort maps not explicitly
            ;; specified.
            (dolist (s (module-all-sorts src-mod))
              (unless (*find-sort-in-map sort-mapping s)
                ;; map is not specified.
                ;; we prefer map in pre-sort-mapping iff given.
                (let ((pre (assq s pre-sort-mapping)))
                  (if pre
                      (push pre sort-mapping)
                      ;; else find by `the same name principle.'
                      (let ((val (find-sort-in dst-mod (sort-id s) t)))
                        (when (eq s pr) ; is this the principal sort?
                          (let ((pval (principal-sort dst-mod)))
                            (when pval
                              (setq val pval))))
                        ;; find compatible same name sort ---
                        ;; should check types,i.e., visible & hidden.
                        (if val
                            (unless (sort= s val)
                              (push (cons s val) sort-mapping))
                            (with-output-chaos-warning ()
                              (princ "view incomplete for sort ")
                              (print-chaos-object s)
                              (print-next)
                              (princ "view from ")
                              (print-chaos-object src-mod)
                              (princ " to ")
                              (print-chaos-object dst-mod)
                              (print-next)
                              (princ "!! this can cause panic.")
                              ;; (chaos-error 'modexp-eval)
                              )))
                      ))))
            ;;
            ;; ** compute operator mapping ------------------------------------
            ;;
            (let* ((src-vars
                    (mapcan #'(lambda (x)
                                (let ((sort (find-sort-in src-mod
                                                          (cadr x))))
                                  (unless sort
                                    (with-output-chaos-error ('modexp-eval)
                                      (format t "sort ~a not found in view variable"
                                              (cadr x))
                                      ))
                                  (mapcar #'(lambda (y)
                                              (make-variable-term sort
                                                                  (if (stringp y)
                                                                      (intern y)
                                                                      y)))
                                          (car x))))
                            vars))
                   (dst-vars
                    (mapcar #'(lambda (x)
                                (let ((val (cdr (assq (variable-sort x)
                                                      sort-mapping))))
                                  (if val
                                      (make-variable-term val (variable-name x))
                                      x)))
                            src-vars))
                   ;;
                   (op-mapping (compute-op-mappings src-mod
                                                    dst-mod
                                                    sort-mapping
                                                    op-maps
                                                    bop-maps
                                                    src-vars
                                                    dst-vars
                                                    )))
              #||
              (when pre-op-mapping
                (setq op-mapping
                      (modmorph-merge-op-assoc op-mapping
                                               pre-op-mapping)))
              ||#
              ;; now op-mapping is in form of ( op-map ... ),
              ;; where op-map is
              ;;       (source-pattern target-pattern)
              ;; this will be converted to operator map of modmorhp:
              ;;       (method :replacement List[psuedo-var] target-pattern)
              ;;    >> *NOTE* we do not use ':simple-map for view.
              ;; we are now about to complete method mapping.
              ;;    >> *NOTE* handle constants last.
              ;; make method map with the same name iff not mapped already.
              ;;
              (dolist (oinfo (module-all-operators src-mod))
                (dolist (method (opinfo-methods oinfo))
                  (block next-method
                    (let ((target-method nil))
                      (when (and
                             (or (method-is-user-defined-error-method method)
                                 ;; t
                                 (not (method-is-error-method method)))
                             (let ((xmod (method-module method)))
                               (not
                                (memq xmod *kernel-hard-wired-builtin-modules*))
                               ))
                        (unless (*find-method-in-map op-mapping method)
                          ;;
                          ;; not in the map
                          ;;
                          (when *on-view-debug*
                            (format t "~%[create-view] :")
                            (format t "~&-find non-specified method ")
                            (print-method method)
                            (format t "~&-try finding in module ")
                            (print-mod-name dst-mod))
                          ;; check in pre-op-mapping,
                          ;; if found use this map.
                          (let ((pre (*find-method-in-map pre-op-mapping method)))
                            (if pre
                                (push pre op-mapping)
                                ;; find in destination module with
                                ;; name, arity and coarity mapped by sort-map.
                                (unless (sort= (method-coarity method)
                                               *sort-id-sort*)
                                  (setq target-method
                                        (find-method-in dst-mod
                                                        (method-symbol method)
                                                        (view-map-image-sorts
                                                         dst-mod
                                                         (method-arity method)
                                                         sort-mapping)
                                                        (view-map-image-sort
                                                         dst-mod
                                                         (method-coarity method)
                                                         sort-mapping)))
                                  (unless target-method
                                    ;; failed to find.
                                    ;; we continue, but may cause big problem later
                                    ;; so gives a warning.
                                    (with-output-chaos-warning ()
                                      (princ "view incomplete for operator ")
                                      (print-method method)
                                      (princ " of ")
                                      (print-mod-name src-mod)
                                      (print-next)
                                      (princ "view to ")
                                      (print-mod-name dst-mod)
                                      (print-next)
                                      (princ "can cause panic later !!"))
                                    (return-from next-method nil)
                                    )
                                  ;;
                                  ;; found the target-method in dst-mod,
                                  ;; i.e., with the same name & has proper rank.
                                  ;;
                                  (unless (eq method target-method)
                                    ;; construct map iff they are different object.
                                    (let ((vars (make-psuedo-vars-from-sorts
                                                 (method-arity method))))
                                      (when *on-view-debug*
                                        (format t "~%-*creating new op-mapping ")
                                        (format t "~& from ")
                                        (print-method method)
                                        (format t "~& to ")
                                        (print-method target-method))
                                      (push (list (make-term-check-op
                                                   method
                                                   vars
                                                   src-mod)
                                                  (make-term-check-op
                                                   target-method
                                                   vars
                                                   dst-mod))
                                            op-mapping)
                                      )
                                    ;;
                                    (let ((thy1 (method-theory
                                                 method
                                                 (module-opinfo-table src-mod)))
                                    (thy2 (method-theory
                                           target-method
                                           (module-opinfo-table dst-mod))))
                                (let ((z1 (car (theory-zero thy1)))
                                      (z2 (car (theory-zero thy2))))
                                  (when (and z1
                                             (not (*find-method-in-map
                                                   op-mapping
                                                   (term-head z1)))
                                             z2
                                             (term-is-constant? z1)
                                             (term-is-constant? z2))
                                    (push (list z1 z2)
                                          op-mapping))))))))))))))
              ;;
              ;; make SortId op mappings
              ;;
              (let ((s-op nil)
                    (t-op nil))
                (dolist (s-map sort-mapping)
                  (let ((source (car s-map))
                        (target (cdr s-map))
                        (ignore-these (list *cosmos* *universal-sort*
                                            *huniversal-sort* 
                                            *bottom-sort*)))
                    (unless (or (err-sort-p source)
                                (memq source ignore-these)
                                (memq target ignore-these))
                      (setq s-op (find-method-in src-mod
                                                 (string (sort-id source))
                                                 nil
                                                 *sort-id-sort*))
                      (unless (*find-method-in-map op-mapping s-op)
                        (setq t-op (find-method-in dst-mod
                                                   (string (sort-id target))
                                                   nil
                                                   *sort-id-sort*))
                        (push (list (make-term-check-op s-op nil src-mod)
                                    (make-term-check-op t-op nil dst-mod))
                              op-mapping))))))
              ;;
              ;; final result
              ;;
              (let ((view (view-struct* :anon-view)))
                (initialize-view view)
                (setf (view-src view) src-mod
                      (view-target view) dst-mod
                      (view-sort-maps view) sort-mapping
                      (view-op-maps view) op-mapping)
                ;;
                (when *on-view-debug*
                  (format t "~%[generated view]:")
                  (format t "~& source = ") (print-chaos-object src-mod)
                  (format t "~& target = ") (print-chaos-object dst-mod)
                  (format t "~& sort-map = ")
                  (dolist (smap sort-mapping)
                    (terpri)
                    (print-chaos-object smap))
                  (format t "~& op-map =")
                  (dolist (opmap op-mapping)
                    (terpri)
                    (print-term-head (car opmap))
                    (princ " --> ")
                    (princ (cdr opmap))
                    ;; (print-chaos-object opmap)
                    ))
                ;;
                view)
              )))))))

(defun *find-sort-in-map (map x)
  (let ((imap (find-if #'(lambda (y)
                           (sort= x (car y)))
                       map)))
    (if imap
        (cdr imap)
        nil)))

(defun *find-method-in-map (op-mapping method)
  ;;
  (find-if #'(lambda (x) (if (operator-method-p (car x))
                             (eq method (car x))
                             (eq method (term-head (car x)))))
           op-mapping))

;;; ***********************
;;; COMPUTING REAL MAPPINGS
;;; ***********************

;;; COMPUTE-SORT-MAPPINGS : src-mod dst-mod sort-maps -> sort-mapping
;;;  resolve sort mapping specified by sort-maps
;;; src-mod : source module
;;; dst-mod : target module
;;; sort-maps : list of (from to).
;;;
;;; returns the list of (from . to).
;;; there are no errors.
;;;
(defun compute-sort-mappings (src-mod dst-mod sort-maps hsort-maps)
  (let ((res (nconc (mapcan
                     #'(lambda (x)
                         (compute-sort-mapping src-mod dst-mod x :visible))
                     sort-maps)
                    (mapcan
                     #'(lambda (x)
                         (compute-sort-mapping src-mod dst-mod x :hidden))
                     hsort-maps)))
        (f-so (module-sort-order src-mod))
        (t-so (module-sort-order dst-mod))
        (err-map nil))
    ;; make implicit mapping for error sorts, if not specified.
    (dolist (map res)
      (let ((s-err (the-err-sort (car map) f-so))
            (t-err (the-err-sort (cdr map) t-so)))
        (unless (assq s-err res)
          (push (cons s-err t-err) err-map))))
    (when err-map
      (setq res (nconc res err-map)))
    ;;
    res))

(defun compute-sort-mapping (src-mod dst-mod sort-map &optional (type :visible))
  (let ((srcs (find-sort-in src-mod (car sort-map)))
        (tgts (find-sort-in dst-mod (cadr sort-map))))
    (unless srcs
      (with-output-chaos-error ('modexp-eval)
        (princ "in view from ")
        (print-mod-name src-mod)
        (princ " to ")
        (print-mod-name dst-mod)
        (print-next)
        (princ "source sort not recognized : ")
        (print-sort-ref (car sort-map))
        ))
    (unless tgts
      (with-output-chaos-error ('modexp-eval)
        (princ "in view from ")
        (print-mod-name src-mod)
        (princ " to ")
        (print-mod-name dst-mod)
        (print-next)
        (princ "target sort not recognized: ")
        (print-sort-ref (cadr sort-map))
        ))
    (case type
      (:visible (unless (and (sort-is-visible srcs)
                             (sort-is-visible tgts))
                  (with-output-chaos-error ('invalid-map)
                    (format t "both source(~A) and target(~A) sorts must be visible for `sort' map"
                            (sort-id srcs)
                            (sort-id tgts))
                    )))
      (otherwise (unless (and (sort-is-hidden srcs)
                              (sort-is-hidden tgts))
                   (with-output-chaos-error ('invalid-map)
                     (format t "both source(~A) and target(~A) sort must be hidden for `hsort' map."
                             (sort-id srcs)
                             (sort-id tgts))
                     ))))
    ;;
    (list (cons srcs tgts))))

;;; COMPUTE-OP-MAPPINGS : src-mod dst-mod op-maps bop-maps src-vars dst-vars
;;;                       -> method-mapping
;;; resolve operator mapping specified by view.
;;;
;;; src-mod   : source module object.
;;; dst-mod   : target module object.
;;; sort-maps : list of sort rename map in solved form, ((from . to) ...)
;;; op-maps   : list of operator rename map to be solved
;;;             ((from to) ....)
;;;             i.e., still in non resolved form (:opref).
;;; bop-maps  : similar to op-maps, but maps between behavioural operators.
;;;
;;; result method mapping will be in the form of
;;;  ((from to) .....)
;;; where from is operator-method
;;;       to is one of the followings:
;;;       a) :simple-map 

(defun compute-op-mappings (src-mod dst-mod sort-maps op-maps bop-maps src-vars dst-vars)
  (nconc (mapcan #'(lambda (opmap)
                     (compute-op-mapping src-mod
                                         dst-mod
                                         sort-maps
                                         opmap
                                         src-vars
                                         dst-vars
                                         :functional
                                         ))
                 op-maps)
         (mapcan #'(lambda (opmap)
                     (compute-op-mapping src-mod
                                         dst-mod
                                         sort-maps
                                         opmap
                                         src-vars
                                         dst-vars
                                         :behavioural
                                         ))
                 bop-maps)))

(defun resolve-op-pattern-as-term (mod pat vars)
  (let ((pparsed (let ((*parse-variables* (mapcar #'(lambda (x)
                                                      (cons (variable-name x) x))
                                                  vars)))
                   (if (and (term? pat)
                            (term-is-application-form? pat))
                       pat
                     (parse-quiet-parse mod pat))
                   )
          ))
    (if (term-is-an-error pparsed)
        (when *on-view-debug*
          (with-in-module (mod)
            (print-term-tree pparsed)
          nil))
        (if (term-is-builtin-constant? pparsed)
            (values pparsed
                    (term-builtin-value pparsed)
                    nil
                    nil)
            (values pparsed
                    (term-head pparsed)
                    (term-variables pparsed)
                    (make-psuedo-vars (term-subterms pparsed)))
            ))))

(defun split-str (str)
  (let ((len (length str))
        (index 0)
        (r nil))
    (dotimes (i len (progn (unless (= index len)
                             (push (subseq str index) r))
                           (reverse r)))
      (when (char= (char str i) #\_)
        (unless (= index i)
          (push (subseq str index i) r))
        (push "_" r)
        (setf index (1+ i)))
      )))
             
(defun delimit-op-pat (pat)
  (let ((res nil))
    (dolist (p pat)
      (setf res (nconc res (split-str p))))
    res))

(defun resolve-op-pattern-as-reference (mod pat)
  (setq pat (delimit-op-pat pat))
  (let ((*modexp-parse-input* (append pat '(".")))
        (new-pat nil)
        (info nil)
        (res nil))
    (setq new-pat (parse-operator-reference '(".")))
    (setq info (find-qual-operators new-pat mod))
    #||
    (when (cdr info)
      (with-output-chaos-warning ()
        (format t "operator reference ~A is ambiguous" pat)
        ;;(print-ast new-pat)
        (print-next)
        (princ "in the context: ")
        (print-mod-name mod *standard-output* t)
        ))
    ||#
    ;; we only need methods
    (dolist (i info)
      (dolist (m (opinfo-methods i))
        (push m res)))
    (if res
        (list 'dummy-op (nreverse res))
        nil)))
        
(defun compute-op-mapping (src-mod dst-mod sort-maps op-map src-vars dst-vars
                                   &optional (type :functional))
  (let ((src-opex (car op-map))
        (dst-opex (cadr op-map))
        (src-opinfo nil)
        (dst-opinfo nil)
        (mappings nil)
        (source-map-type :term)
        (target-map-type :term)
        )
    (multiple-value-bind (src-pat src-method src-varbs src-p-vars)
        ;;
        ;; source op pattern
        ;;
        ;; first we assume pattern is given as term.
        (resolve-op-pattern-as-term src-mod src-opex src-vars)
      ;;
      (when src-pat
        (unless (every #'(lambda (x) (term-is-variable? x))
                       (term-subterms src-pat))
          (with-output-chaos-error ('invalid-op-map)
            (princ "mapping operator: ")
            (print-method (term-head src-pat))
            (print-next)
            (princ "source pattern must not have non-variable subterms:")
            ))
        ;; construct a generic pattern with psuedo variables:
        (unless (term-is-builtin-constant? src-pat)
          (setq src-pat
                (make-term-check-op src-method src-p-vars src-mod)))
        (setq source-map-type :term))
      ;;
      ;; also try,
      ;; given source op pattern may be a operator reference.
      ;;
      (unless src-pat
        (setq src-opinfo (resolve-op-pattern-as-reference src-mod src-opex)))
      (if src-opinfo
          (setq source-map-type :op-ref)
          (unless src-pat
            (with-output-chaos-error ('invalid-map)
              (format t "could not resolve source operator pattern : ~a"
                      src-opex)
              )))
      ;;
      ;; target pattern
      ;;
      (multiple-value-bind (dst-pat dst-method dst-varbs dst-p-vars)
          (if src-pat
              (resolve-op-pattern-as-term dst-mod dst-opex dst-vars)
              nil)
        (declare (ignore dst-method dst-p-vars))
        (when dst-pat
          ;; construct generic target pattern:
          ;; replaces 
          (let ((subst nil)
                (src-vn (mapcar #'(lambda (x) (variable-name x))
                                src-varbs)))
            (dolist (dv dst-varbs)
              (let* ((vm (variable-name dv))
                     (vpos (position-if #'(lambda (x) (equal vm x)) src-vn)))
                (if vpos (push (cons dv (nth vpos src-p-vars)) subst))))
            (setq dst-pat
                  (substitution-simple-image subst dst-pat))
            (setq target-map-type :term)))
        ;;
        (when src-opinfo
          (setq dst-opinfo (resolve-op-pattern-as-reference dst-mod dst-opex))
          (if dst-opinfo
              (setq target-map-type :op-ref)
              (unless dst-pat
                (with-output-chaos-error ('invalid-map)
                  (format t "could not resolve operator target pattern : ~a"
                          dst-opex)
                  ))))
        ;;
        ;; make mapping
        ;;
        (cond ((and src-opinfo dst-opinfo)
               ;; construct mappings, target must be always a term.
               (let ((tm-list (opinfo-methods dst-opinfo)))
                 (dolist (sm (opinfo-methods src-opinfo))
                   (when (or (method-is-user-defined-error-method sm)
                             (not (method-is-error-method sm)))
                     (let ((tm (find-method-mapped sm
                                                   sort-maps
                                                   tm-list
                                                   src-mod
                                                   dst-mod)))
                       (if tm
                           (let ((vars (make-psuedo-vars-from-sorts
                                        (method-arity sm))))
                             (push (list (make-term-check-op sm vars src-mod)
                                         (make-term-check-op tm vars dst-mod))
                                   mappings))
                           ;; 
                           (with-output-chaos-error ('modexp-eval)
                             (format t "Operator mapping is not injective, the declaration")
                             (print-next)
                             (print-method sm src-mod)
                             (print-next)
                             (princ "has no proper image in the target,")
                             (print-next)
                             (princ "wrt the sort mappings:")
                             (let ((*print-indent* (+ 2 *print-indent*)))
                               (dolist (smap sort-maps)
                                 (print-next)
                                 (print-sort-name (car smap) src-mod)
                                 (princ " --> ")
                                 (print-sort-name (cdr smap) dst-mod)))
                             )))))))
              ((and src-pat dst-pat)
               ;; must check rank is properly mapped.
               (let ((src-method (if (term-is-applform? src-pat)
                                     (term-head src-pat)
                                     nil))
                     (dst-method (if (term-is-applform? dst-pat)
                                     (term-head dst-pat)
                                     nil)))
                 (declare (ignore dst-method))
                 (let ((coarity-mapped (*image-rename-sort sort-maps
                                                           (term-sort src-pat)))
                       (arity-mapped (if src-method
                                         (*image-rename-sorts sort-maps
                                                              (method-arity
                                                               src-method))
                                         nil)))
                   (declare (ignore arity-mapped))
                   (unless (sort<= (term-sort dst-pat) coarity-mapped
                                   (module-sort-order dst-mod))
                     (with-output-chaos-warning ()
                     (princ "operator mapping is not strict wrt sort map:")
                     (print-next)
                     (princ "* sort map")
                     (print-next)
                     (princ "- ")
                     (print-sort-name (term-sort src-pat) src-mod)
                     (princ " --> ")
                     (print-sort-name coarity-mapped dst-mod)
                     (print-next)
                     (princ "* operator map")
                     (print-next)
                     (princ "- source: ")
                     (with-in-module (src-mod)
                       (cond ((term-is-applform? src-pat)
                              (print-chaos-object (term-head src-pat)))
                             ((term-is-builtin-constant? src-pat)
                              (print-chaos-object src-pat)
                              (princ " : ")
                              (print-sort-name (term-sort src-pat)))
                             (t (print-chaos-object src-pat))))
                     (print-next)
                     (princ "- target: ")
                     (with-in-module (dst-mod)
                       (cond ((term-is-applform? dst-pat)
                              (print-chaos-object (term-head dst-pat)))
                             ((term-is-builtin-constant? dst-pat)
                              (print-chaos-object dst-pat)
                              (princ " : ")
                              (print-sort-name (term-sort dst-pat)))
                             (t (print-chaos-object dst-pat)))))))
                 )
               ;; construct complex pattern mapping
               (push (list src-pat dst-pat) mappings))
              ;;; *************
              (t (with-output-chaos-error ('modexp-eval)
                   ;; some useful message will going here....
                   (princ "source and target of operator mapping must be the same type:")
                   (print-next)
                   (format t "- source type: ~a, target type: ~a"
                           (if (eq source-map-type :term)
                               'term
                               "operator name")
                           (if (eq target-map-type :term)
                               'term
                               "operator name"))
                   )
                 ))))
    (when *on-view-debug*
      (format t "~%[compute op mapping]:")
      (let* ((*print-indent* (+ *print-indent* 2))
             (map (car mappings))
             (src (first map))
             (pvars (term-subterms src))
             (dst (second map)))
        (with-in-module (src-mod)
          (print-next)
          (princ "src-pattern : ")
          (if (term-is-builtin-constant? src)
              (progn (term-print src)
                     (format t " of built-in sort ")
                     (print-sort-name (term-sort src)))
              (print-method (term-head src)))
          (print-next)
          (princ "p-vars : ")
          (print-chaos-object pvars))
        (with-in-module (dst-mod)
          (print-next)
          (princ "tgt-pattern : ")
          (if (term-is-builtin-constant? dst)
              (progn (term-print dst)
                     (format t " of built-in sort ")
                     (print-sort-name (term-sort dst)))
              (print-method (term-head dst)))
          (princ "(") (princ dst) (princ ")"))
        ))
    ;; check all at once
    (let ((error nil))
      (dolist (map mappings)
        (let ((src (first map))
              (dst (second map)))
          (case type
            (:functional                ; both should be functional operators
             (unless (and (term-is-of-functional? src)
                          (term-is-of-functional? dst))
               (with-output-simple-msg ()
                 (format t "[Error] source and target must be non-behavioural for `op' map:")
                 (format t "~&- source ") (print-term-head src src-mod)
                 (format t "~&- target ") (print-term-head dst dst-mod)
                 (setq error t))))
            (otherwise
             (unless (and (term-is-of-behavioural*?  src (module-opinfo-table
                                                          src-mod))
                          (term-is-of-behavioural*? dst (module-opinfo-table
                                                         dst-mod)))
               (with-output-simple-msg ()
                 (format t "[Error] source and target must be behavioural for `bop' map:")
                 (format t "~&- source ") (print-term-head src src-mod)
                 (format t "~&- target ") (print-term-head dst dst-mod)
                 (setq error t)))))))
      (when error (chaos-to-top)))
    ;; 
    mappings))

(defvar *view-map-operator-strictly* nil)

(defun find-method-mapped (src-method sort-maps method-list src-mod dst-mod)
  (macrolet ((is-similar-theory? (th1_? th2_?)
               (once-only (th1_? th2_?)
                 ` (and (if (theory-contains-associativity ,th1_?)
                            (theory-contains-associativity ,th2_?)
                            t)
                        (if (theory-contains-commutativity ,th1_?)
                            (theory-contains-commutativity ,th2_?)
                            t)
                        (if (theory-contains-identity ,th1_?)
                            (theory-contains-identity ,th2_?)
                            t)))))
    (flet ((filter-method (arity coarity theory so)
             (declare (ignore theory))
             (remove-if-not
                  #'(lambda (method)
                      (and (if *view-map-operator-strictly*
                               (sort= (method-coarity method) coarity)
                               (sort<= (method-coarity method) coarity so))
                           (if *view-map-operator-strictly*
                               (sort-list= (method-arity method) arity)
                               (sort-list<= (method-arity method) arity so))
                           #|| ignore operator theory now. 
                           (is-similar-theory? theory
                                               (method-theory method
                                                              (module-opinfo-table
                                                               dst-mod)))
                           ||#
                           ))
                  method-list))
           (most-general-method (methods sort-order)
             (when *on-view-debug*
               (format t "~&[most-general-method]")
               (dolist (x methods)
                 (print-next)
                 (princ "- ")
                 (print-chaos-object x)))
             (let ((res (car methods)))
               (dolist (x (cdr methods))
                 (when (method< x res sort-order)
                   (setq res x)))
               (when *on-view-debug*
                 (format t "~%* result = ")
                 (print-chaos-object res))
               res)))
      ;;
      (let ((coarity (*image-rename-sort sort-maps (method-coarity src-method)))
            (arity (*image-rename-sorts sort-maps (method-arity src-method)))
            (theory (method-theory src-method (module-opinfo-table src-mod)))
            (sort-order (module-sort-order dst-mod)))
        (let ((val (filter-method arity coarity theory sort-order))
              (result nil))
          (if (and val (null (cdr val)))
              (setq result (car val))
              (if val
                  (setq result (most-general-method val sort-order))
                  (progn
                    (setq arity (find-sorts-image* (method-arity src-method)
                                                   sort-maps
                                                   dst-mod))
                    (setq coarity (find-sort-image*
                                   (method-coarity src-method)
                                   sort-maps
                                   dst-mod))
                    (setq val (filter-method arity coarity theory sort-order))
                    (if (and val (null (cdr val)))
                        (setq result (car val))
                        (if (cdr val)
                            (setq result (most-general-method val sort-order))
                            (setq result nil))))))
          ;;
          (when result
            (unless (and (sort= (method-coarity result) coarity)
                         (sort-list= (method-arity result) arity))
              (with-output-chaos-warning ()
                (princ "operator mapping is not strict wrt sort map:")
                (print-next)
                (princ "* sort map")
                (dotimes (x (length arity))
                  (print-next)
                  (princ "- ")
                  (print-sort-name (nth x (method-arity src-method))
                                   src-mod)
                  (princ " --> ")
                  (print-sort-name (nth x arity) dst-mod))
                (print-next)
                (princ "- ")
                (print-sort-name (method-coarity src-method) src-mod)
                (princ " --> ")
                (print-sort-name coarity dst-mod)
                (print-next)
                (princ "* operator map")
                (print-next)
                (princ "- source: ")
                (with-in-module (src-mod)
                  (print-chaos-object src-method))
                (print-next)
                (princ "- target: ")
                (with-in-module (dst-mod)
                  (print-chaos-object result))
                (fresh-line))))
          ;;
          result)))))

(defun find-sorts-image* (sorts sort-map mod)
  (mapcar #'(lambda (s) (find-sort-image* s sort-map mod)) sorts))

(defun find-sort-image* (sort sort-map mod)
  (let ((imap (find-if #'(lambda (x) (sort= sort (car x))) sort-map)))
    (if imap
        (cdr imap)
        (find-sort-in mod (sort-id sort)))))

#||
;;; MODEXP-REPLACE-MAPPING view mapping
;;;
(defun modexp-replace-mapping (vm map)
  (if (memq (ast-type vm) '(%view-from '%view-mapping))
      (progn (setf (ast-type vm) '%view-mapping)
             (setf (%view-mapping-map vm) map))
      (break "Internal Error : Misuse of modexp-replace-mapping.")))
||#

;;; EOF