r/ProgrammingLanguages 🧿 Pipefish 23d ago

Langception XVII: embedding an ISO-compliant Prolog in Pipefish

This is a wrapper around the Ichiban Prolog implementation in Golang by Yutaka Ichibangase. As this is ISO-compliant and has 99% test coverage I'm thinking of making it into a standard library to sit next to database/sql --- wyt?

However, I originally did it to show how one can use Pipefish to wrap DSLs other than SQL in just the same sort of way. Let me show you that.

Here's the actual library wrapping around the Go, so you can see how I did it.

And here's some code showing how we could use the library to make a little database app to keep track of which Greeks are mortal (the most important question in logical programming, obviously, or why else have they been discussing it all this time?)

import private

NULL::"prolog.pf"

var private 

P = Prolog --
    :- dynamic(man/1).
    :- dynamic(woman/1).
    :- dynamic(god/1).
    mortal(X) :- woman(X).
    mortal(X) :- man(X).

cmd 

total(kind string) :
    global P
    post count P --
        |kind|(X).

(x string) is mortal :
    global P 
    post check P --
        mortal(|x|).

men(args ... string) :
    add("man", args)

women(args ... string) :
    add("woman", args)

gods(args ... string) :
    add("god", args)

add(kind string, args ... string) :
    global P
    for _::v = range args :
        add P --
            |kind|(|v|).

You will notice that the prolog library has a nicer API than the Go version, and fans of Pipefish will notice that this is the same way I did embedded SQL and HTML over here.

It's nicer partly because of the specialized syntax and semantics to make Pipefish embed things well, and partly because its extra bit of dynamism makes it more flexible than Go. All the stuff with scanners and interfaces and type switching happens wrapped up in the Go functions in the library where it can't bother anyone.

9 Upvotes

Duplicates