Did you know ... Search Documentation:
Pack cosmos -- docs/transp - Copy.md

A Prolog Transpiler

Cosmos is an authentic, modern logic programming language that compiles to Prolog.

By acting as a transpiler to Prolog, Cosmos is a transpiler language. A transpiler language compiles to an already existing language.

As a modern logic programming language, we compile to Prolog. Yes, Prolog. Cosmos is an authentic relational or logic language.

This should raise a few questions.

Does Prolog benefit from a transpiler? (Definitely.) --

Cosmos provides plenty of syntax sugar and utilities that may not have been there in "classical Prolog" as most know it.

This includes,

  • A modern syntax with conventional operators (!= for inequality, // for comments).
  • Syntax sugar.
  • Consistent naming.
  • Modularity (modules, objects).
  • Types and better error-checking.
  • Functional programming.
  • Operators such as if and not.
  • A better standard library. Is Prolog Pure? --

    As much as pure logic programming is possible in Prolog, it's not the default.

    A new programmer has to avoid numerous pitfalls and search among thousands of different predicates and libraries for the few that support pure logic programming in order to program logically.

    It's technically possible, but a careful choice of libraries have to be made beforehand.

    In Cosmos, this choice is made for you. A simple construct can be compiled to use any available modern advances in LP (Logic Programming)[1]. Cosmos code is pure unless the user picks an explicitly impure construct.

    Programming in logic is in fact the motto of logic programming. Prioritizing pure logic programming is natural and compatible both with the decision of being a logic language and to being an user-friendly scripting language.

    [1] Such as reif, clp, delaying goals as in Prolog II or NU-Prolog, etc.

    Runtime and overall error-checking --

    This is not frequently mentioned, but Prolog is very prone to runtime errors. There's quite a difference to programming with more or less runtime errors and error-checking.

    Syntax sugar --

    Some syntax sugar can be useful aswell.

    For example, function notation can decrease the amount of explicit variables, i.e. x=double(double(2)).

    This would otherwise be,


    Falling back --

    If one needs to fall back from relational programming, it's possible to do so by using functions.

    When doing so, one avoids dealing with low-level operators like \+ and !. These are widely considered the goto of LP.

    By doing so, we solve what some consider the two main issues with classical LP,

  • Using it as an actual logical language.
  • Falling back from the paradigm (i.e. using low-level operators) when necessary. Scripting languages --

    The syntax of Cosmos™ is based on scripting languages. In particular, modern languages like Python and particularly prototypal ones like Lua and JavaScript.

    At the same time, we try to avoid common criticisms of those languages.

    In particular, we avoid having redundant operators and variables are local by default, not global.

    There's also quite a support for prototypal-mixed-with-logic syntax,

            rel human(x)
            rel mortal(x)

    While inserting functions/relations in a map to create a module or object is a common pattern in such languages, this is often not possible with such clean syntax.

    That is, code outside of a module can be simply inserted as-is into the module or object. It's then possible to switch from an Object to a non-Object style or vice versa.

    A note on syntax --

    It goes without saying that Prolog is an old language. As an old language, certain design choices may be seen as unconventional, if only because these choices weren't widely adopted.

    For example, the identifier foo is not a valid variable name because it's lowercase. I'm sure LP enthusiasts know this. Certainly, they should also know this is unconventional in the context of modern languages and somewhat unique.

    Regardless of whether this makes sense or not, we went for a more conventional approach. foo is a valid variable name. We take care to specify which decisions are based on familiarity (conventional design choices). Regardless, a better focus on pure LP are simply more desirable in general.

    What about performance? Is it good for my application? --

    The performance of a transpiler language depends on (1) the target language's performance, and (2) how it's compiled.

    The compiler relies a lot on meta-predicates to support closures. Using features such as objects will also have the predictable performance cost of using objects.

    Neither Cosmos nor the target language are system languages. It's not the focus of the language.

    Note however that we don't know what your application is. Please determine yourself whether this is good enough for your application.

    What about a more direct C compiler? --

    A C compiler was planned at one point.

    However, using a host language suffices for our goals. Even without its own compiler, Cosmos™ can already be used as,

  • A simple language for scripting.
  • A way to try functional or logic programming with a procedural syntax.
  • An academic language...
  • and more. If you think it deserves its own implementation, please send code or support the language.