Skip to content

Instantly share code, notes, and snippets.

@ggb ggb/sicp44.dl
Created Mar 21, 2017

What would you like to do?
Database and solutions from "Structure and Interpretation of Computer Programs", Chapter 4.4
#lang datalog
town(ben, slumerville).
street(ben, ridge_road).
str_number(ben, 10).
department(ben, computer).
position(ben, guru).
salary(ben, 60000).
town(alyssa, cambridge).
street(alyssa, mass_ave).
str_number(alyssa, 78).
department(alyssa, computer).
position(alyssa, programmer).
salary(alyssa, 40000).
boss(alyssa, ben).
town(cy, cambridge).
street(cy, ames_street).
str_number(cy, 3).
department(cy, computer).
position(cy, programmer).
salary(cy, 35000).
boss(cy, ben).
town(lem, boston).
street(lem, bay_state_road).
str_number(lem, 22).
department(lem, computer).
position(lem, technician).
salary(lem, 25000).
boss(lem, ben).
town(louis, slumerville).
street(louis, pine_tree_road).
str_number(louis, 80).
department(louis, computer).
position(louis, programmer).
level(louis, trainee).
salary(louis, 30000).
boss(louis, alyssa).
boss(ben, oliver).
town(oliver, swellesley).
street(oliver, top_heap_road).
department(oliver, management).
position(oliver, boss).
salary(oliver, 150000).
town(eben, weston).
street(eben, shady_lane).
str_number(eben, 10).
department(eben, accounting).
position(eben, accountant).
salary(eben, 75000).
boss(eben, oliver).
town(robert, allston).
street(robert, havard_street).
str_number(robert, 16).
department(robert, accounting).
position(robert, scribe).
salary(robert, 18000).
boss(robert, eben).
town(dewitt, slumerville).
street(dewitt, onion_square).
str_number(dewitt, 5).
department(dewitt, management).
position(dewitt, secretary).
salary(dewitt, 25000).
boss(dewitt, oliver).
computer_programmer(X) :-
department(X, computer),
position(X, programmer).
% Exercise 4.55
ben_boss(X) :-
boss(X, ben).
all_accounting(X, Y) :-
department(X, accounting),
position(X, Y).
address(N, X, Y, Z) :-
town(N, X),
street(N, Y),
str_number(N, Z).
all_in_slumerville(X, Y, Z) :-
address(X, slumerville, Y, Z).
% Exercise 4.56
all_boss_ben(N, X, Y, Z) :-
address(N, X, Y, Z).
% Not possible in Racket-Datalog because "<" is not supported :-/
%salary_lower_ben(N, S) :-
% salary(ben, X),
% salary(N, S),
% S < Y.
not_computer_department(X, N, P) :-
boss(X, N),
department(N, D),
D != computer,
position(N, P).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.