{"id":27775279,"url":"https://github.com/trealla-prolog/go","last_synced_at":"2025-04-30T03:11:08.813Z","repository":{"id":54309128,"uuid":"522100385","full_name":"trealla-prolog/go","owner":"trealla-prolog","description":"Trealla Prolog embedded in Go using WASM","archived":false,"fork":false,"pushed_at":"2025-04-21T05:21:31.000Z","size":119773,"stargazers_count":79,"open_issues_count":4,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-30T03:09:51.824Z","etag":null,"topics":["go","logic-programming","prolog","trealla-prolog","wasm"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trealla-prolog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-08-07T02:42:19.000Z","updated_at":"2025-04-21T05:14:00.000Z","dependencies_parsed_at":"2023-10-26T08:36:11.048Z","dependency_job_id":"643940ec-4489-41ad-b440-e2d2c811c512","html_url":"https://github.com/trealla-prolog/go","commit_stats":null,"previous_names":["guregu/trealla-go","trealla-prolog/trealla-go"],"tags_count":224,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trealla-prolog%2Fgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trealla-prolog%2Fgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trealla-prolog%2Fgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trealla-prolog%2Fgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trealla-prolog","download_url":"https://codeload.github.com/trealla-prolog/go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251632511,"owners_count":21618628,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","logic-programming","prolog","trealla-prolog","wasm"],"created_at":"2025-04-30T03:11:07.930Z","updated_at":"2025-04-30T03:11:08.801Z","avatar_url":"https://github.com/trealla-prolog.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# trealla-go [![GoDoc](https://godoc.org/github.com/trealla-prolog/go?status.svg)](https://godoc.org/github.com/trealla-prolog/go)\n`import \"github.com/trealla-prolog/go/trealla\"`\n\nProlog interface for Go using [Trealla Prolog](https://github.com/trealla-prolog/trealla) and [wazero](https://wazero.io/).\nIt's pretty fast. Not as fast as native Trealla, but pretty dang fast (about 2x slower than native).\n\n**Development Status**: inching closer to stability\n\n### Caveats\n\n- Beta status, \u003cs\u003eAPI will probably change\u003c/s\u003e\n  - API is relatively stable now.\n\n## Install\n\n```bash\ngo get github.com/trealla-prolog/go\n```\n\n**Note**: the module is under `github.com/trealla-prolog/go`, **not** `[...]/go/trealla`.\ngo.dev is confused about this and will pull a very old version if you try to `go get` the `trealla` package.\n\n## Usage\n\nThis library uses WebAssembly to run Trealla, executing Prolog queries in an isolated environment.\n\n```go\nimport \"github.com/trealla-prolog/go/trealla\"\n\nfunc main() {\n\t// load the interpreter and (optionally) grant access to the current directory\n\tpl, err := trealla.New(trealla.WithPreopenDir(\".\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t// run a query; cancel context to abort it\n\tctx := context.Background()\n\tquery := pl.Query(ctx, \"member(X, [1, foo(bar), c]).\")\n\n\t// calling Close is not necessary if you iterate through the whole result set\n\t// but it doesn't hurt either\n\tdefer query.Close()\n\n\t// iterate through answers\n\tfor query.Next(ctx) {\n\t\tanswer := query.Current()\n\t\tx := answer.Solution[\"X\"]\n\t\tfmt.Println(x) // 1, trealla.Compound{Functor: \"foo\", Args: [trealla.Atom(\"bar\")]}, \"c\"\n\t}\n\n\t// make sure to check the query for errors\n\tif err := query.Err(); err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\n### Single query\n\nUse `QueryOnce` when you only want a single answer.\n\n```go\npl := trealla.New()\nanswer, err := pl.QueryOnce(ctx, \"succ(41, N).\")\nif err != nil {\n\tpanic(err)\n}\n\nfmt.Println(answer.Stdout)\n// Output: hello world\n```\n\n### Binding variables\n\nYou can bind variables in the query using the `WithBind` and `WithBinding` options.\nThis is a safe and convenient way to pass data into the query.\nIt is OK to pass these multiple times.\n\n```go\npl := trealla.New()\nanswer, err := pl.QueryOnce(ctx, \"write(X)\", trealla.WithBind(\"X\", trealla.Atom(\"hello world\")))\nif err != nil {\n\tpanic(err)\n}\n\nfmt.Println(answer.Stdout)\n// Output: hello world\n```\n\n### Scanning solutions\n\nYou can scan an answer's substitutions directly into a struct or map, similar to ichiban/prolog.\n\nUse the `prolog:\"VariableName\"` struct tag to manually specify a variable name.\nOtherwise, the field's name is used.\n\n```go\nanswer, err := pl.QueryOnce(ctx, `X = 123, Y = abc, Z = [\"hello\", \"world\"].`)\nif err != nil {\n\tpanic(err)\n}\n\nvar result struct {\n\tX  int\n\tY  string\n\tHi []string `prolog:\"Z\"`\n}\n// make sure to pass a pointer to the struct!\nif err := answer.Solution.Scan(\u0026result); err != nil {\n\tpanic(err)\n}\n\nfmt.Printf(\"%+v\", result)\n// Output: {X:123 Y:abc Hi:[hello world]}\n```\n\n#### Struct compounds\n\nProlog compounds can destructure into Go structs. A special field of type `trealla.Functor` will be set to the functor.\nThe compound's arguments are matched with the exported struct fields in order.\nThese structs can also be used to bind variables in queries.\n\n```prolog\n?- findall(kv(Flag, Value), current_prolog_flag(Flag, Value), Flags).\n   Flags = [kv(double_quotes,chars),kv(char_conversion,off),kv(occurs_check,false),kv(character_escapes,true),...]\n```\n\n```go\n// You can embed trealla.Functor to represent Prolog compounds using Go structs.\n\n// kv(Flag, Value)\ntype pair struct {\n\ttrealla.Functor `prolog:\"-/2\"` // tag is optional, but can be used to specify the functor/arity\n\tFlag            trealla.Atom   // 1st arg\n\tValue           trealla.Term   // 2nd arg\n}\nvar result struct {\n\tFlags []pair // Flags variable\n}\n\nctx := context.Background()\npl, err := trealla.New()\nif err != nil {\n\tpanic(err)\n}\nanswer, err := pl.QueryOnce(ctx, `\n\tfindall(Flag-Value, (member(Flag, [double_quotes, encoding, max_arity]), current_prolog_flag(Flag, Value)), Flags).\n`)\nif err != nil {\n\tpanic(err)\n}\nif err := answer.Solution.Scan(\u0026result); err != nil {\n\tpanic(err)\n}\nfmt.Printf(\"%v\\n\", result.Flags)\n// Output: [{- double_quotes chars} {- encoding 'UTF-8'} {- max_arity 255}]\n```\n\n## Documentation\n\nSee **[package trealla's documentation](https://pkg.go.dev/github.com/trealla-prolog/go#section-directories)** for more details and examples.\n\n## Builtins\n\nThese additional predicates are built in:\n\n- `crypto_data_hash/3`\n- `http_consult/1`\n  - Argument can be URL string, or `my_module_name:\"https://url.example\"`\n\n## WASM binary\n\nThis library embeds the Trealla WebAssembly binary in itself, so you can use it without any external dependencies.\nThe binaries are currently sourced from [guregu/trealla](https://github.com/guregu/trealla).\n\n### Building the WASM binary\n\nIf you'd like to build `libtpl.wasm` yourself:\n\n```bash\n# The submodule in src/trealla points to the current version\ngit submodule update --init --recursive\n# Use the Makefile in the root of this project\nmake clean wasm\n# Run the tests and benchmark to make sure it works\ngo test -v ./trealla -bench=.\n```\n\n## Thanks\n\n- Andrew Davison ([@infradig](https://github.com/infradig)) and other contributors to [Trealla Prolog](https://github.com/trealla-prolog/trealla).\n- Nuno Cruces ([@ncruces](https://github.com/ncruces)) for the wazero port.\n- Jos De Roo ([@josd](https://github.com/josd)) for test cases and encouragement.\n- Aram Panasenco ([@panasenco](https://github.com/panasenco)) for his JSON library.\n\n## License\n\nMIT. See ATTRIBUTION as well.\n\n## See also\n\n- [trealla-js](https://github.com/guregu/trealla-js) is Trealla for Javascript.\n- [ichiban/prolog](https://github.com/ichiban/prolog) is a pure Go Prolog.\n- [guregu/pengine](https://github.com/guregu/pengine) is a Pengines (SWI-Prolog) library for Go.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrealla-prolog%2Fgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrealla-prolog%2Fgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrealla-prolog%2Fgo/lists"}