This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: quaternions in the numeric tower


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]