lang effort: type conversions



if anyone cares (or wants to make comment), after working some more of the
inferencer for the lang I am implementing, I have started to consider some
rules.


in its basic semantics, the lang will be dynamically typed with a prototype
object system, however, the lang will also have the ability to define static
types and use them for basic optimizations.

at this point in time, a lot of type violations aren't caught, and those
that are often only generate warnings. this may change eventually (it is
still the first month of implementation).


the basic syntax is more or less c style, and similar goes for declarations:
int x;
int fib(int x)if(x>2)fib(x-1)+fib(x-2) else 1;

there will also the variant type ('var'), which may take any other type, and
will not have many of the same restrictions as the other types.

in the case of functions, types may be ommited:
fact(x)if(x>1)x*fact(x-1) else 1;
in which case, variant will be assumed (unless the inferencer thinks
otherwise).

in the case of variables, a type is still required so that the parser is
sure that it is a definition, eg:
var x;

all types are implemented as tagged references.

most casts will be implicit, but an explicit cast may be written as the type
name followed by an expression:
(long 3)/4.


numerical types:

for numbers, the following types are viewed as primitive:
int, represented as a fixnum, with 29 bit precision, 28 unsigned;
long, either a fixnum or an on-heap object, with undefined precision (for
now, 64 bit, but maybe bignums may also exist);
float, as with fixnums, it is in-place with reduced precision;
double, on heap with full precision;
rat, rational, fixnum (if integral), or on heap (if needed).

other numeric types (eg: complex) will typically be heap-based, and will
have more generic rules (and are more resigned to being slower).

basic ops division integer division
int+int->int, int/int->float, int\int->int
long+long->long, long/long->double, long\long->long
long+int->long, long/int->double, long\int->long
rat+rat->rat rat/rat->rat undefined...
rat+int->rat, rat/int->rat
rat+long->rat, rat/long->rat
float+float->float
double+double->double
double+float->double
float+rat->double
....


variants will work slightly different wrt integer values:
all operations will use the smallest, sufficiently large numerical type, and
will promote types as needed (wheras int will generally allow them to
overflow). likewise, int/int->int if int%int=0.



I am also considering seperating the variable and function namespaces.

function calls will work by first checking the function namespace, then the
variable one (if no functions are declared by that name in the current
scope).

declared functions will be immutable. mutable functions are possible, but
will need to be declared like variables.

example:
fact(x)if(x>1)x*fact(x-1) else 1;
will declare an immutable function, however:
var fact=fun r(x)if(x>1)x*r(x-1) else 1;
will declare a mutable one.


also for declared functions, this may be restricted further:
declared functions may not be used in the creation of new objects at all;
it will only be possible to use these functions in a new object via either
delegation or cloning.

note that in the case of delegation, it will be allowed to overload the
functions with new ones (if they have matching function definitions).


these restrictions will not apply to first-class functions.

note, however, for other reasons closures may not be allowed as methods
(first-class functions and closures are distinguished based on whether or
not they capture variables from the parent scope).


and so forth...


.



Relevant Pages

  • One way CL doesnt suck (was Re: How Common Lisp sucks)
    ... CL-USER> (defun foo (x y) ... (declare (fixnum x y)) ... result of down to fixnum bits. ... Well, no, but that's because here your LOGAND doesn't do what I think ...
    (comp.lang.lisp)
  • Re: why is lisp fast?
    ... Why do you equate "wanting to compete ... of the addition as a fixnum if the user wishes to inform the system ... lisp objects. ... (declare (fixnums-remain-fixnum t)) ...
    (comp.lang.lisp)
  • Re: increasing width
    ... which I get every time I want to declare real. ... used to declare single or double precision variables. ... easily support more than one "kind" of single precision variable. ... have at least 13 digits of precision, ...
    (comp.lang.fortran)
  • Re: increasing width
    ... Some fellow over in sci.math.num-analysis tells me I can get at least 6 more sig figs, if instead of declaring as type real, I declare as real. ... used to declare single or double precision variables. ... easily support more than one "kind" of single precision variable. ... have at least 13 digits of precision, ...
    (comp.lang.fortran)
  • Re: static, dynamic and implicitely typed languages
    ... If you *are* sure of that, then you can tell a CL compiler ... | (declare (type number a) ... | (declare (type fixnum a b c) ... (defun foobar (a b c) ...
    (comp.lang.lisp)