[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Axiom-developer] The symbol 4 (was Re: Aldor and Axiom)

From: Ralf Hemmecke
Subject: [Axiom-developer] The symbol 4 (was Re: Aldor and Axiom)
Date: Tue, 14 Feb 2006 14:35:20 +0100
User-agent: Thunderbird 1.5 (X11/20051201)

Hmm.  But aren't there some cases where you want this, like
(say) knowing that 4 is a real integer without having to
explicitly say that? (Sorry that's probably a dumb question).

The phrase "real integer" is kind of dumb ... :) What other kinds
of integers are there? But the symbol 4 can certainly stand for
other things besides the integer 4.

Right. 4 is just a symbol consisting of three strokes. If one doesn't bring it into a context its meaning would be totally unclear. Well, nowadays humans tend to interpret this always as an integer, but I think several thousand years ago that was not so clear.

But in the user interface,
such as implemented by the Axiom interpreter, it is quite natural
that unless stated otherwise, this symbol could be assumed to
represent the element of the positive integers. On the other hand,
in the SPAD and Aldor compilers, there should be no "guessing"
of this kind.

Oh, why should 4 be considered to be a PI and not NNI or INT?

To avoid all this guessing, the Aldor compiler start with (nearly) zero knowledge. Similar to Axiom's "expose" one has to teach Aldor.

Look at the following

woodpecker:~/scratch/FRAC>aldor -gloop
     AA  L      DDDD      OOO    RRRR
    A A  L      D   D    O   O   R   R
   A  A  L      D    D  O     O  R   R
  AAAAA  L      D    D  O     O  RRRR
 A    A  L      D   D    O   O   R  R
A     A  LLLLL  DDDD      OOO    R   R

(c) Numerical Algorithms Group Ltd 1995-2001
Release: Aldor(C) version 1.0.3 for LINUX(glibc2.3) (debug version)
Type "#int help" for more details.
%1 >> #include "aldor"
                                           Comp: 190 msec, Interp: 40 msec
%2 >> #include "aldorinterp"
                                           Comp: 90 msec, Interp: 0 msec
%3 >> 4
[L3 C1] #1 (Error) No meaning for integer-style literal `4'.

%4 >> import from Integer
                                           Comp: 30 msec, Interp: 0 msec
%5 >> 4
4 @ AldorInteger
                                           Comp: 0 msec, Interp: 280 msec
%6 >> import from MachineInteger
                                           Comp: 30 msec, Interp: 0 msec
%7 >> 4
[L7 C1] #1 (Error) Have determined 2 possible types for the expression.
        Meaning 1: MachineInteger
        Meaning 2: AldorInteger

You see, 4 is not known to Aldor, initially.

After importing from Integer, the Aldor FOAM interpreter knows every export of Integer (yes Integer is a macro that expands to AldorInteger). Before FOAM code can be executed, it has to be compiled from Aldor source to FOAM (First Order Abstract Machine), so the compiler is involved here and there are not two languages as in Axiom.

After line %6, Aldor also sees the exports from MachineInteger (32-bit integers). Line %7 clearly tells you that the symbol 4 is ambiguous.

So what can you do?

Either you say
%8 >> address@hidden
4 @ AldorInteger


%9 >> x: MachineInteger := 4
4 @ MachineInteger

You see, the compiler forces you to be clear. That can be nasty when you start with Aldor, but you will love it once you are used to it. It prevents stupid type errors and that is what all these types are made for.

OK, how does that work?
There is a bit of syntactic sugar involved here. In fact, if the aldor compiler sees something that looks like an integer, it tries to find a function
  integer: Literal -> %
that is currently in scope and executes it with the Literal that it finds. Well, clearly, if it finds tow such functions, for example,
  integer: Literal -> Integer
  integer: Literal -> MachineInteger
it does not know which one to apply if the result type cannot be inferred from the context (as in lines %8 and %9 above).

And to make you happy, one can easily write his own domain that behaves quite differently from what you would expect when you read the source code not carefully enough.

#include "aldor"
CrazyInteger: IntegerType with {string: Literal -> %} == Integer add {
        Rep == Integer;
        import from Rep;
        integer(l: Literal): % == per(integer(l)$Integer + 1);
        string(l: Literal): % == per 42;

main(): () == {
        import from TextWriter, Character;
        import from CrazyInteger;
        stdout << "This is crazy :" << space << 1 << newline;
        stdout << "This is crazy :" << space << 2 << newline;
        stdout << "Fourtytwo" + 4 << newline;


For me it says.
>aldor -grun -laldor
42 1
42 3

I hope you expected that. ;-)

Like for integers there are functions
string: Literal -> % and float: Literal -> % which are used for string-like ("I am a string") and float-like (1.24e3) literals.

The first line of output is correct, since the compiler sees the integer style literal 1 and finds that "1: %" is an export of the category so that is clear. Since 1 is not implemented in % directly, it is inherited from "Integer" (the thing that comes before the "add").

For the second line the function string: Literal -> CrazyInteger is invoked for "This is crazy :" and integer: Literal -> CrazyInteger for the symbol 2 resulting in the output shown above.

You may notice that different from other programming languages, Aldor does not simplify seemingly constant expressions like 1+2 to 3 since either that is wrong (given the idea of Literals) or there is some danger of an infinite computation at compile time. Evaluating 1+2 means calling the function +. Now imagine I would have implemented it in that way:
(x:%)+(y:%):% == {while true repeat {}; x;}

So it means that the convertion from Literals to actual elements of the corresponding type happens at runtime.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]