# -----------------
# Takeuchi Function
# -----------------
#
# See https://en.wikipedia.org/wiki/Tak_(function)

@prefix math: <http://www.w3.org/2000/10/swap/math#>.
@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix var: <http://www.w3.org/2000/10/swap/var#>.
@prefix : <http://example.org/#>.

# tak backward rules
[ log:graph (
    [ log:triple ((var:X var:Y var:Z) :tak var:Z)]
)] log:isImpliedBy [ log:graph (
    [ log:triple ([ log:graph (
        [ log:triple (var:X math:notGreaterThan var:Y)]
    )] log:call true)]
    [ log:triple (true log:callWithCut true)]
)].

[ log:graph (
    [ log:triple ((var:X var:Y var:Z) :tak var:A)]
)] log:isImpliedBy [ log:graph (
    [ log:triple ((var:X 1) math:difference var:X1)]
    [ log:triple ((var:X1 var:Y var:Z) :tak var:A1)]
    [ log:triple ((var:Y 1) math:difference var:Y1)]
    [ log:triple ((var:Y1 var:Z var:X) :tak var:A2)]
    [ log:triple ((var:Z 1) math:difference var:Z1)]
    [ log:triple ((var:Z1 var:X var:Y) :tak var:A3)]
    [ log:triple ((var:A1 var:A2 var:A3) :tak var:A)]
)].

# query
[ log:graph (
    [ log:triple ((18 13 8) :tak var:A)]
)] log:impliesAnswer [ log:graph (
    [ log:triple ((18 13 8) :tak var:A)]
)].
