No description
  • Go 83.4%
  • C 16.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
PPTide 1995dd3582
Add Go bindings for clingo
- capi: raw bindings generated by c-for-go from clingo.h (clingo.yml)
- clingo: idiomatic wrapper (Control, Symbol, Model, solving, externals,
  configuration, Go external functions, logging)
- tests, example, README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 12:44:05 +02:00
capi Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
.gitignore Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
bridge.c Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
bridge.h Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
callbacks.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
clingo.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
clingo.yml Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
clingo_test.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
control.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
example_test.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
generate.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
go.mod Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
README.md Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
solve.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00
symbol.go Add Go bindings for clingo 2026-10-17 12:44:05 +02:00

clinGo

Warning

This project is fully vibe-coded.

An AI assistant wrote all of the code, the bindings configuration, the tests and this README. No human has reviewed it line by line. It passes its own test suite, but expect rough edges, and don't rely on it for anything important without reviewing it yourself.

Go bindings for the clingo answer set solver.

The module has two packages:

Package What it is
git.pptie.de/PPTide/clinGo (package clingo) Idiomatic, hand-written wrapper: Control, Symbol, Model, solving, externals, configuration, Go external functions (@f(...)), logging.
git.pptie.de/PPTide/clinGo/capi Raw 1:1 bindings to clingo.h (about 250 functions, plus all types and constants), generated by c-for-go from clingo.yml. A few callback-struct APIs are left out (see below).

Requirements

You need the clingo C library and headers (tested with clingo 5.8.2):

brew install clingo

On Linux, install your distribution's libclingo-dev (or build clingo from source). If clingo lives somewhere non-standard, point cgo at it:

CGO_CFLAGS="-I/path/to/include" CGO_LDFLAGS="-L/path/to/lib" go test ./...

Usage

ctl, err := clingo.NewControl("0") // clingo CLI args; "0" = enumerate all models
if err != nil {
	log.Fatal(err)
}
defer ctl.Close()

ctl.Add("base", nil, "a :- not b. b :- not a.")
ctl.Ground(nil, nil) // grounds "base"

res, err := ctl.Solve(func(m *clingo.Model) bool {
	syms, _ := m.Symbols(clingo.ShowShown)
	fmt.Println(syms)
	return true // keep searching
})
fmt.Println(res) // SAT exhausted

External functions

ctl.Add("base", nil, "p(@double(21)).")
ctl.Ground(nil, func(name string, args []clingo.Symbol) ([]clingo.Symbol, error) {
	n, err := args[0].Number()
	return []clingo.Symbol{clingo.Number(2 * n)}, err
})

Multi-shot solving with externals

ctl.Add("base", nil, "#external e. a :- e.")
ctl.Ground(nil, nil)
e, _ := clingo.ID("e", true)
ctl.AssignExternal(e, clingo.TruthTrue)
ctl.Solve(nil)

Other features: StartSolve returns a *SolveHandle you can step through with Next(). SetConfig and Config read and write configuration, e.g. "solve.models". Model.Cost and Model.OptimalityProven support optimization. Parametrized program parts go through Ground([]Part{{Name: "step", Params: ...}}, nil). NewControlWithLogger captures warnings.

Notes:

  • A *Model is only valid during the Solve callback, or until the next SolveHandle.Next/Close.
  • A Control is not safe for concurrent use. Interrupt is the exception: you may call it from another goroutine.
  • Errors are returned as *clingo.Error with clingo's error code and message.

Using capi directly

capi mirrors the C API one-to-one, with the clingo_ prefix dropped and names in Go case: clingo_control_new becomes capi.ControlNew. It follows these conventions:

  • Opaque handles (*ControlT, *ModelT, *SolveHandleT, …) are plain pointers.
  • Out-parameters and arrays are slices. Pass a one-element slice and read element 0, e.g. out := make([]*capi.ControlT, 1); capi.ControlNew(args, 1, nil, nil, 20, out).
  • char const * inputs are Go strings. Returned C strings are *byte.
  • Functions return bool. On false, read ErrorCode()/ErrorMessage() on the same OS thread (runtime.LockOSThread).
  • Callbacks: pass nil for callback parameters (LoggerT, GroundCallbackT, SolveEventCallbackT, …). c-for-go keeps the first Go func you pass in a package-global, so callbacks don't work per call. Use the clingo package when you need callbacks.

See capi/capi_test.go for a full ground-and-solve example.

Regenerating the raw bindings

go install github.com/xlab/c-for-go@latest
go generate ./...

The manifest skips the parts of the C API that c-for-go can't translate cleanly: variadic clingo_ast_build, and structs of callbacks (propagators, ground program observers, scripts, applications/clingo_main, clingo_options_add). The clingo package implements callbacks itself with //export trampolines and runtime/cgo.Handle. The paths in clingo.yml assume a Homebrew install on Apple Silicon.

Tests

go test ./...