|Did you know ...||Search Documentation:|
|Pack cosmos -- docs/quickstart2 - Copy.md|
Cosmos' arithmetic system is a bit intricate. Let's try summing up two numbers.
x=1+2 print(x) //1+2
This occurs because the arithmetic operators are actually functors with special syntax. 1+2 really is a functor of type Math Integer Integer rather than an actual Integer.
In order to get the intended result, we must then convert this functor to an Integer. This is where we use casting.
Let's look at the following code,
x='c'+y y='d' print(str(z)) //'cd'
Because we are using functors, we can write logically pure code that sums up strings (naturally, string arithmetic is supported in Cosmosâ¢ ) even if one or more of the parameters is not defined at the time. Note that y is only defined in the second line.
Only at the end of the program do we need to compute the result, if we need to- for example, in order to write it on the screen.
Prolog has, to say the least, a very poorly thought-out arithmetic system.
is. Suffice to say, this is misleading in a logic programming language that has an
=operator to boot.
Cosmos inherits this system somewhat, as Prolog is its host language. However,
CLP or constraint logic programming is a kind of sub-paradigm in modern LP. As such, we felt we should address this sooner or later.
Even regular LP implements logical equality, i.e.
x=2. This is what we may call an equality constraint when talking about the paradigm.
What about `x!=2`,
x>=2 and so on? CLP addresses those aswell, making them equally valid constraints that are added to a constraint store. Note that in a normal language, any of these would fail if x is not instantiated.
This should let you write,
>x>2 and x=5 | x=5 >x<2 and x=5 | false
Casting to a real or num (as seen in Casting as Abstraction) should also trigger constraint arithmetics.
No language that we know has constraints by default. What we mean is that if the user has to load external libraries and the default operators (in our case, !, <, >=, etc.) use something else, it can hardly be said that the language implements the paradigm natively. It's a second-class paradigm in the language.
This seems like a waste, however. And Cosmosâ¢ is the perfect language for it, as all we want is a scripting language that works logically by default.
As such, Cosmosâ¢ has constraints as the default modum operandis. You may freely try the paradigm as part of regular programming.
Our criteria for a CLP system was humble.
This does exclude quite a few libraries that use CLP for integers only, or other more specific uses. As such, we borrowed a CLP(R)--or CLP for Reals--library, with floating point numbers.
From what we know, in classical usecases constraints are not added to the store and thus don't consume extra memory, meaning there's no downside other than an extra check. It's just like a BigNum library!
Still, we are deeming this experimental only because we don't know of other languages that do this. As far as we are aware, we've given enough reason to go CLP-by-default. Other languages should be answering why they aren't CLP-by-default! We believe backwards-compatibility is often the only reason.
Cosmos is an user-friendly language. It would not be very user-friendly to not have a ready, general-purpose
Note that this will output,
io.writeln('hello world') will output,
write outputs to the user and is the relation to use in a proper program while
Cosmos is currently compiled into Prolog. As such, it's possible to call predicates of Prolog from Cosmos.
rel write(x) pl::write(x) //calls Prolog predicate 'write'
It's possible to do the opposite, that is, embedding Cosmos in a Prolog program.
See the Swi-Prolog page for more information.