-- SPDX-FileCopyrightText: This work was generated by Claude Opus 4.8 in 2026.
--
-- SPDX-License-Identifier: LicenseRef-No-Human-Authorship
module Language.Burro.TCProof.Configuration (decodeConfiguration) where
import qualified Data.Map.Strict as Map
import qualified Language.Burro.Definition as B
import Language.Turmac.Model (TMRule, Tape(..), Configuration(..))
import Language.Turmac.Normalizer (createSymbolMapping, createStateMapping)
import Language.Burro.TCProof.TM2Kondey (countStates, countSymbols, cellOffset, cellStructSize)
--
-- Translating a Burro State back to a Turmac Configuration
-- ========================================================
--
-- This complements `Language.Burro.TCProof.TM2Kondey.compileToKondey`:
-- given the Burro state reached by running the compiled program, and the
-- original (un-normalized) TM rules that were compiled, reconstruct the
-- Turmac Configuration that the encoded Burro tape represents, so it can
-- be compared against the result of simulating the same rules directly
-- with Language.Turmac.Simulator.
--
-- We use the position field that was recently added to the `State` record
-- in `Language.Burro.Definition` to track the absolute head position, to
-- assist in this conversion.
--
--------------------------------------------------------
-- Decoding a (running or halted) Burro state as a --
-- Turmac Configuration --
--------------------------------------------------------
-- | Given the original TM rules, whether the machine has halted, and the
-- Burro State/head-position reached after some number of iterations of
-- the Kondey-via-Burro compilation (the head must be at rest, i.e. parked
-- on a CellStruct's `state` cell, as it always is between iterations of
-- stepTrackedBurro/runTrackedBurro), reconstruct the Turmac Configuration
-- it represents.
--
-- Decoding the current TM state only ever needs to read the `state` field
-- of the CellStruct the head currently occupies: a CellStruct's `state`
-- field for any other position may hold a harmless stale value (whatever
-- state last departed from it), but that is never read again, because
-- `Language.Burro.TCProof.TM2Kondey`'s writeNewStateAtDest always overwrites a
-- CellStruct's `state` field before the head can return there and dispatch
-- on it.
decodeConfiguration :: [TMRule] -> Bool -> B.State -> Configuration
decodeConfiguration tmRules halted (B.State dat _ _ headPos) =
let
numStates = countStates tmRules
numSymbols = countSymbols tmRules
structSize = toInteger (cellStructSize numStates numSymbols)
cellOff = toInteger (cellOffset numStates)
revSymMap = Map.fromList
[(read normId :: Integer, orig) | (orig, normId) <- createSymbolMapping tmRules]
revStateMap = Map.fromList
[(read normId :: Integer, orig) | (orig, normId) <- createStateMapping tmRules]
(B.Tape l r) = dat
cellMap = Map.fromList (zip [headPos, headPos - 1 ..] l ++ zip [headPos + 1 ..] r)
rawAt i = Map.findWithDefault 0 i cellMap
headStruct
| headPos `mod` structSize /= 0 = error $
"turmac bisimulation: Burro data head at position " ++ show headPos ++
" is not aligned to a TM CellStruct boundary (size " ++ show structSize ++ ")"
| otherwise = headPos `div` structSize
minStruct = (headPos - toInteger (length l - 1)) `div` structSize
maxStruct = (headPos + toInteger (length r)) `div` structSize
decodeSymbol structIdx =
let raw = rawAt (structIdx * structSize + cellOff) in
if odd raw
then error $
"turmac bisimulation: TM tape cell at position " ++ show structIdx ++
" decoded to an odd Burro value (" ++ show raw ++ "); simulation is not at rest"
else case Map.lookup (raw `div` 2) revSymMap of
Just sym -> sym
Nothing -> error $
"turmac bisimulation: no TM symbol corresponds to encoded value " ++
show raw ++ " at TM position " ++ show structIdx
decodeState structIdx =
let raw = rawAt (structIdx * structSize) in
if odd raw
then error $
"turmac bisimulation: TM state cell at position " ++ show structIdx ++
" decoded to an odd Burro value (" ++ show raw ++ "); simulation is not at rest"
else case Map.lookup (raw `div` 2) revStateMap of
Just st -> st
Nothing -> error $
"turmac bisimulation: no TM state corresponds to encoded value " ++
show raw ++ " at TM position " ++ show structIdx
in
Configuration
{ tmTape = Tape
{ leftCells = [decodeSymbol s | s <- [headStruct - 1, headStruct - 2 .. minStruct]]
, currentCell = decodeSymbol headStruct
, rightCells = [decodeSymbol s | s <- [headStruct + 1 .. maxStruct]]
}
, tmPos = headStruct
, tmState = if halted then "@H" else decodeState headStruct
}