mangle

module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2023 License: Apache-2.0

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 docs and the GitHub discussions for more information. There is also a Q&A section.

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 <br /> 60 CHF| /lausanne
    /zurich -->|/code/ZB <br /> 30 CHF| /bern
    /bern -->|/code/BL <br /> 30 CHF| /lausanne

Building

Get the dependencies (see go.mod) and build the library:

go get ./...
go build ./...
Regenerating the parser sources

If you want to regenerate the parser sources, you need to set up ANTLR first. This requires Java runtime environment. This should only be necessary if you require a different version of the antlr runtime for some reason.

wget http://www.antlr.org/download/antlr-4.11.1-complete.jar
alias antlr='java -jar $PWD/antlr-4.11.1-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.

Directories

Path Synopsis
Package analysis contains methods that check whether each datalog clause is valid, and whether a set of valid clauses forms a valid datalog program.
Package analysis contains methods that check whether each datalog clause is valid, and whether a set of valid clauses forms a valid datalog program.
Package ast contains abstract syntax tree representations of Mangle code.
Package ast contains abstract syntax tree representations of Mangle code.
Package builtin contains functions for evaluating built-in predicates.
Package builtin contains functions for evaluating built-in predicates.
Package engine contains an implementation of the semi-naive evaluation strategy for datalog programs.
Package engine contains an implementation of the semi-naive evaluation strategy for datalog programs.
Package factstore contains the interface and a simple implementation for access to facts (atoms that are ground, i.e.
Package factstore contains the interface and a simple implementation for access to facts (atoms that are ground, i.e.
Package functional provides evaluation of function expressions.
Package functional provides evaluation of function expressions.
Package interpreter provides functions for an interactive interpreter.
Package interpreter provides functions for an interactive interpreter.
main
Binary shell is a shell for interactive interpreter.
Binary shell is a shell for interactive interpreter.
Package json2struct contains code to convert JSON objects to Mangle structs.
Package json2struct contains code to convert JSON objects to Mangle structs.
Package packages provides functionality for creating datalog packages.
Package packages provides functionality for creating datalog packages.
Package parse provides methods to parse datalog programs and parts thereof.
Package parse provides methods to parse datalog programs and parts thereof.
gen
Package proto2struct contains code to convert protocol buffers to Mangle structs.
Package proto2struct contains code to convert protocol buffers to Mangle structs.
Package rewrite rewrites rules of a layer (stratum) of a datalog program.
Package rewrite rewrites rules of a layer (stratum) of a datalog program.
Package symbols contains symbols for built-in functions and predicates.
Package symbols contains symbols for built-in functions and predicates.
Package unionfind is an implementation of Union-Find for use in unification.
Package unionfind is an implementation of Union-Find for use in unification.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL