mthom / scryer-prolog

A modern Prolog implementation written mostly in Rust.
BSD 3-Clause "New" or "Revised" License
2.05k stars 120 forks source link

Call Rust Code (FFI) #231

Open Immortalin opened 4 years ago

Immortalin commented 4 years ago

Would be great if Scryer can call libraries written in Rust.

mthom commented 4 years ago

From what I understand, the state of dynamic linking in Rust is not great. I don't know how else it might work.

Immortalin commented 4 years ago

What about passing data structures back and forth without copying? I am trying to do a JIT interpreter with the parser written in Prolog because of its support for DCGs. Any suggestions?

https://github.com/kloudtrader-github/Susie

Prolog parser -> Rust Cranelift JIT code generator

Related #225

XVilka commented 4 years ago

Not at all, see http://adventures.michaelfbryan.com/posts/plugins-in-rust/

mthom commented 4 years ago

I am interested in code generation using Cranelift, and it would be great to perform code generation from the Prolog toplevel.. but that's a ways away. I have plenty to do before getting to that point.

Are you asking if I have suggestions for implementing a parser using DCGs? I'm not sure. I think they're equivalent to pushdown automata/context-free grammars, so their suitability depends on the complexity of your language's grammar.

mthom commented 4 years ago

You could interpret data on the heap in the same fashion the WAM does. The heap is a Rust data structure, in src/prolog/machine/heap.rs. See the Aït-Kaci book for how data is represented.

UWN commented 4 years ago

Direct access to Prolog terms via FFI does not exist in other systems. Instead, a pretty clumsy interface which originated in Quintus Prolog exists in SICStus, SWI, YAP and some more. The clumsiness is due to the possibility to reorganize (GC, atom-GC and some more) Prolog terms at almost any point in time. However, this interface itself produces much too much overhead such that many prefer not to use it at all.

Also, such an FFI leads to code using that FFI. And that is in the rarest case pure code.

triska commented 1 year ago

I think #1744 addresses this nicely?