File: types.scm

package info (click to toggle)
jacal 1c7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,068 kB
  • sloc: lisp: 6,489; sh: 419; makefile: 315
file content (495 lines) | stat: -rw-r--r-- 17,068 bytes parent folder | download
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
;; JACAL: Symbolic Mathematics System.        -*-scheme-*-
;; Copyright 1989, 1990, 1991, 1992, 1993, 1995, 1997, 2005, 2006 Aubrey Jaffer.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or (at
;; your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

(require 'object->string)
(require 'hash-table)
(require 'rev4-optional-procedures)
(require 'common-list-functions)

;;; Scheme doesn't allow for definition of new types which are
;;; distinct from existing types.  So we will carefully use BUNCH
;;; instead of LIST in order to distinguish the types.
;;; This requires that boolean?, pair?, symbol?, number?,
;;; string?, vector? and procedure? be disjoint as outlined in:
;;; Jonathan Rees and William Clinger, editors. The Revised^3
;;; Report on the algorithmic language Scheme, ACM SIGPLAN Notices
;;; 21(12), ACM, December 1986.
;;; If the types are not disjoint you WILL lose.

;;; The following types are mutually exclusive:
;;; SEXP, VARIABLE, EXPL, IMPL, EQLT, BUNCH
;;; INTEGERs are EXPL
;;; An EXPR is an EXPL or IMPL
;;; A LICIT is an EXPL, IMPL, or EQLT.
;;; VARIBLEs can only occur as part of EXPRS and EQLTS.
;;; SYMBOLs can only occur in SEXP.
;;; BUNCHES can contain SYMBOLs, LICITs, and BUNCHEs.
;;; An EXPL, IMPL, or EQLT, or BUNCH of these can be a
;;; lambda expression.

;;; A VAR is a vector which consists of:
;;; 0 var:sexp		- s-expression	;lambda vars have leading "@"
					;shadow vars have trailing ":"
;;; 1 var:pri		- string	;ordering priority
					;first char is priority override
					;last char is differential order
;;; 2 var:def		- poleq		;ext defining equation
;;;		     or	- integer	;simple lambda position
;;;		     or - procedure	;
;;; 3 var:depends	- list of vars	;vars used in var:def
;;; 4 var:shadow	- var		;shadow copies of this lambda var or #f
;;;;		   THE REST ARE FOR FUNCTIONS ONLY
;;; 5 func-arglist			;list of (function and) argument values.
;;; 6 func-parity	- list of atoms	;EVEN, ODD, 0, or #F
;;; 7 func-syms		- list of lists	;of positions of arguments
;;; 8 func-anti-syms	- list of lists	;of positions of arguments
;;; 9 func-dists	- list of lists	;of functions which distribute
;;; 10 func-anti-dists	- list of lists	;of functions which anti-distribute
;;; 11 func-idems	- list		;of positions of arguments
					; perserved in idempotency

(define make-var vector)
(define poly:var? vector?)
(define (var:sexp v) (vector-ref v 0))
(define (var:pri v) (char->integer (string-ref (vector-ref v 1) 0)))
(define (var:set-pri! v i) (string-set! (vector-ref v 1) 0 (integer->char i)))
(define (var:def v) (vector-ref v 2))
(define (var:set-def! v i) (vector-set! v 2 i) v)
(define (var:depends v) (vector-ref v 3))
(define (var:set-depends! v i) (vector-set! v 3 i) v)

(define (func-arglist f) (vector-ref f 5))
(define (func-set-arglist f i) (vector-set! f 5 i))

(define func? func-arglist)

(define (func-parity f) (vector-ref f 6))
(define (func-set-parity! f v) (vector-set! f 6 v))
(define (func-syms f) (vector-ref f 9))
(define (func-set-syms! f v) (vector-set! f 9 v))
(define (func-anti-syms f) (vector-ref f 10))
(define (func-set-anti-syms! f v) (vector-set! f 10 v))
(define (func-dists f) (vector-ref f 11))
(define (func-set-dists! f v) (vector-set! f 11 v))
(define (func-anti-dists f) (vector-ref f 12))
(define (func-set-anti-dists! f v) (vector-set! f 12 v))
(define (func-idems f) (vector-ref f 13))
(define (func-set-idems! f v) (vector-set! f 13 v))

(define (copy-vector v)
  (define iv (make-vector (vector-length v)))
  (do ((i (+ -1 (vector-length iv)) (+ -1 i)))
      ((negative? i) iv)
    (vector-set! iv i (vector-ref v i))))

;;; The shadowed variables are kept in a list in the var:shadow slot
;;; in the var.  This list has the bumped and shadowed transformations
;;; of the var for each arglist-length less than or equal
;;; (var:max-lambda-position var).

;;; For simple lambda variables (@1, @2, ...) the last shadowed
;;; variable is identical to the original except that its shadow slot
;;; is #f.
(define (var:shadow v arglist-length)
  (if (not (vector-ref v 4))
      (vector-set! v 4 (make-list (var:max-lambda-position v) #f)))
  (let ((shadpair (memshad arglist-length (vector-ref v 4))))
    (if (not (car shadpair))
	(set-car! shadpair
		  (cond ((< arglist-length (var:min-lambda-position v))
			 (var:lambda-bump v (- arglist-length)))
			(else (make-shadow v arglist-length)))))
    (car shadpair)))
(define (memshad arglist-length shadlist)
  (cond ((= 1 arglist-length) shadlist)
	((null? (cdr shadlist)) shadlist)
	(else (memshad (+ -1 arglist-length) (cdr shadlist)))))
(define (make-shadow v arglist-length)
  (let ((nv (copy-vector v)))
    (vector-set! nv 4 #f)
    (vector-set!
     nv 0 (if (symbol? (var:sexp nv))	;got here because can't bump
	      (string->symbol
	       (string-append
		(symbol->string (var:sexp nv)) ":"))
	      (do-sexp-symbols
	       (lambda (s)
		 (define st (symbol->string s))
		 (if (and (char=? #\@ (string-ref st 0))
			  (> (string-length st) 1))
		     (let ((i (string->number
			       (substring st 1 (string-length st)))))
		       (if (> i arglist-length)
			   (var:sexp (lambda-var (- i arglist-length) 0))
			   (string->symbol (string-append st ":"))))
		     s))
	       (var:sexp v))))
    ;; Now avoid priority conflicts with minimum increment.
    (let* ((s (string-copy (vector-ref v 1)))
	   (si (+ -1 (string-length s))))
      (string-set! s si (integer->char
			 (+ 1 (char->integer (string-ref s si)))))
      (vector-set! nv 1 s))
    (var:set-depends!
     nv (map (lambda (var)
	       (if (simple-lambdavar? var) (var:shadow var arglist-length)
		   var))
	     (var:depends v)))
    (if (var:def v)
	(var:set-def! nv (licits:do-vars
			  (lambda (var)
			    (if (lambdavardep? var)
				(if (eq? var v) nv
				    (var:shadow var arglist-length))
				var))
			  (var:def v))))
    (if (>= (vector-length v) 5)
	(func-set-arglist nv (licits:do-vars
			      (lambda (var)
				(if (lambdavardep? var)
				    (var:shadow var arglist-length) var))
			      (func-arglist v))))
    nv))

(define (var:> v2 v1)
  (string>? (vector-ref v2 1) (vector-ref v1 1)))

;(define var-tab '())
;(define var-tab-lookup (predicate->asso equal?))
;(define var-tab-define (alist-associator equal?))
;(define var-tab-for-each alist-for-each)

(define var-tab (make-hash-table 43))
(define var-tab-lookup (predicate->hash-asso equal?))
(define var-tab-define (hash-associator equal?))
(define var-tab-for-each hash-for-each)

(define (list-of-vars)
  (define vars '())
  (var-tab-for-each (lambda (k v) (set! vars (cons v vars))) var-tab)
  vars)

(define (sexp->new-var v)
  (let ((base v)
	(diffs 0)
	(lambda? (and (pair? v) (eq? 'lambda (car v)))))
    (do () ((not (and (pair? base) (eq? 'differential (car base)))))
      (set! base (cadr base))
      (set! diffs (+ 2 diffs)))	     ;leave space for shadow priority.
    (let* ((s (object->string base))
	   (sl (string-length s))
	   (arglist (if (pair? v) (map sexp->math (if lambda? (caddr v) v))
			'())))
      (if lambda?
	  (make-var
	   (caddr v)
	   (string-append (case (string-ref s 0)
			    ((#\@) lambda-var-pri-str)
			    (else lambda-var-pri-str))
			  s
			  (string (integer->char diffs)))
	   (and (char=? #\@ (string-ref s 0))
		(not (= sl 1))
		(not (char=? #\^ (string-ref s 1)))
		(string->number (substring s 1 sl)))
	   (var:build-depends arglist)
	   #f
	   arglist
	   #f #f #f #f #f #f		; function properties
	   )
	  (make-var
	   v
	   (string-append (case (string-ref s 0)
			    ((#\@) lambda-var-pri-str)
			    (else median-pri-str))
			  s
			  (string (integer->char diffs)))
	   (and (char=? #\@ (string-ref s 0))
		(not (= sl 1))
		(not (char=? #\^ (string-ref s 1)))
		(or (string->number (substring s 1 sl))
		    ;; handle trailing ":" in shadow-var
		    (string->number (substring s 1 (- sl 1)))))
	   (var:build-depends arglist)
	   #f
	   arglist
	   )))))

(define (sexp->var sexp)
  (let ((vcell (var-tab-lookup sexp var-tab)))
    (if vcell (cdr vcell)
	(let ((val (sexp->new-var sexp)))
	  (set! var-tab (var-tab-define var-tab sexp val))
	  val))))
(define (string->var s) (sexp->var (string->symbol s)))

(define (var:build-depends args)
  (let ((deps '()))
    (for-each (lambda (e)
		(poly:for-each-var
		 (lambda (v)
		   (cond (($var? v))
			 ((symbol? (var:sexp v)) (set! deps (adjoin v deps)))
			 (else
			  (set! deps
				(adjoin v (union
					   (var:depends v) deps))))))
		 e))
	      args)
    deps))

(define (deferop . args)
  (var->expl (sexp->var (deferedmath->sexp args))))

(define lambda-var-pri (+ -5 char-code-limit))
(define lambda-var-pri-str (string (integer->char lambda-var-pri)))
(define median-pri-str (string (integer->char (quotient char-code-limit 2))))

(define (var->string v)
  (let ((sexp (var:sexp v)))
    (if (symbol? sexp) (symbol->string sexp)
	(math:error 'expected-simple-symbol sexp))))

(define (make-rad-var radicand exponent-reciprocal)
  (let ((e (univ:monomial -1 exponent-reciprocal $)))
    (set-car! (cdr e) radicand)
    (let ((v (defext (sexp->var (list '^ (bunch->sexp radicand #t)
				      (list '/ 1 exponent-reciprocal)))
	       e)))
      (func-set-arglist v (list _^ radicand (make-rat 1 exponent-reciprocal)))
      (set! radical-defs (cons (extrule v) radical-defs))
      v)))

(define (var:nodiffs v)
  (do ((base (vector-ref v 0) (cadr base)))
      ((not (and (pair? base) (eq? 'differential (car base))))
       (if (eq? base (vector-ref v 0)) v (sexp->var base)))))
(define (var:differential? v)
  (not (zero? (var:diff-depth v))))
(define (var:diff-depth v)
  (let ((s (vector-ref v 1)))
    (quotient (char->integer (string-ref s (+ -1 (string-length s)))) 2)))
(define (var:differential v)
  (sexp->var (list 'differential (var:sexp v))))

(define (var:undiff v)
  (sexp->var (cadr (var:sexp v))))

(define (radicalvar? v)
  (let ((ve (var:sexp v)))
    (and (pair? ve) (eq? '^ (car ve)))))
(define (lambdavar? v)
  (= lambda-var-pri (var:pri v)))
(define (simple-lambdavar? v)
  (number? (lambda-position v)))
(define (simple-shadowed-lambdavar? v)
  (and (number? (lambda-position v)) (not (vector-ref v 4))))
(define (lambdavarext? v)
  (< lambda-var-pri (var:pri v) $-pri))
(define (lambdavardep? v)
  (< (+ -1 lambda-var-pri) (var:pri v) $-pri))
(define ($var? v)
  (char=? #\:  (string-ref (vector-ref v 1) 1)))
(define (lambda-var i diff-depth)
  (if (zero? diff-depth)
      (let ((v (sexp->var
		(string->symbol
		 (string-append "@" (number->string i))))))
	(var:set-def! v i)
	(or (vector-ref v 4)		;so simple-shadowed-lambdavar? works
	    (vector-set! v 4 (make-list i #f)))
	v)
      (var:differential (lambda-var i (+ -1 diff-depth)))))
;;; This sometimes is called with shadowed variables (:@4)
(define lambda-position var:def)

;;; currently unused
;; (define (var:sexp-string v)
;;   (var->string (var:nodiffs v)))
;; (define (var:sexp-apply proc var)
;;   (if (var:differential? var)
;;       (var:differential (var:sexp-apply proc (var:undiff var)))
;;       (apply proc var '())))

(define (extrule e)
  (let ((vd (var:def e)))
    (and (pair? vd) vd)))
(define (potent-extrule e)
  (let ((vd (var:def e)))
    (and (pair? vd) (not (eqv? 0 (cadr vd))) vd)))
(define (defext var impl)
  (let ((deps (var:depends var)))
    (set! deps
	  (if (null? deps) (remove var (var:build-depends (list impl)))
	      (remove (car _^) deps)))
    (var:set-depends! var deps)
    (var:set-pri! var (if (null? deps) 10 ;must be a constant.
			  (+ 1 (apply max (map var:pri deps)))))
    (var:set-def! var (vsubst var $ impl))
    var))

;;; IMPL is a data type consisting of a poly with major variable
;;; $.  The value of the IMPL is negative of the poly solved for $.
;;; Using this representation, if poly is square-free and has no
;;; content (gcd (coefficients) = 1), we can express any
;;; algebraic function or number uniquely, even those with no standard
;;; representation (order > 4 roots).

(define (expr? p)
  (or (number? p)
      (and (pair? p)
	   (poly:var? (car p)))))
(define (impl? p) (and (pair? p) (poly:var? (car p)) ($? (car p))))
(define (rat:number? p)
  (or (number? p)
      (and (impl? p)
	   (= 3 (length p))
	   (number? (cadr p))
	   (number? (caddr p)))))
(define (expr:0? p) (or (eqv? 0 p) (and (impl? p) (eqv? 0 (rat:num p)))))
(define (expl? p)
  (or (number? p)
      (and (pair? p)
	   (poly:var? (car p))
	   (not ($? (car p))))))
;;; Rational impl?
(define (rat? p) (and (impl? p) (= 3 (length p))))
(define (make-rat num denom) (list $ num (poly:negate denom)))
(define (rat:num p) (poly:negate (cadr p)))
(define (rat:denom p) (caddr p))
(define (rat:unit-denom? p) (unit? (caddr p)))

(define (bunch? p)
  (or (null? p)
      (and (pair? p)
	   (not (poly:var? (car p)))
	   (not (eqv? $= (car p))))))

(define $= "=")
(define (eqn? p) (and (pair? p) (eqv? $= (car p))))
(define (eqns? p) (if (bunch? p) (some eqns? p) (eqn? p)))
(define (licit? p)
  (or (number? p)
      (and (pair? p)
	   (or (poly:var? (car p))
	       (eqv? $= (car p))))))

(define eqn->poly cdr)
(define (poly->eqn p) (cons $= p))
(define (polys->eqns p) (if (bunch? p) (map polys->eqns p) (poly->eqn p)))
(define (var->expl v) (list v 0 1))
(define (expl->impl p) (make-rat p 1))
(define (var->impl v) (make-rat (var->expl v) 1))

;;; Two paradigms for doing algebra on equations and expressions:
;;; Polynomials as expressions and Polynomials as equations.
;;; Polynomials are used as expressions in GCD.
;;; Polynomials are used as equations in ELIMINATE.
;;;	licit->	polxpr	poleqn
;;;	eqn	expl	expl
;;;	expl	expl	impl
;;;	impl	expl(?)	impl
;;; After the operation is done, we need to convert back.  For
;;; Polynomials as expressions, the result is already expl.  For
;;; polynomials as equations:
;;;	poleqn->licit
;;;	expl	eqn
;;;	impl	expr
(define (licit->poleqn p)
  (cond ((symbol? p) (var->impl (sexp->var p)))
	((eqn? p) (eqn->poly p))
	((impl? p) p)
	((expl? p) (expl->impl p))
	(else (math:error 'cannot-be-coerced-to-poly-eqn:- p))))
(define (licits->poleqns p)
  (if (bunch? p) (map licits->poleqns p) (licit->poleqn p)))
(define (poleqn->licit p)
  (cond ((impl? p) (expr:norm p))
	((expl? p) (poly->eqn p))
	(else (math:error 'not-a-polynomial-equation p))))
(define (poleqns->licits p)
  (if (bunch? p) (map poleqns->licits p) (poleqn->licit p)))
(define (licit->polxpr p)
  (cond ((symbol? p) (var->expl (sexp->var p)))
	((eqn? p) (eqn->poly p))
	((expl? p) p)
	((and (impl? p) (poly:/? (rat:num p) (rat:denom p))))
	(else (math:error 'cannot-be-coerced-to-expl:- p))))
(define (licit->impl p)
  (cond ((symbol? p) (var->impl (sexp->var p)))
	((eqn? p) (math:error 'value-expected-equation-found:- p))
	((impl? p) p)
	((expl? p) (expl->impl p))
	(else (math:error 'cannot-be-coerced-to-implicit:- p))))
(define (licits->impls p)
  (if (bunch? p) (map licits->impls p) (licit->impl p)))
(define (expr p)
  (cond ((symbol? p) (var->expl (sexp->var p)))
	((expr? p) p)
	(else (math:error 'cannot-be-coerced-to-expr:- p))))
(define (exprs p)
  (if (bunch? p) (map exprs p) (expr p)))
(define (expl:var? p)
  (and (pair? p)
       (expl? p)
       (equal? (cdr p) '(0 1))))
(define (expl->var p)
  (cond ((symbol? p) (sexp->var p))
;	((poly:var? p) p)
	((expl:var? p)
	 (car p))
	(else (math:error 'not-a-simple-variable:- p))))
(define (variables p)
  (cond ((symbol? p) (list (sexp->var p)))
;	((poly:var? p) (list p))
	((expl:var? p)
	 (list (car p)))
	((list? p) (map expl->var p))
	(else (math:error 'not-a-simple-variable:- p))))
(define (plicit->integer p)
  (cond ((integer? p) p)
	((not (rat:number? p)) (math:error 'not-an-integer- p))
	((rat:unit-denom? p) (* (rat:denom p) (rat:num p) -1))
	(else (math:error 'not-an-integer- p))))
(define (unit? x) (member x '(1 -1)))
(define (expr:norm p)
  (if (and (rat? p) (rat:unit-denom? p))
      (poly:* (rat:num p) (rat:denom p))
    p))
(define (expr:norm-or-unitcan p)
  (if (and (rat? p) (rat:unit-denom? p))
      (poly:* (rat:num p) (rat:denom p))
      (unitcan p)))

;;; These two functions return type expl
(define (num p)
  (cond ((impl? p) (rat:num p))
	((expl? p) p)
	(else (math:error 'cannot-extract-numerator- p))))
(define (denom p)
  (cond ((rat? p) (rat:denom p))
	((expl? p) 1)
	(else (math:error 'cannot-extract-denominator- p))))
(define (sexp? e)
  (cond ((number? e) #t)
	((symbol? e) #t)
	((pair? e) (symbol? (car e)))
	((vector? e) #t)
	(else #f)))

;;; A useful companion for ZERO?
(define (one? n) (eqv? 1 n))