Skip to content

Latest commit

 

History

History
464 lines (327 loc) · 9.88 KB

note.md

File metadata and controls

464 lines (327 loc) · 9.88 KB

To address these limitations, we turn to symbolic logic languages that support more structured and interpretable reasoning. Among them, Prolog [12] stands out as a high-level declarative programming language designed for logic-based inference. It represents knowledge through facts and rules and, with its strict syntactic structure and built-in backtracking mechanism, enables efficient symbolic reasoning. These features make it particularly suitable for tasks such as logical inference, knowledge representation, and rule-based problem solving. However, Prolog’s rigid syntax and limited expressive power pose challenges for directly representing certain logical structures inherent in NL. For example, Prolog cannot directly express the existential quantifier commonly found in NL. A sentence like There exists a bird that is black cannot be directly encoded in Prolog, which only supports universally applicable facts or rules. Similarly, Prolog has fundamental limitations in expressing classical negation. A statement such as The tiger is not a bird cannot be directly asserted as a fact in Prolog. These limitations significantly reduce the expressiveness of Prolog in capturing the logical structures of NL, and in turn, constrain its effectiveness as a reasoning engine for NL-derived logical representations.

Yes. I think meta could be useful in the compiler, but I would not primarily lower Cosmos meta into SWI's library(macros) syntax. I would make Cosmos meta an AST-level expansion mechanism, and only use SWI term_expansion/2 / goal_expansion/2 as a possible backend implementation technique.

The important distinction is that your two examples are actually two different kinds of macro.

1. Your examples have different expansion targets

meta unless(Goal condition, Goal body)
    if not condition
        body

meta twice(Expr value)
    value + value

Conceptually:

unless(...)  → Goal → Goal
twice(...)   → Expr → Expr

So I would classify them as:

Cosmos meta Input Output Natural expansion
unless Goal Goal goal expansion
twice Expr Expr expression/term expansion
meta foo(...) producing declaration(s) AST/declaration AST/declaration(s) term expansion

This fits the distinction you were already making between goals and expressions. It also avoids the problem of treating everything as one generic UnaryOp-like category.


What lowering could look like

Suppose:

meta unless(Goal condition, Goal body)
    if not condition
        body

and later:

rel test()
    unless(x = 0, print(x))

The Cosmos compiler could first expand that to something conceptually like:

rel test()
    if not (x = 0)
        print(x)

and then lower that to Prolog.

So the pipeline is:

Cosmos source
     │
     ▼
parse
     │
     ▼
AST
     │
     ├── meta expansion
     │      unless(C,B)
     │        ↓
     │      if not C then B
     │
     ▼
expanded / typed AST
     │
     ▼
Cosmos → Prolog lowering
     │
     ▼
Prolog

This is preferable to generating a Prolog macro immediately because the compiler still understands that condition is a Goal and body is a Goal.


How it maps to SWI

unless → goal_expansion/2

At the Prolog level, something analogous could be:

goal_expansion(unless(C, B), Expanded) :-
    Expanded = ... .

The exact replacement depends on what Cosmos if, not, and backtracking semantics lower to.

The important thing is that SWI's goal_expansion/2 is operating on a goal position.

So:

foo :-
    unless(C, B).

can become approximately:

foo :-
    <expanded-Cosmos-form>.

This is a good conceptual correspondence.


twice is different

For:

meta twice(Expr value)
    value + value

if you have:

x = twice(y)

you want:

x = y + y

This isn't really a Prolog goal expansion.

It is an expression/term expansion.

SWI's term_expansion/2 is closer in spirit, but there is an important difference: SWI's term_expansion/2 primarily operates on program terms/declarations being compiled, whereas Cosmos wants to expand arbitrary typed AST nodes nested inside other expressions.

For example:

Assign(
    x,
    Call(twice, [y])
)

would become:

Assign(
    x,
    Add(y, y)
)

That's something I would implement directly in the Cosmos AST expander rather than trying to make SWI do it.


What about library(macros)?

You could lower the two definitions into something resembling:

#define(unless(C, B), ...).
#define(twice(X), X + X).

and then generate Prolog containing calls such as:

