A Brief Introduction to Datalog++
by K. Yue, January 31, 2000 Datalog++ Example #1:

Class Definitions:

//    No class needed.
//    A dummy is used because empty class file is not accepted.
CLASS dummy
{
    instance signatures
    {
        //  name: public value (fact) with one argument.
        pub,val name/1;
    }
}

Object Definitions:

boss(X,Y) :- supervise(X,Y);
boss(X,Y) :- supervise(X,Z), boss(Z,Y);

supervise('joe','mary');
supervise('mary','peter');
supervise('mary','paul');
supervise('paul','stan');
supervise('paul','lee');

Queries

boss('mary','joe');
boss('mary','stan');
boss(X,'lee');
boss('joe',Y);
boss(A,B);

Datalog++ Example #2: (adaped from the XSB site).

Class Definitions:

// This is the schema of a demo deductive object-oriented database.
// Classes grad_stud, faculty, and gta model graduate student,
// faculty, and graduate teaching assistant respectively. Class gta
// inherits from classes grad_stud and faculty.

CLASS grad_stud
{
    instance signatures
    {
        //  name: public value (fact) with one argument.
        pub,val name/1;
        pub,val sid/1;
        priv,val stipend/1;
        //  income: public code (rule) with one argument.
        pub,code income/1;
    }
}

CLASS faculty
{
    instance signatures
    {
        pub,val name/1;
        pub,val eid/1;
        priv,val salary/1;
        pub,code income/1;
    }
}

CLASS gta subclass of {grad_stud,faculty}
{
    instance signatures
    {
        priv,val taship/1;
    }
    //    Do not inherit salary and income from faculty.
    controls
    {
    reject sig salary/1 from faculty;
    reject sig income/1 from faculty;
    }
}

Object Definitions

// This object definition file defines objects and their methods
// in a demo deductive object-oriented database.

// class methods
grad_stud.stipend(12000);
grad_stud.income(X) :- stipend(X);

// instances
//  object declaration.
joe: grad_stud;

joe.stipend(15000);
kelly : grad_stud;
//    Kelly income is a call of the salary of john.
kelly.income(X) :- john<<salary(X);

// class methods
faculty.salary(60000);
faculty.income(X) :- salary(X);

// instances

john : faculty;
john.name('john');
max : faculty;
max.salary(75000);

// class methods
gta.taship(16000);
gta.income(X) :- stipend(S), taship(T), X is S+T;

 // instances
sally : gta;
sue : gta;
sally.taship(20000);
 

// global methods
// faculty john teaches grad_stud joe
// with the help of gta sally.
teaches(john, sally, joe);

Queries:

// This query file defines queries to a demo deductive object-oriented database.
john<<income(X);
X<<income(Y);
joe<<income(Z);
joe<<income(15000);
stipend(X);

Output:

[sysinitrc loaded]
[dbgrp loaded]
[aggregs loaded]
[rulebase loaded]
[AAAa001ju.tempf1 compiled, cpu time used: 1.4420 seconds]
[AAAa001ju.tempf1 loaded]
Loaded schema successfully.
% Specialising partially instantiated calls to meth/4
[AAAa001ju.tempf2 compiled, cpu time used: 2.1490 seconds]
[AAAa001ju.tempf2 loaded]
Loaded object successfully.

***answer to your query 1
60000

***answer to your query 2

12000 grad_stud
15000 joe
60000 faculty
60000 john
75000 max
28000 gta
32000 sally
28000 sue

***answer to your query 3
15000
 
***answer to your query 4
YES

***answer to your query 5
Undefined predicate/function: stipend/1
Aborting...

Short Recap Example:

(1)    john<<income(X);

1.1    john : faculty;
1.2    john<<income(X) not directly defined.
1.3    faculty<<income(X) used.
1.4    faculty.income(X) :- salary(X);
1.5    faculty.salary(60000);

Thus, list X as 60000.

(2)    X<<income(Y);
1.1    Find eight objects X with method income Y. (Note that classes are also objects).
1.2    Compute income for each object and return the values of (Y,X) pairs.

Example: for

32000 sally

1.    sally.income(X) not directly defined.
2.    use gta.income(X) :- stipend(S), taship(T), X is S+T;  That is:
        sally.income(X) :- stipend(S), taship(T), X is S+T;
3.    Find fact: sally.taship(20000); (gta.taship(16000 not used).
4.    sally.stipend(S) not directly defined.
5.    directly superclass of sally is gta, but gta.stipend(S) not directly defined.
6.    directly superclass of gta isgrad_stud and find grad_stud.stipend(12000);
7.    Thus sally.stipend(12000) is used.
8.    Thus, for sally.income(X) :- stipend(S), taship(T), X is S+T;
        X is 32000.