Skip to content

Instantly share code, notes, and snippets.

@andreabedini
Created December 15, 2022 08:18
Show Gist options
  • Save andreabedini/4d58336a01d8ae2e28e6903729863760 to your computer and use it in GitHub Desktop.
Save andreabedini/4d58336a01d8ae2e28e6903729863760 to your computer and use it in GitHub Desktop.
plan-to-datalog.jq
#!/usr/bin/env -S jq -f -r
#
# This jq scripts transforms cabal's plan.json file into a series of fact that souffle can interpret.
#
# Example use
#
# $ ~/plan-to-datalog.jq <dist-newstyle/cache/plan.json >plan.dl
#
# Then create a file query.dl
#
# #include "plan.dl"
#
# .decl pkg_id_depending_on_srp(id : symbol, id_srp : symbol, repo : symbol)
#
# pkg_id_depending_on_srp(id, id_srp, repo) :-
# pkg_src_type(id_srp, "source-repo"),
# pkg_src_source_repo_location(id, repo),
# depends(id, id_srp).
#
# pkg_id_depending_on_srp(id1, id2, repo) :-
# depends(id1, x),
# pkg_id_depending_on_srp(x, id2, repo).
#
# .decl pkg_depending_on_srp(name : symbol, name_srp : symbol, repo : symbol)
# .output pkg_depending_on_srp(IO=stdout)
#
# pkg_depending_on_srp(name, name_srp, repo) :-
# pkg_id_depending_on_srp(id, id_srp, repo),
# pkg_name(id, name),
# pkg_name(id_srp, name_srp).
#
# And run with
#
# $ souffle query.dl
def words: join(" ");
def lines: join("\n");
def strip_arrays: [paths(arrays)] as $arrays | delpaths($arrays);
def get_columns(a):
a
| map([ strip_arrays | paths(scalars)])
| flatten(1)
| unique
| map(select(first != "flags" and first != "components" and first != "id"))
;
def make_preamble(columns):
columns[]
| (join("-") | split("-") | join("_")) as $c
| [".decl", $c, "(id: symbol, value: symbol)"]
| words
;
def make_facts(columns):
[ columns[] as $path
| getpath($path) as $v
| select($v != null)
| [ ($path | join("-") | split("-") | join("_")), "(\"", .id, "\", \"", $v, "\")." ]
| join("")
]
;
def make_dependencies(plan):
plan[]
| paths(arrays) as $path
| getpath($path)[] as $dst
| [ "depends(\"", .id, "\", \"", $dst, "\")." ]
| join("")
;
."install-plan" as $plan
| get_columns($plan) as $columns
| [make_preamble($columns)] as $preable
| [$plan[] | make_facts($columns)[]] as $facts
| [make_dependencies($plan)] as $deps
| [$preable[], $facts[], ".decl depends(a: symbol, b: symbol)", $deps[]]
| lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment