https://github.com/google/mangle Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code + Explore + All features + Documentation + GitHub Skills + Blog * Solutions + For + Enterprise + Teams + Startups + Compare all + By Solution + CI/CD & Automation + DevOps + DevSecOps + Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles + Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this organization All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} google / mangle Public * Notifications * Fork 1 * Star 41 License Apache-2.0 license 41 stars 1 fork Star Notifications * Code * Issues 1 * Pull requests 1 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights google/mangle This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 1 branch 0 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/g] Use Git or checkout with SVN using the web URL. [gh repo clone google] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @burakemir Mangle Team and burakemir Internal change ... c31020e Nov 25, 2022 Internal change PiperOrigin-RevId: 490915335 c31020e Git stats * 1 commit Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time analysis ast builtin docs engine examples factstore interpreter packages parse rewrite symbols unionfind CONTRIBUTING.md LICENSE README.md go.mod View code [ ] Mangle Table of Contents Examples Simple Queries Aggregation Recursive Queries Knowledge Graphs, Property Graphs Building Contributing README.md 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 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 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. This is not an officially supported Google product. 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 use cases and tests continue to work. Table of Contents * Examples * Building(#building) Examples Simple Queries Imagine you were asked to spot software affected by the log4j vulnerability discovered in late 2021. We do this by looking for projects that contain a Java archive (jar file) of log4j that is not 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 When engineering requirements, it is useful to model a slice of the real world through a domain model and controlled vocabulary. Description logics use roles to describe how concepts interact, but the relationships are always binary. Mangle can represent binary predicates, but also arbitrary n-ary relations. one_or_two_leg_flight(Codes, Start, Destination, Price) :- direct_flight(Code, Start, Destination, Price) |> let Codes = fn:list(Code). one_or_two_leg_flight(Codes, Start, Destination, Price) :- direct_flight(FirstCode, Start, FirstStop, FirstLegPrice). direct_flight(SecondCode, FirstStop, Destination, SecondLegPrice) |> let Codes = [FirstCode, SecondCode], let Price = fn:sum(FirstLegPrice, SecondLegPrice). graph LR A[Square Rect] -- Link text --> B((Circle)) A --> C(Round Rect) B --> D{Rhombus} C --> D Building If you want to build from source develop extensions, you need to set up ANTLR first, which requires Java runtime environment. wget http://www.antlr.org/download/antlr-4.11.1-complete.jar alias antlr='java -jar $PWD/antlr-4.11.1-complete.jar' Then you can generate the parser sources antlr -Dlanguage=Go -package gen -o ./ parse/gen/Mangle.g4 -visitor ... and finally build the library: go build ... Contributing This project is used in an internal application. Pull requests will be handled by merging into internal repository. About No description, website, or topics provided. Resources Readme License Apache-2.0 license Code of conduct Code of conduct Security policy Security policy Stars 41 stars Watchers 4 watching Forks 1 fork Releases No releases published Packages 0 No packages published Languages * Go 99.1% * ANTLR 0.9% Footer (c) 2022 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.