trueagi-io / hyperon-experimental

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

Fix unique #785

Closed vsbogd closed 1 month ago

vsbogd commented 1 month ago

Fixes #760 Also removes interpreter from the functions implementations.

vsbogd commented 1 month ago

@ngeiswei could you please check if it what you needed?

ngeiswei commented 1 month ago

Works like a charm, thanks!

ngeiswei commented 1 month ago

Unfortunately I spoke too fast :-(, it does not work in my case, but it is a complex one, I want to simplify it a bunch before pasting it here. But to give you an idea, the following

!(unique (bc &kb
             (fromNumber 5)
             (: $prg (-> (: $x (SongIn "English")) (SongIn "Chinese")))))

does not work, while the following

!(let* (($results (collapse
                   (bc &kb
                       (fromNumber 5)
                       (: $prg (-> (: $x (SongIn "English")) (SongIn "Chinese"))))))
        ($uniq-results (unique (superpose $results))))
   $uniq-results)

does. Meaning that I need to explicitly collapse the results, then call unique on their superposition, and then it works.

I am not sure if it is a bug or a feature, I can still use that, but of course it is not as elegant as just calling unique on my non-deterministic function call.

ngeiswei commented 1 month ago

Please find a simplified example

(= (f) a)
(= (f) a)

!(unique (f))
!(let $y (collapse (f)) (unique (superpose $y)))
vsbogd commented 1 month ago

Thanks @ngeiswei. Seems like it is a bug which prevented me to implement it last time. unique is implemented in stdlib and thus when collapse is called from implementation it uses stdlib space as a context. As consequence (f) is not evaluated and stays (f) because stdlib doesn't have definition of (f). It is the reason why unique returns (f) and after collapse it is evaluated in two instances of a.

ngeiswei commented 1 month ago

This is better than nothing, I can still use it by collapsing and superposing, so I approve merging if that's the best we can get for now.

vsbogd commented 1 month ago

Well, I think we can merge it, because it is better then previous state. Then I will think how to fix it properly and raise another PR.

vsbogd commented 1 month ago

Btw, @ngeiswei , the following code works:

(= (f) a)
(= (f) a)

!(unique (capture (f)))