// Souffle - A Datalog Compiler
// Copyright (c) 2021, The Souffle Developers. All rights reserved
// Licensed under the Universal Permissive License v 1.0 as shown at:
// - https://opensource.org/licenses/UPL
// - <souffle root>/licenses/SOUFFLE-UPL.txt

// building a shortest-path tree for a simple graph
// with multiple subsumptive rules

.decl A(from:number, to:number, z:number) btree_delete
.output A

.decl graph(from:number, to:number)

graph(1, 2).
graph(1, 3).
graph(3, 4).
graph(2, 5).
graph(4, 6).
graph(5, 6).
graph(6, 7).

A(1, 2, 1).
A(1, 3, 1).

A(from, to, c+1) :-
    A(_, from, c),
    graph(from, to).

A(from, to, c1) <= A(_, to, c2) :-
    c1 > c2.

A(from1, to, c) <= A(from2, to, c) :-
    from1 > from2.


.decl eql(a: number, b: number)
.decl rel(a: number, b: number) btree_delete

eql(x,x) :- eql(x,_); eql(_,x).
eql(x,y) :- eql(y,x).
eql(x,z) :- eql(x,y), eql(y,z).

rel(1, 2).
rel(1, 3).

eql(x, x) :- rel(x, _).
eql(2, 3).

rel(a, b) <= rel(c, d) :- eql(a, c), eql(b, d), a <= c, b <= d.

.output rel

// check query plan
.decl C(x:number) btree_delete
.decl D(x:number, y:number)
C(1).
D(1, 1).
C(x1) <= C(x2) :-
    D(x1, x2),
    x1 <= x2.
    .plan 0:(1,3,2)
.output C

// check unnamed variables in dominated head

.decl E(x:number, y:number) btree_delete

E(1,1).
E(1,2).

E(_, x1) <= E(_, x2) :-
   x1 <= x2.

.output E

// check unset btree_delete qualifier

.decl F(x:number)

F(1).
F(2).

F(x1) <= F(x2) :-
   x1 <= x2.

.output F

// build full comparator for all indices in a relation

.decl G(from:number, to:number)
G(1, 1).

.decl R(a: number, b: number)
R(2,2).

.decl ES(x:number, y:number) btree_delete
ES(1,1).
ES(1,2).
ES(_, x1) <= ES(_, x2) :- x1 <= x2.

.decl AF(a:number)
AF(b) :- G(g, b), !R(b, d), ES(d, a), ES(d, e).
.output AF

.decl my(x:number)
.decl mx(x:number)
mx(n) <= mx(n) :- my(n).
.output mx

.decl nx(x:number)
nx(n) <= nx(n) :- nx(n).
.output nx
