Skip to content

Instantly share code, notes, and snippets.

@christianromney
Created April 17, 2019 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianromney/f435aab4c437e516c164467cf8114929 to your computer and use it in GitHub Desktop.
Save christianromney/f435aab4c437e516c164467cf8114929 to your computer and use it in GitHub Desktop.
Exploring Racket's Datalog implementation
#lang datalog
% -- facts -- %
female(georgene).
female(randi).
female(nikki).
female(jenny).
female(jen).
female(taylor).
male(randy).
male(leslie).
male(wesley).
male(christian).
male(sebastian).
male(rob).
parent(jenny, christian).
parent(leslie, christian).
parent(christian, sebastian).
parent(christian, nikki).
parent(randi, nikki).
parent(randi, sebastian).
parent(georgene, randi).
parent(randy, randi).
parent(georgene, rob).
parent(randy, rob).
parent(rob, taylor).
parent(jen, taylor).
% -- rules -- %
father(X, Y) :-
male(X),
parent(X, Y).
mother(X, Y) :-
female(X),
parent(X, Y).
mom(X, Y) :-
mother(X, Y).
dad(X, Y) :-
father(X, Y).
spouse(X, Y) :-
parent(X, C),
parent(Y, C),
X != Y.
son(X, Y) :-
male(X),
parent(Y, X).
daughter(X, Y) :-
female(X),
parent(Y, X).
child(X, Y) :-
parent(Y, X).
grandparent(X, Y) :-
parent(X, Z),
parent(Z, Y).
grandfather(X, Y) :-
father(X, Z),
parent(Z, Y).
grandmother(X, Y) :-
mother(X, Z),
parent(Z, Y).
grandchild(X, Y) :-
child(X, Z),
child(Z, Y).
grandson(X, Y) :-
male(X),
grandchild(X, Y).
granddaughter(X, Y) :-
female(X),
grandchild(X, Y).
sibling(X, Y) :-
parent(Z, X),
parent(Z, Y),
X != Y.
parent-sibling(X, Y) :-
parent(P, Y),
sibling(P, X).
aunt(X, Y) :-
parent-sibling(X, Y),
female(X).
uncle(X, Y) :-
parent-sibling(X, Y),
male(X).
% spouse's siblings
sibling-in-law(X, Y) :-
spouse(X, S),
sibling(S, Y).
% sibling's spouses
sibling-in-law(X, Y) :-
sibling(X, S),
spouse(S, Y).
parent-in-law(X, Y) :-
spouse(X, S),
parent(Y, S).
in-law(X, Y) :-
parent-in-law(X, Y).
in-law(X, Y) :-
sibling-in-law(X, Y).
brother-in-law(X, Y) :-
sibling-in-law(X, Y),
male(Y).
sister-in-law(X, Y) :-
sibling-in-law(X, Y),
female(Y).
% -- queries / tests -- %
sister-in-law(randi, S)?
sister-in-law(jen, S)?
brother-in-law(christian, S)?
brother-in-law(rob, S)?
aunt(R, taylor)?
uncle(R, nikki)?
sibling(nikki, S)?
mom(M, christian)?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment