// Souffle - A Datalog Compiler
// Copyright (c) 2016, Oracle and/or its affiliates. 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

// Test complicated forms of finding the maximum in a relation.

.decl A(n:number,d:number)
.input A()
.decl B(n:number,s:number)
.input B()

.decl C1(n:number,id:number)
.output C1()
C1(n,id) :-
   A(n,id),
   B(id,max  s : {A(n,d), B(d,s)}).

.decl C2(n:number,id:number,s:number)
.output C2()
C2(n,id,Y) :-
   A(n,id),
   B(id,Y),
   Y = max  s : {A(n,d), B(d,s)}.

.decl C3(n:number,id:number)
.output C3()
C3(n,id) :-
   A(n,id),
   B(id,Y),
   2 * Y + 1 =  2 * max  s : {A(n,d), B(d,s)} + 1.

.decl C4(n:number,id:number)
.output C4()
C4(n,id) :-
   A(n,id),
   B(id,Y),
   Y =  max  s : {A(n,d), B(d,s)} .

