From 41b63eff7f76e6574ef238f821dad0212a619c2a Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 22 Feb 2015 17:14:15 +0100 Subject: Improved `simulate` function of the Turing machine example * introduced `state::state_accessor` helper function and moved position logic into `updatePosition` * added `IsEqual` comparator and expressed other basic comparators in terms of itself --- example/turing/src/machine.h | 92 +++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 40 deletions(-) (limited to 'example/turing/src/machine.h') diff --git a/example/turing/src/machine.h b/example/turing/src/machine.h index c1338e1..a30c6e3 100644 --- a/example/turing/src/machine.h +++ b/example/turing/src/machine.h @@ -8,21 +8,41 @@ namespace machine { +// (define (updatePosition direction position) +// (cond ((= direction RIGHT) (+ position 1)) +// ((= direction LEFT) (- Position 1)) +// (else position))) +template < + typename Direction, + typename Position +> +using updatePosition = tav::Cond< + tav::Branch< + tav::IsEqualValue>, + tav::Add> + >, + tav::Branch< + tav::IsEqualValue>, + tav::Substract> + >, + tav::Else< + Position + > +>; + + // (define (simulate transition tape state position) // (if (= state FINAL) // tape -// (let* ((current_symbol (readSymbol position tape)) -// (current_state (transition state current_symbol)) -// (direction (nth MOVE current_state)) -// (next_symbol (nth WRITE current_state)) -// (next_state (nth NEXT current_state)) -// (next_position (cond ((= direction RIGHT) (+ position 1)) -// ((= direction LEFT) (- Position 1)) -// (else position)))) +// (let ((current_state (transition state +// (readSymbol position tape)))) // (simulate transition -// (writeSymbol position next_symbol tape) -// next_state, -// next_position))) +// (current_state NEXT) +// (writeSymbol position +// (current_state WRITE) +// tape) +// (updatePosition (current_state MOVE) +// position))))) template < template class Transition, typename State, @@ -30,39 +50,31 @@ template < typename Position > class simulate { - private: - using current_symbol = tape::readSymbol; - using current_state = Transition; - - using direction = tav::Nth; - - using next_symbol = tav::Nth; - using next_state = tav::Nth; - using next_position = tav::Cond< - tav::Branch< - tav::IsEqualValue>, - tav::Add> - >, - tav::Branch< - tav::IsEqualValue>, - tav::Substract> - >, - tav::Else< - Position - > - >; + static_assert( + tav::Not>>::value, + "machine head position out of bounds" + ); - static_assert( - tav::Not>>::value, - "next position out of bounds" - ); + private: + template + using current_state = typename Transition< + State, + tape::readSymbol + >::template get; public: using type = tav::Eval, - next_position + current_state, + tape::writeSymbol< + Position, + current_state, + Tape + >, + updatePosition< + current_state, + Position + > >>; }; @@ -77,8 +89,8 @@ struct simulate { // (define (run program state tape position) // (simulate (transition program) -// tape // state +// tape // position)) template < typename Program, -- cgit v1.2.3