• Stars
    star
    560
  • Rank 79,541 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The only reasonable scripting engine for Go.

prolog - the only reasonable scripting engine for Go

Go Reference Actions Status Go Report Card codecov Mentioned in Awesome Go

What is this?

ichiban/prolog is an embeddable ISO Prolog interpreter in Go.

Comparison with Other Libraries

prolog otto go-lua
Language ISO Prolog ECMA Script Lua
Paradigm πŸŽ“ Logic Object-oriented Object-oriented
Go API 😻 database/sql-like original original
Declarative βœ… ❌ ❌
Sandboxing βœ… ❌ βœ…

Getting started

Install latest version

go get -u github.com/ichiban/prolog

Usage

Instantiate an interpreter

p := prolog.New(os.Stdin, os.Stdout) // Or `prolog.New(nil, nil)` if you don't need user_input/user_output.

Or, if you want a sandbox interpreter without any built-in predicates:

// See examples/sandboxing/main.go for details.
p := new(prolog.Interpreter)

Load a Prolog program

if err := p.Exec(`
	human(socrates).       % This is a fact.
	mortal(X) :- human(X). % This is a rule.
`); err != nil {
	panic(err)
}

Similar to database/sql, you can use placeholder ? to insert Go data as Prolog data.

if err := p.Exec(`human(?).`, "socrates"); err != nil { // Same as p.Exec(`human("socrates").`)
	panic(err)
}

Run the Prolog program

sols, err := p.Query(`mortal(?).`, "socrates") // Same as p.Query(`mortal("socrates").`)
if err != nil {
	panic(err)
}
defer sols.Close()

// Iterates over solutions.
for sols.Next() {
	fmt.Printf("Yes.\n") // ==> Yes.
}

// Check if an error occurred while querying.
if err := sols.Err(); err != nil {
	panic(err)
}

Or, if you want to query for the variable values for each solution:

sols, err := p.Query(`mortal(Who).`)
if err != nil {
	panic(err)
}
defer sols.Close()

// Iterates over solutions.
for sols.Next() {
	// Prepare a struct with fields which name corresponds with a variable in the query.
	var s struct {
		Who string
	}
	if err := sols.Scan(&s); err != nil {
		panic(err)
	}
	fmt.Printf("Who = %s\n", s.Who) // ==> Who = socrates
}

// Check if an error occurred while querying.
if err := sols.Err(); err != nil {
	panic(err)
}

The Default Language

ichiban/prolog adheres the ISO standard and comes with the ISO predicates as well as the Prologue for Prolog and DCG predicates.

See the Wiki for the directives and the built-in predicates.

Top Level

1pl is an experimental top level command for testing the default language and its compliance to the ISO standard.

You can install it with go install:

go install github.com/ichiban/prolog/cmd/1pl@latest

Then, you can enter the top level with 1pl:

$(go env GOPATH)/bin/1pl [<file>...]

Extensions

  • predicates: Native predicates for ichiban/prolog.
  • kagomelog: a Japanese morphological analyzing predicate.

License

Distributed under the MIT license. See LICENSE for more information.

Contributing

See ARCHITECTURE.md for architecture details.

  1. Fork it (https://github.com/ichiban/prolog/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Acknowledgments

We would like to extend our thanks to the following individuals for their contributions to this project:

We are grateful for the support and contributions of everyone involved in this project. Arigatou gozaimasu!

More Repositories

1

jesi

Hypermedia API Accelerator
Go
19
star
2

logcentric

ZeroMQ centralized logging for log4j. ZMQAppender is now a separate project by @lusis
Java
15
star
3

ascii-diagram

<ascii-diagram> tag
HTML
10
star
4

unagi

8
star
5

assets

Go resource embedding library which doesn't require `go generate` at all
Go
8
star
6

cl-jack

a thin JACK Audio Conection Kit wrapper written in Common Lisp
Common Lisp
7
star
7

cyclomatic

go cyclomatic complexity analyzer
Go
7
star
8

thelper

Go static analyzer that reports where you forgot to call t.Helper().
Go
5
star
9

btdb

Embeddable B+Tree Database Management System
Go
3
star
10

minhash

online LSH server based on Minhash that stores in Berkeley DB Java Edition and speaks ZeroMQ
Java
3
star
11

linesqueak

linenoise clone in Go
Go
2
star
12

prodinspect

Go
2
star
13

b07

a twitter bot
Ruby
2
star
14

sqlfmt

An opinionated SQL formatter
Go
2
star
15

iimio-hai

Common Lisp
2
star
16

tlsinspect

Go
1
star
17

bday

Ruby
1
star
18

onakahelicopter

おγͺγ‹γƒ˜γƒͺγ‚³γƒ—γ‚ΏγƒΌγΏγŸγ„γͺγƒ€γ‚Έγƒ£γƒ¬γ‚’θ€ƒγˆγ‚‹γ¨γγ«δ½Ώγ†γ¨γ™γ”γδΎΏεˆ©
Ruby
1
star
19

httpproxyfailover

Create a fault-tolerant HTTP proxy out of multiple somewhat unreliable HTTP proxies.
Go
1
star
20

rel

experimental go code generator for DB operations
Go
1
star
21

rtunnel

HTTP tunneling server/client
Go
1
star
22

llbl

"Let Localhost be Localhost" DNS server which resolves `*.localhost` to `127.0.0.1`
Go
1
star
23

xorsaver

XOR texture screensaver
C
1
star
24

hanakyabetsu

Bot library with Markov generator in Common Lisp
Common Lisp
1
star
25

kagomelog

Go
1
star
26

cast

The easiest command to watch your contents on TV
Go
1
star
27

seams

Go static analyzer that reports untestable function/method calls
Go
1
star
28

smatch

programs for my graduation thesis.
OCaml
1
star
29

funcdecl

Go
1
star
30

ocaml-datrie

Double-Array Trie in OCaml
OCaml
1
star
31

cellar

Go
1
star