-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtao-d.lisp
1914 lines (1700 loc) · 69.8 KB
/
tao-d.lisp
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
(tao:common-lisp)
(in-package #:tao-internal)
(eval-when (:compile-toplevel :load-toplevel :execute)
(or (boundp 'tao-lambda-list-keywords)
(defconstant tao-lambda-list-keywords
'(tao:&optional
tao:&optn
tao:&opt
:opt
tao:&rest
:rest
tao:&key
tao:&allow-other-keys
tao:&aux
:aux
tao:&whole
tao:&body
tao:&environment ))))
(defun canonicalize-lambda-list-keyword (arg-list)
(mapcar (lambda (a)
(case a
(tao:&optional 'tao:&optional)
(tao:&optn 'tao:&optional)
(tao:&opt 'tao:&optional)
(:opt 'tao:&optional)
(tao:&rest 'tao:&rest)
(:rest 'tao:&rest)
(tao:&key 'tao:&key)
(tao:&allow-other-keys 'tao:&allow-other-keys)
(tao:&aux 'tao:&aux)
(:aux 'tao:&aux)
(tao:&whole 'tao:&whole)
(tao:&body 'tao:&body)
(tao:&environment 'tao:&environment)
(otherwise a)))
arg-list))
(define
"dashift"
(subr nil)
:documentation
"形式 : dashift object1 object2
object2 を評価した結果(数)だけ object1 の内容を左に (負なら右に)
ビットシフトし、その結果を返す (元のオブジェクトは破壊される)。
サインビットは破壊されない。左へのシフトで空白になったビット部分
には 0 をつめる。右へのシフトで空白になったビット部分にはサインビット
の値をつめる。ashift の破壊版。"
:example
"")
(define
"tao.sys:data-type"
(subr nil)
:documentation
"形式 : sys:data-type object
object のデータタイプを返す。"
:example
"(sys:data-type \"qwe\") -> sys:str
(sys:data-type 23) -> sys:shortnum
(sys:data-type '(1 2 3)) -> sys:cell")
(define
"day-of-week-string"
(expr (number)
(case number
(0 "Sun")
(1 "Mon")
(2 "Tue")
(3 "Wed")
(4 "Thu")
(5 "Fri")
(6 "Sut")
(otherwise nil)))
:documentation
"形式 : day-of-week-string number
number に対応する曜日名を文字列で返す。number が 0〜6 以外の時は、nil
を返す。"
:example
"(day-of-week-string 0) -> \"Sun\"
(day-of-week-string 1) -> \"Mon\"
(day-of-week-string 2) -> \"Tue\"")
(define
"dbit-off"
(subr nil)
:documentation
"形式 : dbit-off bit-array &rest position1 position2 ... positionN
bit-array (locative データ型か、64 ビット以内で表現できる数値) の
ビット位置 position1 position2 ... positionN のビットをクリアし、
その結果を返す。bit-array は破壊される。bit-off の破壊版。"
:example
"(!a #177) -> #177
(bit-off a 3 5) -> #127
a -> #127
(dbit-off a 2) -> #123
a -> #123")
(define
"dbit-on"
(subr nil)
:documentation
"形式 : dbit-on bit-array &rest position1 position2 ... positionN
bit-array (locative データ型か、64 ビット以内で表現できる数値)の
ビット位置 position1 position2 ... positionN のビットをセツトし、
その結果を返す。bit-array の内容は破壊される。bit-on の破壊版。"
:example
"(!x #123) -> #123
(bit-on x 2) -> #127
x -> #127
(dbit-on x 3 5) -> #177
x -> #177")
(define
"dbp"
(expr nil)
:documentation
"形式 : dpb newbyte bytespec integer
integer の bytespec が指定するバイトを newbyte の bytespec が指定
するバイトで置き換え、その結果を返す。"
:example
"(dbp 1 (byte 1 2) 3) -> #7
(dbp 3 (byte 4 6) 10) -> #312")
(define
"dcu-terminal"
(class T)
:documentation
"インスタンスが dcu ターミナルであるクラス。"
:example
"")
(define
"de"
(macro (fn var-list &body body)
;; evalは、null lexical environmentにするために利用
(let ((result (gensym)))
`(macrolet ((tao:exit (&optional ,result)
`(return-from ,',fn ,,result)))
(defun ,fn ,(canonicalize-lambda-list-keyword var-list)
,@body))))
:documentation
"形式 : de 'fn 'var-list &rest 'body
fn が関数名、var-list が引数リストである expr 型関数、すなわち
スコープ限定型関数を body で定義。"
:example
"(de fact (n)
(cond ((n = 0) 1)
(t (n * (fact (n - 1)))) )) -> fact
fact は、階乗の計算をする関数。
(de cell-count (x)
(cond ((consp x)
(+ 1 (cell-count (car x)) (cell-count (cdr x))))
(t 0) )) -> cell-count")
(define
"debug"
(expr nil)
:documentation
"形式 : debug &opt prompt init-str
デバッガを呼ぶ。デバッガが走っている間、トップレベルと同じようにして
ユーザはスタックの情報(デバッガ呼びだしまで走っていた関数の状態につい
ての情報)を得ることができ、あらゆる関数を実行させることができる。
:ok または :exit をフォームとともにタイプすると、デバッガを終了でき、
デバッガはフォームの評価結果の値を返す。
プロンプトのシンボル > の数は、デバッガがネストされている深さを表わす。
例えば、debug>>>> は、現在走っているデバッガが 4 重にネストされている
ことを示す。デバッガのプロンプトで、種々のコマンドを実行できる。
それらは、 :bt, :btf, :btff, :fn, :def, :body, :form, :varp,
:specialp, :ok, :exit 等。それぞれの機能を以下に示す。
:bt, :btf, :btff ---- バックトレースの情報が得られる。:bt, :btf, :btff
の順により詳細な情報となる。
:fn ----------------- デバッガを呼んだ関数の名前が得られる。
:def ---------------- デバッガを呼んだ関数の定義が得られる。
:body --------------- デバッガを呼んだ関数の本体が得られる。
:form --------------- デバッガを呼んでいるフォームがプリントされる。
:varp --------------- :varp var-name とすると var-name が変数かどうか
チェックされる。
:specialp ----------- :specialp var-name とすると var-name がスペシャル
変数かどうかチェックされる。
:debug-on ----------- エラーが起こるたびにデバッガが呼ばれる。
このデバッガ呼びだしのプロンプトは error> 。
:debug-off ---------- エラーが起こってもデバッガは呼ばれない。"
:example
"")
(define
"dec"
(macro (var &optional (val 1))
`(setf ,var (- ,var ,val)))
:documentation
"形式 : dec var &opt val
var の値から val の値を引き、その結果を返す。 val の既定値は 1 。
(!!- !x n) と同じ。"
:example
"(!x 10)
(dec x) -> 9
x -> 9
(dec x -2) -> 11
x -> 11
(dec 3 2) -> エラー
(dec 3) -> エラー")
(define
"decf"
(cl-macro decf)
:documentation
"形式 : decf place &opt delta
place の値から delta の値を引き、その結果を返すとともに、 place に
代入する。 delta の既定値は 1 。機能は dec と同じ。"
:example
"(!n 0)
(decf n 3) -> -3 そして n=-3
(decf n -5) -> 2 そして n=2
(decf n) -> 1 そして n=1")
#|(define
"declare"
(macro nil)
:documentation
"形式 : declare &rest 'decl-spec
プログラム内の特定の部分だけに動的束縛を発生させる。
decl-spec で指定される宣言子は以下のようなものがある。
(special 変数1 ... 変数n) 変数1〜変数n のスペシャル宣言
(type データ型 変数1 ... 変数n) 変数1〜変数n の型宣言
これは (データ型 変数1 変数n) と簡略化できる
(ftype データ型 関数名1 ... 関数名n)
関数名1〜関数名n が名前の関数型宣言
データ型は次の形式
(function (第1引数の型 ... 第m引数の型) 返す値の型)
または
(function 関数名 (第1引数の型 ... 第m引数の型)
(values 第1返値の型 ... 第n返値の型))
(function 関数名 (第1引数の型 ... 第m引数の型)
第1返値の型 ... 第n返値の型)
関数名が名前の関数の引数と返す値の型宣言
(ignore 変数1 ... 変数n)
変数1〜変数n が決して参照されないことを宣言
(inline 関数名1 ... 関数名n)
関数名1〜関数名n が名前の関数を、可能なら、コンパイル時に
インライン展開することを宣言
(notinline 関数名1 ... 関数名n)
関数名1〜関数名n が名前の関数をコンパイル時にインライン展開しては
ならないことを宣言
(optimize 指定1 ... 指定n) コンパイル時の最適化指定
指定i は次の形式
(speed レベル) 実行効率の最適化レベルの指定
(space レベル) メモリ効率の最適化レベルの指定
(safety レベル) 実行時エラーチェックのレベル指定
(compilation-speed レベル) コンパイル時間の最適化レベルの指定
レベルは 0 〜 3 の整数
(declaration 記号1 ... 記号n)
記号i で始まるリストを宣言子として使用することを宣言"
:example
"x を以下のように定義すると、この定義の中に限って
x がスペシャル変数になる。
(de foo (x)
(declare (special x))
(h (1+ x)))
従って、関数 h には大域変数の x の値に 1 を加えた値が送られる。
(de h (x)
(cons (symbol-value 'x) x))
(foo 1) -> (1 . 2)")|#
(define
"decnum"
(expr (number) number)
:documentation
"形式 : decnum number
number を 10 進数に変換し、それを返す。"
:example
"(decnum #x10) -> 16
(decnum #o10) -> 8")
(define
"decode-float"
(expr (number)
(let ((fr (float-radix number)))
(values (/ number (expt 2 fr))
(float-radix number)
(float-sign number))))
:documentation
"形式 : decode-float number
浮動小数点数 number の値に対応する、以下の 3 つの値を返す。
(1) 小数部を表す新しい浮動小数点数
(2) 指数部を表す整数
(3) 符号を表す同一形式の浮動小数点数"
:example
"(decode-float 3.278) -> !(0.8195 2 1.0)
(decode-float 3.0) -> !(0.75 2 1.0)
(decode-float -0.239) -> !(0.956001 -2 -1.0)")
(define
"decode-universal-time"
#'decode-universal-time
:documentation
"形式 : decode-universal-time universal-time &opt time-zone
ユニバーサルタイム形式の時刻 universal-time を、デコーデッドタイム
形式の時刻に変更する。
リターン値は、秒、分、時、日、月、年、週日、夏時間かどうか、タイム
ゾーンの 9 つの値。time-zone の既定値は、現在のタイムゾーン。"
:example
"(decode-universal-time (get-universal-time)) -> 21
(multiple-value-list (decode-universal-time
(get-universal-time))) ->
(15 38 12 4 7 1985 4 nil -9)
(decode-universal-time 0) -> !(0 0 -9 1 1 1900 1 nil -9)")
(define
"defclass"
(cl-macro defclass)
:documentation
"形式 : defclass 'class-name 'class-vars 'inst-vars
&opt 'supers &rest 'options
クラスベクタと呼ばれるベクタを作成し、class-name で指定された識別子
の属性リストに格納する。
class-name が、この作成されたクラスの名前を表し、作成したクラスの名前を
返す。
class-vars はクラス変数を宣言する部分であり、その要素が cv のよう
な識別子または (cv cvini) のようなリストであるリストとして宣言する。
class-vars の要素が例えば (cv cvini) というリストである場合、car 部で
ある cv はクラス変数を宣言し、cdr 部である cvini はクラス変数cv の初期
値となる。この cvini 式はそのクラスが定義される時に各々一回評価される。
inst-vars はインスタンス変数を宣言する部分で、宣言の仕方はクラス変数
の場合と同様である。 inst-vars においても、 class-vars と同様に、インス
タンス変数の初期値設定ができる。
supers は、作成されるクラスのスーパクラス名である識別子から成るリストで
ある。 supers の既定値は nil 。
options で種々のオプションを指定する。もし、そのオプションの 1 つ
:no-vanilla-class が options として与えられなければ、vanilla-class が作
成されるクラスのスーパクラスになる。
クラスはスーパクラスやサブクラスで示されるように階層構成をとる。
クラスの作成はこの階層構成の上にあるクラスから順に行なう方が望ましい。
ただし、関連のあるクラスがすべて作成された後に、それらのクラスの
インスタンスの最初の 1 つが関数 make-instance によって作られる場合には、
この関数の適用の順序は階層構成の順である必要はない。
クラスの構造を示すクラスベクタはそのジェネレーションによって管理される。
つまり、あるクラスが同じクラス名によって再定義された場合、このクラスの
バージョンナンバーが 1 増して、このクラスのジェネレーションが変わって
いることを示す。さらにこのクラスのサブクラスのバージョンナンバーも
すべて最新のものになる。
クラスは、そのすべてのスーパクラスの全インスタンス変数と全メソッドを
継承するが、クラス変数は継承しない。"
:example
"(defclass a1 () ((b11 1) (b12 2)) () :gettable) -> a1
(defclass a2 () ((b21 3) (b22 4)) (a1) :gettable) -> a2
(defclass a3 () ((b31 5) (b32 6)) (a2) :gettable) -> a3
(!aa (make-instance 'a3)) -> {udo}40766a3
[aa b11] -> 1
[aa b22] -> 4")
(define
"defclass-method"
(expr nil)
:documentation
"形式 : defclass-method 'class-message-pair 'args &rest 'body
class-message-pair は (class-name class-message-pattern) の形式。
クラス class-name に、クラスメッセージ名 class-message-pattern で呼び
出されるクラスメソッドを定義し、そのクラスメッセージ名を返す。
クラスメッセージは、関数 send-class-message によって、class-name へ
送られる。
クラスメソッドのボディ body の中では、関数 cvar によってクラス変数に
アクセス可能。"
:example
"(defclass abc ((a 1)) ()) -> abc
(defclass-method (abc init) (x) (!(cvar a) x)) -> init
(send-class-message abc init 0) -> 0")
(define
"defconstant"
(cl-macro defconstant)
:documentation
"形式 : defconstant symbol init-value &opt doc
symbol をグローバル定数として宣言し、 init-value (省略時は nil) を
初期値とする。 doc はドキュメンテーション文字列。
symbol に代入することはできない。"
:example
"(!aaa 0) -> 0
(defconstant aaa 1) -> aaa
aaa -> 1
(defconstant aaa (- 5 1)) -> aaa
aaa -> 4
(!aaa 0) -> エラー")
(define
"defglobal"
(cl-macro #+sbcl sb-ext:defglobal
#+lispworks system:defvar-global
#+allegro cl:defvar
#+ccl ccl::defglobal)
:documentation
"形式 : defglobal symbol &opt init-val doc
symbol をグローバル変数として宣言し、init-val (省略時は nil)を初期値と
する。doc はドキュメンテーション文字列。"
:example
"(defglobal aa) -> aa
aa -> nil
(defglobal bb 10) -> bb
bb -> 10
(defglobal cc \"fight\") -> cc
cc -> \"fight\"")
(define
"define"
(macro (symbol &body body)
(let ((applobj (car body)))
`(progn
,(typecase applobj
((cons (member lambda function) *)
`(progn
(declaim (ftype function ,symbol))
(setf (symbol-function ',symbol)
,applobj)))
((cons (member tao:Hclauses tao:&+ tao:&+dyn) *)
`(tao.logic::define-logic ,symbol ,applobj))))))
:documentation
"形式 : define symbol applobj
symbol を関数オブジェクト applobj に結び付ける。"
:example
"(define fn (lambda (x y) (list x y)))
= (dye fn (x y) (list x y))
(define fn (expr (x y) (list x y)))
= (de fn (x y) (list x y))
(define aa (array 10)) -> aa")
(define
"define-modify-macro"
(cl-macro define-modify-macro)
:documentation
"形式 : define-modify-macro &rest name lambda-list f-name doc
指定された方法で変数の値を変更する関数を生成したいとき、使用。
de や defun 等の関数定義関数によって生成できず、マクロを用いること
によってのみ定義される。
(define-modify-macro name (arg1 arg2 ...) func doc)
= (defmacro name (x arg1 arg2 ...)
doc
(setf ,x (func ,arg1 ,arg2 ...)))
doc は省略可。"
:example
"(define-modify-macro (counter (up-by) +) -> counter
(!count 0) -> 0
(counter count 2) -> 1
count -> 2
(counter count 3) -> 5
count -> 5
以下のように defmacro 関数を使って同様な関数が定義できる。
(defmacro calc (n delta) `(setf ,n (+ ,n ,delta))) -> calc
(calc count 4) -> 9
(calc count -3) -> 6")
(define
"define-setf-method"
(cl-macro cl:define-setf-expander)
:documentation
"形式 : define-setf-method &rest x
関数 setf が指定された関数式に対してどう働くかを定義する。
次の 2 つのどちらかの方法で用いる。
(1)
(defsetf access-fn update-fn)
access-fn は setf が新しい値を格納する場所を供給する関数。
update-fn は 場所内に新しい値を格納する関数。
つまり、(setf (access-fn argument) new-value) は (access-fn argument)
が供給する場所に新しい値を格納する。
(2)
(define-setf-method access-fn (arg1 arg2 ... ) forms)
(access-fn arg1 arg2 ... ) は新しい値が格納される場所を供給する。
一時的な変数である var は place に格納されるべき新しい値を持つ。
form は (access-fn arg1 arg2 ... ) によって指定された場所に新しい値を
格納するのに使用。
両方法共に access-fn は関数、またはマクロの名前でなくてはならない。
get-setf-method、defsetf を参照。
(define-setf-method access-fn (arg1 arg2 ... ) forms)
は以下の点を除いて関数 defsetf と同じ。
forms が評価されている間は arg1 arg2 ... は一時的な変数に束縛されない。
access-fn は関数とマクロどちらの名前も必要としない。
返される値は 5 つの値のリストである。"
:example
"")
(define
"definition"
(expr nil)
:documentation
"形式 : definition symbol
symbol の種類に従って以下の情報を返す。
+------------------+------------------------------------+
| symbol の種類 | 返却値 |
+------------------+------------------------------------+
| 値をもつ変数 | (!symbol 'value) |
+------------------+------------------------------------+
| | (de symbol ...) |
| | (defun symbol ...) |
| 関数 | (defrel symbol ...) |
| | (defmacro symbol ... ) |
| | (defsubst symbol ... ) |
+------------------+------------------------------------+
| クロージャ | (!symbol (closure ... )) |
+------------------+------------------------------------+
| クラス | (defclass symbol ...) |
+------------------+------------------------------------+
| 配列 | the same information as array-info |
+------------------+------------------------------------+"
:example
"(de add1 (x)
(1+ x) ) -> add1
(definition add1) -> (de add1 (x)
(1+ x) )
((definition *print-base*) -> (!*print-base* 10.)")
(defmacro tao:deflogic-macro (name (&rest args) &body body)
`(progn
(setf (get ',name :logic-macro) t)
(defmacro ,name (,@args) ,@body)))
(defun remove-logical-methods (gfname class)
(dolist (mc '((tao:assert :first)
(tao:assert :last)
(tao:assert :cut)))
(let* ((gf (fdefinition gfname))
(m (find-method gf mc (cons (find-class class)
(mapcar (constantly (find-class T))
(cdr (c2cl:generic-function-lambda-list gf))))
nil)))
(when m (remove-method gf m)))))
(define
"deflogic-method"
(macro ((class &rest message) (&rest args) &body body)
(let ((slot-names (mapcar #'c2mop:slot-definition-name (c2mop:class-slots (find-class class)))))
(ecase (length message)
(1 (let* ((message (car message))
(pred (tao.logic::make-predicate message (1+ (length args)))))
`(progn
(unless (fboundp ',pred)
(defgeneric ,pred (self cont)
(:method-combination tao:assert)))
(remove-logical-methods ',pred ',class)
(defmethod ,pred tao:assert :first
((tao:self ,class) ,@args cont)
(with-slots (,@slot-names)
tao:self
(declare (ignorable ,@slot-names))
,(tao.logic::compile-body body
'(lambda ()
(if (next-method-p)
(call-next-method)
T))
tao.logic::no-bindings)))
(define-method-predicate-in-lisp-world ,message))))
(2 (destructuring-bind (qualifier message)
message
(let ((pred (tao.logic::make-predicate message (1+ (length args)))))
`(progn
(unless (fboundp ',pred)
(defgeneric ,pred (self cont)
(:method-combination tao:assert)))
(remove-logical-methods ',pred ',class)
(defmethod ,pred tao:assert ,qualifier
((tao:self ,class) ,@args cont)
(with-slots (,@slot-names)
tao:self
(declare (ignorable ,@slot-names))
,(tao.logic::compile-body body
'(lambda ()
(if (next-method-p)
(call-next-method)
T))
tao.logic::no-bindings)))
(define-method-predicate-in-lisp-world ,message))))))))
:documentation
"形式 : deflogic-method 'method-spec 'arg-pattern-list &rest 'forms
論理メソッドを定義する。defmethod と同じように働く。defmethod を参照。
method-spec = (class-name method-type selector)
arg-pattern-list はヘッド部分に対応する。 forms はボディ部分に対応する。
forms には (&aux x y z) のような補助変数宣言がくることがある。論理メソ
ッドでは self を使用可能。"
:example
"(defclass aclass () ()) -> aclass
(deflogic-method (aclass amethod) (_x _y) (cons _x _y))
-> amethod
(deflogic-method (aclass amethod) (_x _y _z) [_x + _y + _z])
-> amethod
(deflogic-method (aclass amethod) ((_p . _)) _p) -> amethod
(!x (make-instance 'aclass)) -> {udo}76445aclass
[x amethod p (q r s)] -> (p q r s)
[x amethod 1 2 3] -> 6
[x amethod (aa bb cc)] -> aa
[x amethod aa bb cc dd] -> nil")
(define
"defmacro"
(cl-macro defmacro)
:documentation
"形式 : defmacro 'fn 'var-list &rest 'body
fn が名前、var-list が引数リストのマクロ関数を body で定義する。"
:example
"(defmacro first (x) (list 'car x)) -> first")
(defmacro with-c&ivars ((&rest cvars) (&rest ivars) &body body)
`(macrolet ((tao:cvar (name)
(check-type name (member ,@cvars))
`(slot-value tao:self ',name)))
(with-slots (,@ivars) tao:self
,@body)))
(defun make-super-form (msg args)
(let* ((pkg (symbol-package msg))
(msg (string msg))
(sep (position #\. msg))
(class (intern (subseq msg 0 sep) pkg))
(gf (intern (subseq msg (1+ sep)) pkg)))
#+lispworks
`(let ((args (list ,@args)))
(declare (dynamic-extent args))
(apply (clos:compute-effective-method-function-from-classes
#',gf
(list (find-class ',class)))
tao:self
args))))
(defmacro with-super (args &body body)
`(macrolet ((tao:super (msg &rest args)
(make-super-form msg ,args)))
,@body))
(define
"defmethod"
(macro (method-spec (&rest arglist) &body body)
(let (class-name method-type message-patern)
(ecase (length method-spec)
(2 (setf (values class-name message-patern)
(values-list method-spec)))
(3 (setf (values class-name method-type message-patern)
(values-list method-spec))))
(let* ((slots (c2mop:class-slots (find-class class-name)))
(cvars (mapcar #'c2mop:slot-definition-name
(remove-if-not (lambda (v)
(eq :class (c2mop:slot-definition-allocation v)))
slots)))
(ivars (mapcar #'c2mop:slot-definition-name
(remove-if-not (lambda (v)
(eq :instance (c2mop:slot-definition-allocation v)))
slots))))
(etypecase message-patern
(symbol
`(let (#+lispworks (lw:*handle-warn-on-redefinition* ,(not (keywordp message-patern))))
(defmethod ,message-patern ,@(and method-type (list method-type)) ((tao:self ,class-name) ,@arglist)
(with-c&ivars (,@cvars) (,@ivars)
(declare (ignorable ,@ivars))
(with-super ,arglist
,@body)))))
(cons
`(progn
(defmethod list-message ,@(and method-type (list method-type))
((tao:self ,class-name)
(_ (eql ,(sxhash message-patern)))
,@arglist)
(declare (ignore _))
(with-c&ivars (,@cvars) (,@ivars)
(declare (ignorable ,@ivars))
(with-super ,arglist
,@body)))))))))
:documentation
"形式 : defmethod 'method-spec 'arg-list &rest 'forms
method-spec は (class-name message-pattern) または、
(class-name method-type message-patern) という形式。
method-type は :before, :primary, :after, :or, :and, :list,
:inverse-list, :nconc, :progn のいずれかで、メソッド結合の時に使われる。
クラス class-name に、メッセージ名 message-pattern で呼び出される
メソッドを定義し、そのメッセージ名を返す。 message-pattern は、
メッセージを送るときに使われ、メソッド名とも呼ばれる。
メソッドには id-method と list-method の 2 種類がある。
id-method を定義するとき、この id-method で使われる引数は、 arg-list の
部分にリストにして書く。このリスト中にはキーワード &aux を書ける。
forms でメソッドのボディが指定される。
メソッドが呼び出されると、このボディ中の式が一つずつ評価される。
list-method が定義されると、arg-list は、forms の一部分とみなされる。
list-massage は id-message と同じ役割を果たすことができる。
しかし list-message は id-message と違って message-pattern をユニフィケ
ーションのパターンとして使うことができる。しかし list-message では送ら
れるメッセージ名と一致するメソッド名をもつメソッドが起動されるが、
list-method では送られるメッセージ名とユニファイできるメソッド名をもつ
メソッドが起動される。それゆえ list-message の message-pattern は論理
変数をその一部分として含むこともある。
メソッドのボディ (forms) の中では、クラス変数は関数 cvar を使うことによ
りアクセスされる。
スーパクラスのメソッドは、関数 super によって実行される。
クラスはメソッド結合で指定された方法で、全てのスーパクラスの、全ての
メソッドを継承する。"
:example
"(defclass a1 () (b1) () :gettable :settable) -> a1
(defclass a2 () (b2) (a1) :gettable :settable) -> a2
(defclass a3 () (b3) (a2) :gettable :settable) -> a3
(defmethod (a1 mult) () (!!* !b1 b2)) -> mult
(defmethod (a3 (which is larger))
(cond ([b1 > b2] 'b1) (t 'b2)) ) -> (which is larger)
(!cc (make-instance 'a3 b1 10 b2 20)) -> {udo}44994a3
[cc b1] -> 10
[cc b2] -> 20
[cc mult] -> 200
[cc b1] -> 200
[cc b2] -> 20
[cc (which is larger)] -> b1
(goal (== _x ,(cc (which is _y))))
_y = larger
_x = b1 ;
no")
(define
"defparameter"
(cl-macro defparameter)
:documentation
"形式 : defparameter symbol init-value &opt doc
symbol をグローバル変数として宣言し、init-value を初期値とする。
関数 defvar では初期値が与えられる前に値が代入されていたら初期値は無視
されるが、この関数では初期値が優先 (代入)。doc はドキュメンテーション
文字列。"
:example
"(!aa 0) -> 0
(defparameter aa 1) -> aa
aa -> 1
(defparameter aa (- 4 1)) -> aa
aa -> 3")
(define
"defprop"
(macro (p-list val ind)
`(tao:putprop ',p-list ',val ',ind))
:documentation
"形式 : defprop p-list val ind
属性リスト p-list において ind と eq な最初のインディケータに対応し
た属性値を val に置き換え ind を返す。 eq なインディケータがない場合
は、 p-list に ind と val のペアをコンスし ind を返す。
p-list は破壊される。
引数が評価されないこと以外は putprop と同じ。
(defprop foo bar next-to) = (putprop 'foo 'bar 'next-to)
連想リストのかわりに属性リストを扱う点を除いて、putalist と同じ。"
:example
"(!(plist 'xxx) '(q 2 r 3 s 4)) -> (q 2 r 3 s 4) そして
xxx の属性リストは、 (q 2 r 3 s 4)
(!yyy (plist 'xxx)) -> (q 2 r 3 s 4) そして
(defprop xxx 1 p) -> 1 そして
(plist 'xxx) -> (p 1 q 2 r 3 s 4) しかし
yyy = (q 2 r 3 s 4)
(defprop xxx 5 s) -> 5
(plist 'xxx) -> (p 1 q 2 r 3 s 5) そして
ここで yyy = (q 2 r 3 s 5)")
(define
"defrel"
(macro (name &body clauses)
`(progn
(tao:abolish ,name ,(length (caar clauses)))
(define-predicate-in-lisp-world ,name)
,@(mapcar (lambda (cl)
(list 'tao.logic::add-clause
(list 'quote
(cons
(cons name (car cl))
;(if (null (cdr cl)) nil (list (cons 'tao:&and (cdr cl))))
(list (cons 'tao:&and (cdr cl)))
;(cdr cl)
))
:asserta nil))
clauses)
(tao.logic::prolog-compile ',name)
',name))
:documentation
"形式 : defrel p ((A1\") (P11 B11\") (P12 B12\") ... (P1m B1m\"))
((A2\") (P21 B21\") (P22 B22\") ... (P2m B2m\"))
...
((An\") (Pn1 Bn1\") (Pn2 Bn2\") ... (Pnm Bnm\"))
主ファンクタが p である n 個のホーン節を定義する。
assert を n 回実行するのと同じ。
Pij は主ファンクタ、Aij\" と Bij\" はそれぞれ Aij と Bij から得たもの。
DEC10-Prolog では次のように記述される。
p(A1\") :- P11(B11\"), P12(B12\"), ... (P1m(B1m\").
p(A2\") :- P21(B21\"), P22(B22\"), ... (P2m(B2m\").
....
p(An\") :- Pn1(B1n\"), Pn2(Bn2\"), ... (Pnm(Bnm\")."
:example
"(defrel concat ((() _x _x))
((( _a . _x) _y ( _a . _z)) (concat _x _y _z)))
-> concat
(goal (concat _x _y (1 2))) ->
_x = ()
_y = (1 2);
_x = (1)
_y = (2);
_x = (1 2)
_y = ();
_x = ()
no
(defrel p ((A1\") (P11 B11\") (P12 B12\")...(P1m B1m\"))
((A2\") (P21 B21\") (P22 B22\")...(P2m B2m\"))
....
((An\") (Pn1 Bn1\") (Pn2 Bn2\")...(Pnm Bnm\")))
=
(define p (hclauses (&+ (A1\") (P11 B11\") (P12 B12\")
...(P1m B1m\"))
(&+ (A2\") (P21 B21\") (P22 B22\")
...(P2m B2m\"))
....
(&+ (An\") (Pn1 Bn1\") (Pn2 Bn2\")
...(Pnm Bnm\")))")
(define
"defsetf"
(cl-macro defsetf)
:documentation
"形式 : defsetf &rest x
関数 setf が指定された関数式に対してどう働くかを定義する。
次の 2 つのどちらかの方法で用いる。
(1)
(defsetf access-fn update-fn)
access-fn は setf が新しい値を格納する場所を供給する関数。
update-fn は 場所内に新しい値を格納する関数。
つまり、(setf (access-fn argument) new-value)
は (access-fn argument) が供給する場所に新しい値を格納する。
(2)
(defsetf access-fn (arg1 arg2 ... ) (var) form)
(access-fn arg1 arg2 ... ) は新しい値が格納される場所を供給する。
一時的な変数である var は place に格納されるべき新しい値を持つ。
formは (access-fn arg1 arg2 ... ) によって指定された場所に新しい値を
格納するのに使用。
両方法共に access-fn は関数、またはマクロの名前でなくてはならない。"
:example
"(defsetf symbol-value-set) ->
{applobj}33390(#!subr-simpl . 6)
(defsetf substring (str a &opt z) (new-var)
`(seq (replace ,str ,new-var :string1 ,a :end1 ,z)
,new-var))
-> ((#:g1 #:g2 #:g3) (#:g4)
(replace #:g1 #:g4 :star1 #:g2 :end1 #:g3) #:g4)
上記の 2 番目の例の返される値について get-setf-method を参照。")
(define
"defstruct"
(cl-macro defstruct)
:documentation
"形式 : defstruct 'name-opts &rest 'slots
レコード構造のデータ型を定義する関数。一般的には、次の様に呼び出す。
(defstruct (symbol option-1 option-2 ... )
doc-string
slot-description-1 slot-description-2 ... )
symbol が構造の全具体例を構成する新しいデータ型の名前となる。
symbol がリターン値。
slot-description-1 slot-description-2 ... は、それぞれ次のような形を
している。
(slot-name default-init
slot-option-name-1 slot-option-value-1
slot-option-name-2 slot-option-value-2
...)
slot-name は、シンボル。
default-init は、構造が作られる度に評価される形式で、その値はスロット
の初期値として用いられる。
構造を定義するフォームの評価は以下のことを含んでいる。
・レコードの実体をアクセスするためのアクセス関数が、それぞれのスロット
に対して定義される。
・レコードの実体を生成するコンストラクタ関数が定義される。
・name は、関数 typep で受け入れ可能なものとなる。
・#s 構文が、構造の具体例を読むために用いることができる。
・オブジェクトが与えられた時に、その与えられたもののコピーである新しい
オブジェクトを生成するコピー関数が定義される。
・関数 setf を用いて、それぞれのスロットの実体を変更することができる。
・name により name-p という 1 引数の関数が定義される。"
:example
"(defstruct date day month year) -> date
コンストラクタ関数 make-date 、
アクセス関数 date-day, date-month, date-year が用意される。
(setq today (make-date :day 1 :month 6 :year 1987)
-> #S(date day 1 monyh 6 year 1987)
(date-day today) -> 1
(date-month today) -> 6
(setf (date-day today) 3) -> 3
(date-day today) -> 3
(typep today date) -> t
(typep tommorow date) -> nil")
(define
"deftype"
(cl-macro deftype)
:documentation
"形式 : deftype symbol lambda-list &rest body
body で新しい型指定子を定義し、それを symbol に結び付ける。
lambda-list は新しいデータタイプが必要とする引数のリスト。
マクロと同様に動作する。lambda-list 中のオプショナル変数の初期値は * 。"
:example
"(deftype modulus (n) `(integer 0 (,n))) -> modulus
(deftype str-cha () '(or character string)) -> str-cha
str-cha は文字またはストリングを表す新しいデータタイプ。
(square-matrix number 10) は (array number 10 10) と、型指定子
に関して同等。")
(define
"defun"
(cl-macro defun)
:documentation
"形式 : defun 'fn 'var-list &rest 'body
fn を名前、var-list を引数リストとする大域関数を body で定義する。
var-list にはキーワード引数 &optional, &rest, &key, &aux を含むことが
できる。"
:example
"(defun fact (n)
(cond ((n = 0) 1)
(t (n * (fact (n - 1)))) )) -> fact
(defun tatoeba (x y &optional p q &aux m)
(!m (index (!!or !p 0) (!!or !q 9)))
(image i m (+ x (y / i)))) -> tatoeba")
(define
"defvar"
(cl-macro defvar)
:documentation
"形式 : defvar symbol &opt init-value doc
symbol をスペシャル変数として定義し、init-value (省略時 nil)を初期値と
するが、グローバル値を持っているならは init-value を評価せずグローバル
値をも再代入しない。doc はドキュメンテーション文字列。"
:example
"(defvar aa 10) -> aa
aa -> 10
(defglobal bb 12) -> bb
bb -> 12
(defvar bb 100) -> bb
bb -> 12")
(define
"del"
(subr (pred item list &optional n)
(declare (list list))
(delete-if (lambda (x) (funcall pred item x))
list :count n))
:documentation
"形式 : del pred item list &opt n
list の要素を順に item とともに述語 pred に適用し、その述語を満足する
要素を n 個取り除く。 pred は引数を 2 つとる関数で、第 1 引数は item 、
第 2 引数は list の各要素。n が、省略されたり pred を満足する要素の個数
以上の数の場合、あるいは負の場合は、pred を満足するすべての要素を
取り除く。list は破壊される。rem の破壊版。"
:example
"x を (1 2 3 4 5 6 7) とする
(del #'< 4 x) -> (1 2 3 4)
(del #'< 4 x 2) -> ( 1 2 3 4 7)")