- Go 83.4%
- C 16.6%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
- 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> |
||
| capi | ||
| .gitignore | ||
| bridge.c | ||
| bridge.h | ||
| callbacks.go | ||
| clingo.go | ||
| clingo.yml | ||
| clingo_test.go | ||
| control.go | ||
| example_test.go | ||
| generate.go | ||
| go.mod | ||
| README.md | ||
| solve.go | ||
| symbol.go | ||
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
*Modelis only valid during theSolvecallback, or until the nextSolveHandle.Next/Close. - A
Controlis not safe for concurrent use.Interruptis the exception: you may call it from another goroutine. - Errors are returned as
*clingo.Errorwith 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 Gostrings. Returned C strings are*byte.- Functions return
bool. Onfalse, readErrorCode()/ErrorMessage()on the same OS thread (runtime.LockOSThread). - Callbacks: pass
nilfor 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 theclingopackage 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 ./...