This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: quaternions in the numeric tower
- From: Per Bothner <per at bothner dot com>
- To: kawa at sourceware dot org
- Date: Thu, 13 Nov 2014 00:24:59 -0800
- Subject: Re: quaternions in the numeric tower
- Authentication-results: sourceware.org; auth=none
- References: <6000E8B8-620C-4080-9D53-740C1334B164 at theptrgroup dot com> <546406F6 dot 6020509 at bothner dot com> <FCC79058-B575-4465-A4F3-814E36C5D924 at theptrgroup dot com> <54644B70 dot 30609 at bothner dot com> <9183E9EA-B19A-4EF9-B13E-B1E4D75C78BA at theptrgroup dot com>
On 11/12/2014 11:06 PM, Jamison Hope wrote:
Wait, but vector *is* in the LispLanguage type map. That's what
enables all the "::vector" type decorations in vectors.scm to work, no?
That type map isn't really needed for names that are in the lexical scope.
That's how define-alias works.
It might be desirable to get rid of the LispLanguage type map, but I haven't
explored what if anything would break.
That's what I'm trying to do. What I want to avoid is this:
#|kawa:1|# quaternion
ClassType gnu.math.Quaternion
#|kawa:2|# (quaternion? (java.lang.Double:valueOf 3))
#f
#|kawa:3|# (import (kawa quaternions))
#|kawa:4|# (quaternion? (java.lang.Double:valueOf 3))
#t
If the actual hand-written function doesn't get automatically inserted
into the global environment via initScheme(), then on Line 2 there
Kawa will work its usually-desirable magic of seeing the undefined
symbol named "quaternion?" and recognizing that it has the form of
typename + "?" and treating it as syntactic sugar for
(lambda (x) (instance? x <quaternion>)).
I tried an experiment. I created a module rl.scm with:
(module-name (rl))
(export myreal myreal?)
(require kawa.lib.numbers)
(import (rename (only (gnu kawa lispexpr LangObjType)
realType)
(realType myreal)))
(define (myreal? x) (real-valued? x))
It seemed to work:
#|kawa:1|# (import (rl))
#|kawa:2|# (define xx::myreal 12.4)
#|kawa:3|# xx
12.4
#|kawa:4|# (invoke xx 'getClass)
class gnu.math.DFloNum
#|kawa:5|# (set! xx 4)
#|kawa:6|# (invoke xx 'getClass)
class gnu.math.IntNum
#|kawa:7|# (set! xx "abc")
/dev/stdin:7:10: warning - type constant-string is incompatible with required type real
/dev/stdin:7:1: warning - cannot convert literal (of type java.lang.String) to Type real
Value 'abc' has wrong type (String) (expected: real)
at gnu.kawa.lispexpr.LangObjType.coerceRealNum(LangObjType.java:252)
...
#|kawa:8|# myreal?
#<procedure myreal?>
#|kawa:9|# (myreal? 4.5)
#t
#|kawa:10|# (myreal? "bc")
#f
#|kawa:11|# (myreal? xx)
#t
#|kawa:12|# myreal
Type real
One solution would be not to define quaternion as a type, and then
the first two lines there would throw errors. But I want to be able
to use quaternion as a type for definitions (in numbers.scm and
elsewhere) like
(define (exp (x :: <quaternion>)) :: <quaternion>
(invoke x 'exp))
This works:
#|kawa:13|# (define (myexp x::myreal) ::myreal (exp x))
However, this doesn't work:
#|kawa:16|# (define (myexp2 x::<myreal>) ::<myreal> (exp x))
That may be ok, since I'm trying to deprecated the <typename> syntax,
especialy for type aliases (i.e. non-classes). Or we could tweak
checkDefaultBinding in Translator.java to resolve <NAME> to NAME
when NAME is bound to a type.
Clearly asin, acos, and atan still belong in numbers.scm, so
either I make use of unit-vector from (kawa quaternions), or I
reimplement it in as a private function in numbers.scm. Or I
move the big expression to Java code, but that wouldn't be any
fun.
You can make unit-vector a public number in numbers.scm.
Just don't define it in initScheme.
Of course you have to import and re-export it in quaternions.scm.
--
--Per Bothner
per@bothner.com http://per.bothner.com/