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

# interpreter for Turing machine
CONSTRUCT {
    () :compute ?OutTape.
} WHERE {
    ?Machine :start ?I.
    (?I () "#" ()) :find ?OutTape.
} ;

CONSTRUCT {
    ?List :compute ?OutTape.
} WHERE {
    ?List list:firstRest (?Head ?Tail).
    ?Machine :start ?I.
    (?I () ?Head ?Tail) :find ?OutTape.
} ;

CONSTRUCT {
    (?State ?Left ?Cell ?Right) :find ?OutTape.
} WHERE {
    (?State ?Cell ?Write ?Move) :tape ?Next.
    (?Move ?Left ?Write ?Right ?A ?B ?C) :move true.
    (?Next ?A ?B ?C) :continue ?OutTape.
} ;

CONSTRUCT {
    (:halt ?Left ?Cell ?Right) :continue ?OutTape.
} WHERE {
    ?Left :reverse ?R.
    ?List list:firstRest (?Cell ?Right).
    (?R ?List) list:append ?OutTape.
} ;

CONSTRUCT {
    (?State ?Left ?Cell ?Right) :continue ?OutTape.
} WHERE {
    (?State ?Left ?Cell ?Right) :find ?OutTape.
} ;

CONSTRUCT {
    (:left () ?Cell ?Right () "#" ?L) :move true.
} WHERE {
    ?L list:firstRest (?Cell ?Right).
} ;

CONSTRUCT {
    (:left ?List ?Cell ?Right ?Tail ?Head ?L) :move true.
} WHERE {
    ?List list:firstRest (?Head ?Tail).
    ?L list:firstRest (?Cell ?Right).
} ;

CONSTRUCT {
    (:stop ?Left ?Cell ?Right ?Left ?Cell ?Right) :move true.
} WHERE {} ;

CONSTRUCT {
    (:right ?Left ?Cell () ?L "#" ()) :move true.
} WHERE {
    ?L list:firstRest (?Cell ?Left).
} ;

CONSTRUCT {
    (:right ?Left ?Cell ?List ?L ?Head ?Tail) :move true.
} WHERE {
    ?List list:firstRest (?Head ?Tail).
    ?L list:firstRest (?Cell ?Left).
} ;

CONSTRUCT {
    () :reverse ().
} WHERE {} ;

CONSTRUCT {
    ?List :reverse ?Reverse.
} WHERE {
    ?List list:firstRest (?Head ?Tail).
    ?Tail :reverse ?R.
    (?R (?Head)) list:append ?Reverse.
}
