# -----------------
# Fibonacci numbers
# -----------------
#
# See https://en.wikipedia.org/wiki/Fibonacci_number

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

# declare components
:fibonacci a log:Component.
:fib a log:Component.

(_:X _:Y) log:onNegativeSurface {
    () log:onNegativeSurface {
        _:X :fibonacci _:Y.
    }.
    (_:X 0 1) :fib _:Y.
}.

(_:A _:B) log:onNegativeSurface {
    () log:onNegativeSurface {
        (0 _:A _:B) :fib _:A.
    }.
}.

(_:A _:B) log:onNegativeSurface {
    () log:onNegativeSurface {
        (1 _:A _:B) :fib _:B.
    }.
}.

(_:A _:B _:C _:D _:X _:Y) log:onNegativeSurface {
    () log:onNegativeSurface {
        (_:X _:A _:B) :fib _:Y.
    }.
    _:X math:greaterThan 1.
    (_:X 1) math:difference _:C.
    (_:A _:B) math:sum _:D.
    (_:C _:B _:D) :fib _:Y.
}.

# query
(_:X) log:onNegativeSurface {
    1 :fibonacci _:X.
    () log:onNegativeAnswerSurface {
        1 :fibonacci _:X.
    }.
}.

(_:X) log:onNegativeSurface {
    2 :fibonacci _:X.
    () log:onNegativeAnswerSurface {
        2 :fibonacci _:X.
    }.
}.

(_:X) log:onNegativeSurface {
    91 :fibonacci _:X.
    () log:onNegativeAnswerSurface {
        91 :fibonacci _:X.
    }.
}.

(_:X) log:onNegativeSurface {
    283 :fibonacci _:X.
    () log:onNegativeAnswerSurface {
        283 :fibonacci _:X.
    }.
}.

(_:X) log:onNegativeSurface {
    3674 :fibonacci _:X.
    () log:onNegativeAnswerSurface {
        3674 :fibonacci _:X.
    }.
}.
