## Casting as Abstraction

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.

``````print(int(x)) //3
``````

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.

• An operator is used instead, making it unclear that 1+2 and 3 are different types.
• The operator is called `is`. Suffice to say, this is misleading in a logic programming language that has an `=` operator to boot.
• While this allows for logically pure code in some ways, that's not always the case. 1+2=3 evaluating to false is an example.

Prolog tries to have its cake and eat it too in how it tries to treat arithmetic. It's unsure on whether to consider 1+2 as a number or not. This is done to make a logically sound system but it ends up being unsound in some ways.

Cosmos inherits this system somewhat, as Prolog is its host language. However,

• Erroneous results can be better dealt with in a typed language (for example, 1+2=3 is simply a type error).
• We use casting as it's a better metaphor for the whole thing.

## What is a functor?

Most Prolog tutorials simply explain functors as "composite data". Effectively, they're very similar to what's known as `named tuples`. They may in fact just be `named tuples`.

The use of the word in Prolog predates the functional use of the word. Despite this, many of the same patterns can be used regardless of whether it's functional or language programming we're talking about. It seems being typed makes logic functors very similar to functional functors.

But what is a functor? You're free to think about this whether it is from a philosophical, mathematical or historical perspective. It gets asked a lot.

Cosmos, however, is only interested in this from a pragmatic perspective. It's enough that we can use them as a language feature to implement lists and other structures. We don't ask "what is a for-statement?". We simply use for-statements.

Cosmos is an user-friendly language. It would not be very user-friendly to not have a ready, general-purpose `print` statement. As such, a `hello world` can be written as,

`print('hello world')`

Note that this will output, `'hello world'` While `io.writeln('hello world')` will output, `hello world`

In short, write outputs to the user and is the relation to use in a proper program while `print` can still be used for debugging or short scripts.

## Host

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'
``````

## Using Cosmos within Prolog

It's possible to do the opposite, that is, embedding Cosmos in a Prolog program.