PREFIX log: <http://www.w3.org/2000/10/swap/log#>
PREFIX : <http://example.org/#>

# add
CONSTRUCT {
    (?A 0) :add ?A.
} WHERE {}

CONSTRUCT {
    (?A (:s ?B)) :add (:s ?C).
} WHERE {
    (?A ?B) :add ?C.
}

# multiply
CONSTRUCT {
    (?A 0) :multiply 0.
} WHERE {}

CONSTRUCT {
    (?A (:s ?B)) :multiply ?C.
} WHERE {
    (?A ?B) :multiply ?D.
    (?A ?D) :add ?C.
}

# factorial
CONSTRUCT {
    ?A :factorial ?B.
} WHERE {
    (?A (:s 0)) :fac ?B.
}

CONSTRUCT {
    (0 ?A) :fac ?A.
} WHERE {}

CONSTRUCT {
    ((:s ?A) ?B) :fac ?C.
} WHERE {
    (?B (:s ?A)) :multiply ?D.
    (?A ?D) :fac ?C.
}
