{-# 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 :: forall state' state dbgState.
(Typeable state', Typeable state) =>
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 <- forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast state'
state'
  forall (m :: * -> *) a. Monad m => a -> m a
return 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 (t :: * -> * -> *).
Typeable t =>
(forall a b c.
 (Typeable a, Typeable b, Typeable c) =>
 t b c -> a -> Maybe (t b c))
-> Migration
migrationTo2 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 :: forall state' state dbgState.
(Typeable state', Typeable state) =>
Debugging dbgState state -> Maybe state'
maybeMigrateFromDebugging Debugging {state
state :: state
state :: forall dbgState state. Debugging dbgState state -> state
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 (t :: * -> * -> *).
Typeable t =>
(forall a b c.
 (Typeable a, Typeable b, Typeable c) =>
 t b c -> Maybe a)
-> Migration
constMigrationFrom2 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 forall a. Semigroup a => a -> a -> a
<> Migration
migrationFromDebugging