unless(C, B)
twice(X)

That would be a neat proof-of-concept.

But I don't think it should be the main compiler architecture.

The problem is that Cosmos has more semantic information:

meta unless(Goal condition, Goal body)
meta twice(Expr value)

Those Goal and Expr annotations aren't just cosmetic. They tell the compiler where the macro is legal.

For example:

twice(x = 1)

should presumably be rejected because:

twice : Expr → Expr

while:

unless(x = 1, foo())

has:

unless : Goal × Goal → Goal

A generic Prolog macro system doesn't naturally give you that Cosmos-level type distinction.


I would therefore give Cosmos its own macro expander

Something roughly like:

              ┌──────────────────┐
Cosmos ──────►│ Parser           │
              └────────┬─────────┘
                       ▼
                  Cosmos AST
                       │
                       ▼
              ┌──────────────────┐
              │ Meta expansion  │
              │                  │
              │ Goal → Goal      │
              │ Expr → Expr      │
              │ Decl → Decl      │
              └────────┬─────────┘
                       ▼
                 Typed AST
                       │
                       ▼
                 Cosmos IR
                       │
                       ▼
                 Prolog backend

Internally, this could be quite SWI-like:

expand_meta(Node, Expanded).
expand_goal(Goal, Expanded).
expand_expr(Expr, Expanded).
expand_decl(Decl, Expanded).

The names aren't important; the separation is.


This could even mirror SWI very closely

Conceptually:

SWI-Prolog                    Cosmos

term_expansion/2       →      meta/AST expansion
goal_expansion/2       →      goal macro expansion
expand_term/2          →      expand_ast/2
expand_goal/2          →      expand_goal/2

But Cosmos has an additional category:

expand_expr/2

because Cosmos explicitly distinguishes expressions from goals.

That distinction is actually useful here.


There is another important possibility

You don't necessarily need meta to mean "macro."

For example, eventually you might have:

meta unless(Goal condition, Goal body)
    if not condition
        body

which is a transforming macro.

But:

meta verify_tla(Goal body)
    ...

might instead mean:

inspect this AST
    ↓
convert to temporal IR
    ↓
invoke TLA+ adapter
    ↓
produce diagnostics

It doesn't necessarily produce replacement Cosmos AST.

So I'd distinguish:

meta transformation
    AST → AST

meta verification / compiler hook
    AST → diagnostics/artifacts/check result

That becomes particularly useful for the architecture we discussed earlier.

For example:

meta unless(Goal condition, Goal body)
    if not condition
        body

is a true macro.

Whereas something like:

@verify(tla)
rel producer_consumer(...)
    ...

is better thought of as a compiler hook operating on the resulting typed AST/IR.


Would this actually help the Cosmos compiler?

I think yes, but mainly if you keep it small.

It gives you a clean way to implement language conveniences without adding each one as a special case to the parser/typechecker/code generator.

Instead of adding:

if
unless
when
repeat
...

all as hard-coded compiler constructs, some can potentially be defined in terms of existing Cosmos constructs.

For example:

meta unless(Goal c, Goal b)
    if not c
        b

could be expanded away very early.

Then the core compiler only needs to understand:

if
not
goals
expressions
relations
...

rather than necessarily treating unless as a fundamental IR operation.

That is essentially the same benefit macros provide in languages such as Lisp-family languages, but with your Goal/Expr type distinction preserved.


I would not make everything a macro, though

A useful rule would be:

Language semantics
    ↓
core AST / IR

Convenience syntax
    ↓
meta expansion

Backend-specific optimization
    ↓
backend expansion

External verification
    ↓
compiler hook/plugin

So something fundamental like:

and
or
not
until
always
eventually

especially if they have formal temporal semantics, should probably remain represented in the IR.

But something like:

unless(...)
twice(...)
swap(...)
clamp(...)

could potentially be library-level meta definitions.

That gives you a fairly powerful property:

The Cosmos language can have a small semantic core while still having a rich standard library of syntactic constructs.

And the SWI-Prolog analogy is useful here: Cosmos can borrow the idea of term_expansion and goal_expansion without making the Cosmos compiler itself dependent on SWI's macro implementation.