-
Notifications
You must be signed in to change notification settings - Fork 6
/
direct-sums.lisp
169 lines (137 loc) · 6.31 KB
/
direct-sums.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
;;; -*- Mode:Lisp; Package:Weyli; Base:10; Lowercase:T; Syntax:Common-Lisp -*-
;;; ===========================================================================
;;; Direct Sums
;;; ===========================================================================
;;; (c) Copyright 1991,1993 Cornell University
;;; direct-sums.lisp,v 1.4 1995/05/24 17:41:59 rz Exp
(in-package :weyli)
;;; DELETE (make::adjust-version-numbers Weyl "1.4")
(defmethod dimension-of ((domain direct-sum))
(length (tuple-value domain)))
(defmethod initialize-instance :after ((domain direct-sum) &rest plist)
(declare (ignore plist))
(with-slots (print-function) domain
(setf print-function 'direct-sum-print-object)))
(defun direct-sum-print-object (domain stream)
(%apply #'format stream "~S~@{ (+) ~S~}"
(loop with v = (tuple-value domain)
for i below (array-dimension v 0)
collect (aref v i))))
(defgeneric %make-direct-sum (domain1 domain2)
(:documentation
"The purpose of this method is unknown."))
(defmacro define-direct-sum (domain-name classes
&optional other-domain-classes other-elt-classes)
(let ((ds-domain (intern (format nil "DIRECT-SUM-~A" domain-name)))
(ds-domain-elt (intern (format nil "DIRECT-SUM-~A-ELT" domain-name))))
`(progn
(defclass ,ds-domain
(,@(loop for name in classes
collect (intern (format nil "DIRECT-SUM-~A" name)))
,domain-name ,@other-domain-classes direct-sum) ())
(defclass ,ds-domain-elt
(,@(loop for name in classes
collect (intern (format nil "DIRECT-SUM-~A-ELT" name)))
,@other-elt-classes direct-sum-element) ())
(define-domain-element-classes ,ds-domain ,ds-domain-elt)
(defmethod %make-direct-sum ((a ,domain-name) (b ,domain-name))
(%make-direct-sum-internal ',ds-domain a b))
(defmethod make-element ((domain ,ds-domain) elt1 &rest elts)
(%apply #'make-instance ',ds-domain-elt
:domain domain
:values (cons elt1 elts)))
(defmethod weyl::make-element ((domain ,ds-domain) elt1 &rest elts)
(let* ((domains (tuple-value domain))
(len (array-dimension domains 0)))
(unless (cl:= (1- len) (length elts))
(error "Incorrect number of elements to MAKE-ELMENT ~A" domain))
(make-instance ',ds-domain-elt
:domain domain
:values (cons (coerce elt1 (aref domains 0))
(loop for i upfrom 1 below len
for elt in elts
collect (coerce elt (aref domains i))))))))))
(defun make-direct-sum* (domain1 &rest domains)
(when (null domains)
(error "Illegal number of arguments to MAKE-DIRECT-SUM"))
(labels ((iterate (values)
(cond ((null (rest values))
(first values))
(t (%make-direct-sum (first values)
(iterate (rest values)))))))
(let ((domain (iterate (cons domain1 domains)))
(Z (get-rational-integers)))
(make-homomorphism Z #'(lambda (x)
(map-with-domain
(first (domain-element-classes domain))
domain
#'(lambda (d) (coerce x d)) domain))
domain)
domain)))
(defun make-direct-sum (domain1 &rest domains)
(add-domain #'false
(%apply #'make-direct-sum* domain1 domains)))
(defun %make-direct-sum-internal (type a b)
(flet ((domain-list (a)
(loop for i below (dimension-of a)
collect (ref a i))))
(cond ((typep a 'direct-sum)
(if (typep b 'direct-sum)
(make-instance type :values (nconc (domain-list a) (domain-list b)))
(make-instance type :values (nconc (domain-list a) (list b)))))
((typep b 'direct-sum)
(make-instance type :values (cons a (domain-list b))))
(t (make-instance type :values (list a b))))))
(defun get-direct-sum (domain1 &rest domains)
(add-domain #'(lambda (d)
(and (typep d 'direct-sum)
(eql domain1 (ref d 0))
(cl:= (1- (dimension-of d)) (length domains))
(loop for i below (dimension-of d)
for dom in domains
when (not (eql (ref d i) dom))
do (return nil)
finally (return t))))
(%apply #'make-direct-sum* domain1 domains)))
(defmethod print-object ((domain direct-sum-element) stream)
(%apply #'format stream "~S~@{ (+) ~S~}"
(loop with v = (tuple-value domain)
for i below (array-dimension v 0)
collect (aref v i))))
(define-direct-sum semigroup ())
(defmethod-sd times ((a direct-sum-semigroup-elt) (b direct-sum-semigroup-elt))
(map-with-domain 'direct-sum-semigroup-elt domain #'times a b))
(define-direct-sum monoid (semigroup))
(defmethod one ((domain direct-sum-monoid))
(map 'direct-sum-monoid-elt #'one domain))
(defmethod 0? ((x direct-sum-monoid-elt))
(let ((v (tuple-value x)))
(loop for i below (array-dimension v 0)
when (not (0? (aref v i)))
do (return nil)
finally (return t))))
(define-direct-sum group (monoid))
(defmethod-sd quotient ((a direct-sum-group-elt) (b direct-sum-group-elt))
(map-with-domain 'direct-sum-semigroup-elt domain #'quotient a b))
(defmethod recip ((a direct-sum-group-elt))
(map-with-domain 'direct-sum-semigroup-elt (domain-of a) #'recip a))
(define-direct-sum abelian-semigroup ())
(defmethod-sd plus ((a direct-sum-semigroup-elt) (b direct-sum-semigroup-elt))
(map-with-domain 'direct-sum-semigroup-elt domain #'plus a b))
(define-direct-sum abelian-monoid (abelian-semigroup))
(defmethod zero ((domain direct-sum-monoid))
(map 'direct-sum-monoid-elt #'zero domain))
(defmethod 1? ((x direct-sum-monoid-elt))
(let ((v (tuple-value x)))
(loop for i below (array-dimension v 0)
when (not (1? (aref v i)))
do (return nil)
finally (return t))))
(define-direct-sum abelian-group (abelian-monoid))
(defmethod-sd difference ((a direct-sum-abelian-group-elt) (b direct-sum-abelian-group-elt))
(map-with-domain 'direct-sum-semigroup-elt domain #'difference a b))
(defmethod minus ((a direct-sum-abelian-group-elt))
(map-with-domain 'direct-sum-semigroup-elt (domain-of a) #'minus a))
(define-direct-sum module (abelian-group) (has-coefficient-domain))
(define-direct-sum algebra (module semigroup))
(define-direct-sum ring (algebra monoid))