trueagi-io / hyperon-experimental

MeTTa programming language implementation
https://metta-lang.dev
MIT License
133 stars 44 forks source link

Minimal MeTTa lazy and, or implementations #612

Open ngeiswei opened 6 months ago

ngeiswei commented 6 months ago

In minimal MeTTa, or and and are implemented as follows

(: or (-> Bool Bool Bool))
(= (or False False) False)
(= (or False True) True)
(= (or True False) True)
(= (or True True) True)
(: and (-> Bool Bool Bool))
(= (and False False) False)
(= (and False True) False)
(= (and True False) False)
(= (and True True) True)

At first sight it looks like an eager implementation. It is possible to replace it by a lazy implementation?

ngeiswei commented 6 months ago

I tried the following

(: or (-> Bool Atom Bool))
(= (or False $x) $x)
(= (or True $x) True)
(: and (-> Bool Atom Bool))
(= (and False $x) False)
(= (and True $x) $x)

but it's not any faster.

It's not lazier either, indeed the following

!(and False (trace! "and: not lazy" True))
!(or True (trace! "or: not lazy" False))

outputs

"and: not lazy"
"or: not lazy"
[False]
[True]
ngeiswei commented 6 months ago

It looks like the reason my suggestion above does not work is because or and and in stdlib.metta does not seem to be used by minimal MeTTa. If I introduce lazy-or and lazy-and instead as follows:

(: lazy-or (-> Bool Atom Bool))
(= (lazy-or False $x) $x)
(= (lazy-or True $x) True)
(: lazy-and (-> Bool Atom Bool))
(= (lazy-and False $x) False)
(= (lazy-and True $x) $x)

then it works.

ngeiswei commented 6 months ago

Which brings another issue, how to simultaneously type annotate metatypes and regular types? For instance, how to express that an argument should be left unchanged (via the Atom metatype) but should be of a certain regular type, like Bool?

ngeiswei commented 6 months ago

This issue is a duplicate of https://github.com/trueagi-io/hyperon-experimental/issues/514