Basic Types in Alice
Well, I guess that “basic” may not be really correct, but I have not found another name for them, so I’ll stay with this.
I’m talking about the basic elements which can be introduced as immediate values, such as “1″ or “1.3″.
There are quite a few of them, and so my first exploration has been related to trying them in the REPL.
ML has builtin boolean values, and logical operators for them:
> true; val it : bool = true > false; val it : bool = false > false andalso true; val it : bool = false > true orelse false; val it : bool = true
I don’t know why there are those strange names, but it seem that and is a keyword used somewhere else. The boolean operators are short-circuiting as in most languages.
Note the interesting output of the REPL, it gets read like:
val [name]: [type] = [value]
The it thing is a pseudo variable representing the last computed thing (when you don’t assign it to something else), you can refer to it explicitly like this:
> false; val it : bool = false > it andalso true; val it : bool = false
You can, obviousaly, declare your own things with the same syntax, and you can omit the type declaration since Alice will be able to infer it by itself looking at the value you are assigning:
> val myVal= true andalso false; val myVal : bool = false
ML, and thus Alice also have some types representing numbers, specifically:
ints (signed integers) introduced as decimal or hexadecimal values in C-like format (0xDEADBEEF)
reals introduced with the usual dotted notation (1.234) or scientific notation (1.1E2 or 1.1e2)
words (unsigned integers) inhtroduce in decimal notation as 0w1234 or hexadecimal 0wx1234aF.
An unusual thing is that when declaring negative values you must prepend a “~” to the value, not a “-”.
Basic operations are provided on these values: +,-,*, div and mod ara available for int and word values, while you can only access / with real values.
The comparison operators are ,=,=, and are available on all types, but there is a difference wrt other languages, since you can’t compare apples and oranges, unless you explicitly transform one value into another.
See this:
> 0xff >= 255; val it : bool = true > 0wxff >= 255; 1.0-1.12: mismatch on application: expression type word * int does not match function's argument type word * word because type int does not unify with word
If you’re wondering why the REPL writes “word * int” when we were using a comparison operator.. well, that should be considered as a type composed of the product of a word value and an int value. Product types are quite common, so feel free to familiarize with it.