// Souffle - A Datalog Compiler
// Copyright (c) 2017, 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

// Tests rules containing body literals that are independent of the
// head arguments (i.e. they are 'constant' wrt. head arguments).

.decl a(x:number)

// Should become:
//  - a(x) :- b(z,x), !e(z), newrel0(), newrel1().
//  - newrel0() :- b(y,y), c(y).
//  - newrel1() :- b(m,n), e(n).
a(x) :- b(y,y), b(z,x), !e(z), c(y), b(_,n), e(n).

// Should remain unchanged.
a(x) :- b(x,y), c(y), d(y,_).

.decl b(x:number, y:number)
.input b()

.decl c(x:number)
.input c()

.decl d(x:number, y:number)
.input d()

.decl e(x:number)

// Should become:
//  - e(x) :- b(x,x), newrel().
//  - newrel() :- a = max Z : b(_,Z), a + 1 < 10.
e(x) :- b(x,x), a = max Z : b(_,Z), a + 1 < 10.

.decl f(x:number)

// Should become:
//  - f(x) :- d(x,_), newrel().
//  - newrel() :- e(_).
f(x) :- d(x,_), e(_).

.decl good()

// Should remain unchanged (no head arguments).
good() :- a(x), e(x).

.decl query(x:number)
.output query()

// Should become:
//  - query(x) :- (a(x) ; e(x) ; f(x)), good(), newrel().
//  - newrel() :- (a(y) ; f(y)).
query(x) :- (a(x) ; e(x) ; f(x)), good(), (a(y) ; f(y)).
