MCPcopy
hub / github.com/google/mangle

github.com/google/mangle @v0.5.0 sqlite

repository ↗ · DeepWiki ↗ · release v0.5.0 ↗
1,991 symbols 7,054 edges 88 files 894 documented · 45%
README

Mangle

Mangle is a programming language for deductive database programming. It is an extension of Datalog, with various extensions like aggregation, function calls and optional type-checking.

Deductive database programming is useful for bringing data from multiple data sources together since it enables us to represent and query that data in a uniform way. It can also be used to model domain knowledge, similar to machine-readable ontology but without being restricted to binary predicates.

Datalog is an expressive declarative language similar to relational calculus (think SQL and relational views). Unlike relational calculus, it also supports recursive rules and program structuring in a straightforward way.

Mangle contains Datalog as a fragment and adds extensions that make its use more practical. Some of the good properties like guaranteed termination are lost when such extensions are used.

The goal of Mangle as an open source project is to convey the concepts in a way that is accessible to developers and lends itself to easy experimentation. This repository contains an implementation of Mangle as a go library that can be easily embedded into applications.

Check out the users documentation and the GitHub discussions for more information. There is also a Q&A section.

For an example how to use Mangle library in a database-like grpc service, see the separate Mangle demo service repo.

This is not an officially supported Google product.

Table of Contents

Examples

Simple Queries

Imagine you were asked to spot software affected by the log4j vulnerability discovered in late 2021. We want to look for projects that contain a Java archive (jar file) of log4j that is not updated to the patched version.

projects_with_vulnerable_log4j(P) :-
  projects(P),
  contains_jar(P, "log4j", Version),
  Version != "2.17.1",
  Version != "2.12.4",
  Version != "2.3.2".

This is a Mangle rule: conceptually, the implementation retrieve all possible values for variables P and Version that make all the subgoals true.

Simple Mangle rules like this correspond to select-project-join relational queries. The same query in SQL would look like this:

SELECT projects.id as P
FROM projects JOIN contains_jar ON projects.id = contains_jar.project_id
WHERE contains_jar.version NOT IN ("2.17.1", "2.12.4", "2.3.2")

Unlike SQL, our Mangle rule projects_with_vulnerable_log4j has a name and can be referenced in other queries.

(If translating non-recursive Datalog into SQL queries sounds interesting, you should check out the Logica open source project.)

Aggregation

In practice, querying is rarely enough and we also need grouping and aggregation.

count_projects_with_vulnerable_log4j(Num) :-
  projects_with_vulnerable_log4j(P) |> do fn:group_by(), let Num = fn:Count().

Recursive Queries

The example does not specify what contains_jar does. Here is a possible implementation for contains_jar that walks a dependency graph. This shows that Mangle rules can be recursive.

contains_jar(P, Name, Version) :-
  contains_jar_directly(P, Name, Version).

contains_jar(P, Name, Version) :-
  project_depends(P, Q),
  contains_jar(Q, Name, Version).

The two rules correspond to two cases in which a project may "contain" a jar: either directly, or through some dependency.

Knowledge Graphs, Property Graphs

In requirements engineering, one needs to captures real world concepts in a domain model and controlled vocabulary. Description logics use roles to describe how concepts interact, but these relationships are always binary. Mangle can represent binary predicates, but also arbitrary n-ary relations. Moreover it also has support for structured data.

one_or_two_leg_trip(Codes, Start, Destination, Price) :-
  direct_conn(Code, Start, Destination, Price)
  |> let Codes = [Code].

one_or_two_leg_trip(Codes, Start, Destination, Price) :-
  direct_conn(FirstCode, Start, Connecting, FirstLegPrice).
  direct_conn(SecondCode, Connecting, Destination, SecondLegPrice)
  |> let Code = [FirstCode, SecondCode],
     let Price = fn:plus(FirstLegPrice, SecondLegPrice).

graph LR
    /zurich -->|/code/ZL 

 60 CHF| /lausanne
    /zurich -->|/code/ZB 

 30 CHF| /bern
    /bern -->|/code/BL 

 30 CHF| /lausanne

Temporal Knowledge Graphs

In many real-world scenarios, facts are not static but have a validity period. Mangle supports temporal reasoning, allowing facts to be associated with time intervals. This enables querying not just what is true, but when it is true.

