{"id":13614406,"url":"https://github.com/google/mangle","last_synced_at":"2025-05-14T03:11:03.935Z","repository":{"id":63823345,"uuid":"570159378","full_name":"google/mangle","owner":"google","description":null,"archived":false,"fork":false,"pushed_at":"2025-04-07T15:03:47.000Z","size":513,"stargazers_count":1101,"open_issues_count":12,"forks_count":39,"subscribers_count":23,"default_branch":"main","last_synced_at":"2025-04-19T22:27:28.761Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/google.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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-11-24T13:22:28.000Z","updated_at":"2025-04-15T14:05:46.000Z","dependencies_parsed_at":"2023-09-27T18:24:13.927Z","dependency_job_id":"f4ba84af-0394-4768-ac6d-8431d53e9038","html_url":"https://github.com/google/mangle","commit_stats":{"total_commits":165,"total_committers":10,"mean_commits":16.5,"dds":0.5212121212121212,"last_synced_commit":"5dbf9daf58b8289f6a576f3c9ccf7e9140997f85"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fmangle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fmangle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fmangle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fmangle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/google","download_url":"https://codeload.github.com/google/mangle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254059514,"owners_count":22007769,"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":[],"created_at":"2024-08-01T20:01:01.180Z","updated_at":"2025-05-14T03:10:58.925Z","avatar_url":"https://github.com/google.png","language":"Go","funding_links":[],"categories":["Go","Repos"],"sub_categories":[],"readme":"# Mangle\n\nMangle is a programming language for deductive database programming. It\nis an extension of Datalog, with various extensions like aggregation, function\ncalls and optional type-checking.\n\nDeductive database programming is useful for bringing data from multiple\ndata sources together since it enables us to represent and query that data in\na uniform way. It can also be used to model domain knowledge, similar\nto machine-readable ontology but without being restricted to binary\npredicates.\n\nDatalog is an expressive declarative language similar to relational calculus\n(think SQL and relational views). Unlike relational calculus, it also supports\nrecursive rules and program structuring in a straightforward way.\n\nMangle contains Datalog as a fragment and adds extensions that make its use\nmore practical. Some of the good properties like guaranteed termination are\nlost when such extensions are used.\n\nThe goal of Mangle as an open source project is to convey the concepts in\na way that is accessible to developers and lends itself to easy experimentation.\nThis repository contains an implementation of Mangle as a go library that can be\neasily embedded into applications.\n\nCheck out the [docs](docs/README.md) and the\n[GitHub discussions](https://github.com/google/mangle/discussions) for more\ninformation. There is also a Q\u0026A section.\n\nFor an example how to use Mangle library in a database-like grpc service,\nsee the separate [Mangle demo service repo](https://github.com/burakemir/mangle-service).\n\nThis is not an officially supported Google product. \n\n## Table of Contents\n- [Examples](#examples)\n- [Building](#building)\n\n## Examples\n\n### Simple Queries\n\nImagine you were asked to spot software affected by the\n[log4j vulnerability discovered in late 2021](https://www.cisa.gov/uscert/apache-log4j-vulnerability-guidance).\nWe want to look for projects that contain a Java archive (jar file) of\nlog4j that is not updated to the patched version.\n\n```prolog\nprojects_with_vulnerable_log4j(P) :-\n  projects(P),\n  contains_jar(P, \"log4j\", Version),\n  Version != \"2.17.1\",\n  Version != \"2.12.4\",\n  Version != \"2.3.2\".\n```\n\nThis is a Mangle *rule*: conceptually, the implementation retrieve all\npossible values for variables `P` and `Version` that make all the subgoals true.\n\nSimple Mangle rules like this correspond to select-project-join relational\nqueries. The same query in SQL would look like this:\n\n```sql\nSELECT projects.id as P\nFROM projects JOIN contains_jar ON projects.id = contains_jar.project_id\nWHERE contains_jar.version NOT IN (\"2.17.1\", \"2.12.4\", \"2.3.2\")\n```\n\nUnlike SQL, our Mangle rule `projects_with_vulnerable_log4j` has a name\nand can be referenced in other queries.\n\n(If translating non-recursive Datalog into SQL queries sounds interesting, you\nshould check out the [Logica](https://logica.dev/) open source project.)\n\n### Aggregation\n\nIn practice, querying is rarely enough and we also need grouping and\naggregation.\n\n```\ncount_projects_with_vulnerable_log4j(Num) :-\n  projects_with_vulnerable_log4j(P) |\u003e do fn:group_by(), let Num = fn:Count().\n```\n\n### Recursive Queries\n\nThe example does not specify what `contains_jar` does. Here is a possible\nimplementation for `contains_jar` that walks a dependency graph.\nThis shows that Mangle rules can be recursive. \n\n```\ncontains_jar(P, Name, Version) :-\n  contains_jar_directly(P, Name, Version).\n\ncontains_jar(P, Name, Version) :-\n  project_depends(P, Q),\n  contains_jar(Q, Name, Version).\n```\n\nThe two rules correspond to two cases in which a project may \"contain\" a jar:\neither directly, or through some dependency.\n\n### Knowledge Graphs, Property Graphs\n\nIn requirements engineering, one needs to captures real world concepts in a\ndomain model and controlled vocabulary. Description logics use\nroles to describe how concepts interact, but these relationships are always\nbinary. Mangle can represent binary predicates, but also arbitrary n-ary\nrelations. Moreover it also has support for structured data.\n\n```\none_or_two_leg_trip(Codes, Start, Destination, Price) :-\n  direct_conn(Code, Start, Destination, Price)\n  |\u003e let Codes = [Code].\n\none_or_two_leg_trip(Codes, Start, Destination, Price) :-\n  direct_conn(FirstCode, Start, Connecting, FirstLegPrice).\n  direct_conn(SecondCode, Connecting, Destination, SecondLegPrice)\n  |\u003e let Code = [FirstCode, SecondCode],\n     let Price = fn:plus(FirstLegPrice, SecondLegPrice).\n\n```\n\n```mermaid\ngraph LR\n    /zurich --\u003e|/code/ZL \u003cbr /\u003e 60 CHF| /lausanne\n    /zurich --\u003e|/code/ZB \u003cbr /\u003e 30 CHF| /bern\n    /bern --\u003e|/code/BL \u003cbr /\u003e 30 CHF| /lausanne\n```\n\n## Building \u0026 Testing\n\nGet the dependencies (see [go.mod](go.mod)), build the library, run tests:\n\n```\ngo get -t ./...\ngo build ./...\ngo test ./...\n```\n\n### Regenerating the parser sources\n\nIf you want to regenerate the parser sources, you need to set up ANTLR first.\nThis requires a Java runtime environment.\n\n```\nwget http://www.antlr.org/download/antlr-4.13.2-complete.jar\nalias antlr='java -jar $PWD/antlr-4.13.2-complete.jar'\nantlr -Dlanguage=Go -package gen -o ./ parse/gen/Mangle.g4 -visitor\n```\n\n## Contributing\n\nThe Mangle maintainers welcome external contributions to spec, documentation\nand this implementation (see [CONTRIBUTING.md](CONTRIBUTING.md)) and also other\nimplementations. Pull requests will be handled\n[like for tensorflow](https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md),\nto ensure our internal usage and tests will pass. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fmangle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoogle%2Fmangle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fmangle/lists"}