Skip to content

Commit

Permalink
Switch to moving head instead of tape for less ambiguity
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondong committed Aug 2, 2024
1 parent 49ff0ce commit 5e16c1e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ fn show_head_pos_radio_label(
let read_value = current_tape[current_head_pos];
let tape_move_dir = STATE_TABLE.get_tape_mov_dir(current_state, read_value);
let updated_head_pos = match tape_move_dir {
TapeMoveDir::Left => current_head_pos + 1, // Tape moves left -> head position moves right.
TapeMoveDir::Right => current_head_pos - 1,
TapeMoveDir::Left => current_head_pos - 1,
TapeMoveDir::Right => current_head_pos + 1,
};

let expect_selected = head_pos_idx == updated_head_pos;
Expand All @@ -321,15 +321,15 @@ fn show_head_pos_radio_label(
current_state: usize,
) -> bool {
// STATE_TABLE.get_tape_mov_dir(const, const) is a known constant value during the compilation process.
(current_state == 1 && current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, true) == TapeMoveDir::Left && head_pos_idx == current_head_pos + 1 && !dest_head_pos[head_pos_idx]) ||
(current_state == 1 && current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, true) == TapeMoveDir::Right && head_pos_idx == current_head_pos - 1 && !dest_head_pos[head_pos_idx]) ||
(current_state == 1 && !current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, false) == TapeMoveDir::Left && head_pos_idx == current_head_pos + 1 && !dest_head_pos[head_pos_idx]) ||
(current_state == 1 && !current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, false) == TapeMoveDir::Right && head_pos_idx == current_head_pos - 1 && !dest_head_pos[head_pos_idx])
(current_state == 1 && current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, true) == TapeMoveDir::Left && head_pos_idx == current_head_pos - 1 && !dest_head_pos[head_pos_idx]) ||
(current_state == 1 && current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, true) == TapeMoveDir::Right && head_pos_idx == current_head_pos + 1 && !dest_head_pos[head_pos_idx]) ||
(current_state == 1 && !current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, false) == TapeMoveDir::Left && head_pos_idx == current_head_pos - 1 && !dest_head_pos[head_pos_idx]) ||
(current_state == 1 && !current_tape[current_head_pos] && STATE_TABLE.get_tape_mov_dir(1, false) == TapeMoveDir::Right && head_pos_idx == current_head_pos + 1 && !dest_head_pos[head_pos_idx])
// And so on for each possible value of current_state (finite number of states)...
}
```

The second line, assuming `STATE_TABLE.get_tape_mov_dir(1, true)` returns `TapeMoveDir::Right`, can be translated to:
The first line, assuming `STATE_TABLE.get_tape_mov_dir(1, true)` returns `TapeMoveDir::Left`, can be translated to:
```css
input#state-1:checked ~ input:not(:checked) + input + label + label + input:checked + input:checked + input + input + label {
visibility: visible;
Expand Down
14 changes: 7 additions & 7 deletions src/CompiledMachinePageBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ function addTapeCellValueLabelStyling(sb, config) {
function addHeadPositionLabelStyling(sb, config) {
// Rules for head position where destination has mismatch with move instruction:
config.forEach((state, stateIdx) => {
const moveLeftIf1 = state[1].move === 'L';
const moveLeftIf0 = state[0].move === 'L';
// TODO: if moveLeftIf1 === moveLeftIf0, then we can optimize this to one shorter selector.
const moveRightIf1 = state[1].move === 'R';
const moveRightIf0 = state[0].move === 'R';
// TODO: if moveRightIf1 === moveRightIf0, then we can optimize this to one shorter selector.

if (moveLeftIf1) {
if (moveRightIf1) {
// Concrete example: current head is at tape cell 0, moving to 1, destination head label is in tape cell 2 group.
const displayMoveIf1Mismatch = select(id(BUFFER_SWITCH_ID).unchecked(), '~', id(getInputId(0, STATE_PREFIX, stateIdx)).checked(), '~',
checked(), '+', '*', '+', checked(), '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+',
Expand All @@ -323,7 +323,7 @@ function addHeadPositionLabelStyling(sb, config) {
.displayBlock();
sb.push(displayMoveIf1Mismatch);
}
if (moveLeftIf0) {
if (moveRightIf0) {
const displayMoveIf0Mismatch = select(id(BUFFER_SWITCH_ID).unchecked(), '~', id(getInputId(0, STATE_PREFIX, stateIdx)).checked(), '~',
checked(), '+', '*', '+', unchecked(), '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+',
unchecked(), '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+',
Expand All @@ -339,7 +339,7 @@ function addHeadPositionLabelStyling(sb, config) {
}

// Reverse direction:
if (moveLeftIf1) {
if (moveRightIf1) {
const displayMoveIf1Mismatch = select(id(BUFFER_SWITCH_ID).checked(), '~', id(getInputId(1, STATE_PREFIX, stateIdx)).checked(), '~',
checked(), '+', '*', '+', checked(), '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+',
unchecked(), '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+',
Expand All @@ -353,7 +353,7 @@ function addHeadPositionLabelStyling(sb, config) {
.displayBlock();
sb.push(displayMoveIf1Mismatch);
}
if (moveLeftIf0) {
if (moveRightIf0) {
const displayMoveIf0Mismatch = select(id(BUFFER_SWITCH_ID).checked(), '~', id(getInputId(1, STATE_PREFIX, stateIdx)).checked(), '~',
checked(), '+', '*', '+', unchecked(), '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+',
unchecked(), '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+', '*', '+',
Expand Down
4 changes: 2 additions & 2 deletions src/TuringMachineForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import TuringMachineStateTable from './TuringMachineStateTable.jsx'
import ShareableHtmlLink from './ShareableHtmlLink.jsx';
import toHTML from './CompiledMachinePageBody.jsx';

const DEFAULT_STATE_0 = { name: 'A', 0: { write: '1', move: 'L', next: 'HALT' }, 1: { write: '0', move: 'L', next: 'A' } };
const DEFAULT_ADD = { 0: { write: '1', move: 'L', next: 'HALT' }, 1: { write: '0', move: 'L', next: 'HALT' } };
const DEFAULT_STATE_0 = { name: 'A', 0: { write: '1', move: 'L', next: 'HALT' }, 1: { write: '0', move: 'R', next: 'A' } };
const DEFAULT_ADD = { 0: { write: '1', move: 'L', next: 'HALT' }, 1: { write: '0', move: 'R', next: 'HALT' } };

export default function TuringMachineForm() {
const [numTapeCells, setNumTapeCells] = useState("15");
Expand Down
2 changes: 1 addition & 1 deletion src/TuringMachineStateTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function TuringMachineStateTable({ config, setConfig }) {
</th>)}
</tr>
<tr className="instruction-labels">
{config.map((c) => <React.Fragment key={c.name}><td>Write symbol</td><td>Move tape</td><td>Next state</td></React.Fragment>)}
{config.map((c) => <React.Fragment key={c.name}><td>Write symbol</td><td>Move head</td><td>Next state</td></React.Fragment>)}
</tr>
<tr>
<td>0</td>
Expand Down

0 comments on commit 5e16c1e

Please sign in to comment.