# A network link is active for a specific duration
Decl link(X, Y) temporal bound [/name, /name].

# When can we reach Y from X?
# Only when the links are active simultaneously.
reachable(X, Y)@[T] :- link(X, Y)@[T].
reachable(X, Z)@[T] :- reachable(X, Y)@[T], link(Y, Z)@[T].

See the Temporal Reasoning documentation for more details.

Talks

  • From Facts to Theories: Burak Emir's talk at REBASE 2025 discusses how Mangle bridges the gap between raw data and logical theories. Watch on YouTube.

Building & Testing

Get the dependencies (see go.mod), build the library, run tests:

go get -t ./...
go build ./...
go test ./...

Regenerating the parser sources

If you want to regenerate the parser sources, you need to set up ANTLR first. This requires a Java runtime environment.

wget http://www.antlr.org/download/antlr-4.13.2-complete.jar
alias antlr='java -jar $PWD/antlr-4.13.2-complete.jar'
antlr -Dlanguage=Go -package gen -o ./ parse/gen/Mangle.g4 -visitor

Contributing

The Mangle maintainers welcome external contributions to spec, documentation and this implementation (see CONTRIBUTING.md) and also other implementations. Pull requests will be handled like for tensorflow, to ensure our internal usage and tests will pass.

Star History

Star History Chart

Extension points exported contracts — how you extend this code

ReadOnlyFactStore (Interface)
ReadOnlyFactStore provides read access to a set of facts. [9 implementers]
factstore/factstore.go
Term (Interface)
Term represents the building blocks of datalog programs, namely constants, variables, atoms, and also negated atoms, equ [9 …
ast/ast.go
ReadOnlyTemporalFactStore (Interface)
ReadOnlyTemporalFactStore provides read access to temporal facts. [2 implementers]
factstore/temporal.go
MangleVisitor (Interface)
A complete Visitor for a parse tree produced by MangleParser. [2 implementers]
parse/gen/mangle_visitor.go
ExternalPredicateCallback (Interface)
ExternalPredicateCallback is used to query external data sources. An atom `mydb(input1, ..., inputN, OutputVar1, ..., O [1 …
engine/seminaivebottomup.go
FactStore (Interface)
FactStore provides access to a set of facts. [10 implementers]
factstore/factstore.go
BaseTerm (Interface)
BaseTerm represents a subset of terms: constant, variables or ApplyFn. Every BaseTerm will implement Term. [3 implementers]
ast/ast.go
TemporalFactStore (Interface)
TemporalFactStore provides access to temporal facts. [2 implementers]
factstore/temporal.go

Core symbols most depended-on inside this repo

NewAtom
called by 398
ast/ast.go
Number
called by 371
ast/ast.go
String
called by 270
ast/ast.go
Equals
called by 164
ast/ast.go
Date
called by 130
ast/temporal.go
String
called by 100
ast/ast.go
Add
called by 76
factstore/factstore.go
NewRelType
called by 73
symbols/typeexprs.go

Shape

Method 1,046
Function 787
Struct 111
Interface 31
TypeAlias 14
FuncType 2

Languages

Go100%

Modules by API surface

parse/gen/mangle_parser.go592 symbols
ast/ast.go141 symbols
factstore/factstore.go83 symbols
parse/gen/mangle_base_listener.go65 symbols
parse/gen/mangle_listener.go61 symbols
parse/parse.go54 symbols
functional/functional_test.go51 symbols
factstore/temporal.go51 symbols
ast/temporal.go47 symbols
builtin/builtin_test.go39 symbols
symbols/typeexprs.go38 symbols
analysis/validation.go36 symbols

Dependencies from manifests, versioned

bitbucket.org/creachadair/stringsetv0.0.11 · 1×
github.com/antlr4-go/antlr/v4v4.13.1 · 1×
github.com/chzyer/readlinev1.5.1 · 1×
github.com/golang/glogv1.2.4 · 1×
go.uber.org/multierrv1.11.0 · 1×
golang.org/x/expv0.0.0-2024070723363 · 1×
golang.org/x/sysv0.22.0 · 1×
google.golang.org/protobufv1.34.0 · 1×

For agents

$ claude mcp add mangle \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact