{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
module LiveCoding.Migrate.Debugger where

-- base
import Data.Data

-- essence-of-live-coding
import LiveCoding.Debugger
import LiveCoding.Migrate.Migration

maybeMigrateToDebugging
  :: (Typeable state', Typeable state)
  => Debugging dbgState state
  -> state'
  -> Maybe (Debugging dbgState state)
maybeMigrateToDebugging :: Debugging dbgState state
-> state' -> Maybe (Debugging dbgState state)
maybeMigrateToDebugging Debugging { dbgState
dbgState :: forall dbgState state. Debugging dbgState state -> dbgState
dbgState :: dbgState
dbgState } state'
state' = do
  state
state <- state' -> Maybe state
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast state'
state'
  Debugging dbgState state -> Maybe (Debugging dbgState state)
forall (m :: * -> *) a. Monad m => a -> m a
return Debugging :: forall dbgState state.
state -> dbgState -> Debugging dbgState state
Debugging { state
dbgState
state :: state
state :: state
dbgState :: dbgState
dbgState :: dbgState
.. }

-- | Tries to cast the current state into the joint state of debugger and program.
migrationToDebugging :: Migration
migrationToDebugging :: Migration
migrationToDebugging = (forall a b c.
 (Typeable a, Typeable b, Typeable c) =>
 Debugging b c -> a -> Maybe (Debugging b c))
-> Migration
forall (t :: * -> * -> *).
Typeable t =>
(forall a b c.
 (Typeable a, Typeable b, Typeable c) =>
 t b c -> a -> Maybe (t b c))
-> Migration
migrationTo2 forall a b c.
(Typeable a, Typeable b, Typeable c) =>
Debugging b c -> a -> Maybe (Debugging b c)
forall state' state dbgState.
(Typeable state', Typeable state) =>
Debugging dbgState state
-> state' -> Maybe (Debugging dbgState state)
maybeMigrateToDebugging

maybeMigrateFromDebugging
  :: (Typeable state', Typeable state)
  => Debugging dbgState state
  -> Maybe              state'
maybeMigrateFromDebugging :: Debugging dbgState state -> Maybe state'
maybeMigrateFromDebugging Debugging { state
state :: state
state :: forall dbgState state. Debugging dbgState state -> state
state } = state -> Maybe state'
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast state
state

-- | Try to extract a state from the current joint state of debugger and program.
migrationFromDebugging :: Migration
migrationFromDebugging :: Migration
migrationFromDebugging = (forall a b c.
 (Typeable a, Typeable b, Typeable c) =>
 Debugging b c -> Maybe a)
-> Migration
forall (t :: * -> * -> *).
Typeable t =>
(forall a b c.
 (Typeable a, Typeable b, Typeable c) =>
 t b c -> Maybe a)
-> Migration
constMigrationFrom2 forall a b c.
(Typeable a, Typeable b, Typeable c) =>
Debugging b c -> Maybe a
forall state' state dbgState.
(Typeable state', Typeable state) =>
Debugging dbgState state -> Maybe state'
maybeMigrateFromDebugging

-- | Combines 'migrationToDebugging' and 'migrationFromDebugging'.
migrationDebugging :: Migration
migrationDebugging :: Migration
migrationDebugging = Migration
migrationToDebugging Migration -> Migration -> Migration
forall a. Semigroup a => a -> a -> a
<> Migration
migrationFromDebugging