nOptimalSteppedTime  ::
  (Vec.Zipper vec,Eq a,Show a,Show (vec a),
   Vec.Walker vec,
   Vec.Storage vec (a, a),
   Arith.Constant a,
   Vec.FromList vec,
   Vec.Storage vec a,
   Vec.Singleton vec,
   Vec.Storage vec [Idx.State]) =>
  Sig.UTSignal vec [Idx.State] ->
  Sig.TSignal vec a -> Sig.TSignal vec a
genOptimalSteppedTime indexSignal time =
  Sig.fromList $ concat $ zipWith f is ts
  where f states (t1, t2) =
          if t1 == t2
             then []
             else concat $ zipWith (\x y -> [x, y]) leftTimes rightTimes

          where
                leftTimes = map (g . convert) [0 .. len]
                rightTimes =
                  case leftTimes of
                       (_:xs) -> xs
                       _ -> error "genOptimalSteppedTime: empty time list"

                len = length states
                convert = Arith.fromRational . fromIntegral
                g cnt = t1 ~+ (cnt ~* (t2 ~- t1) ~/ convert len)

        is = Sig.toList indexSignal
        ts = Sig.toList $ Sig.deltaMap (,) time


genOptimalSteppedTime  ::
  (Vec.Zipper vec,Eq a,Show a,Show (vec a),
   Vec.Walker vec,
   Vec.Storage vec (a, a),
   Arith.Constant a,
   Vec.FromList vec,
   Vec.Storage vec a,
   Vec.Singleton vec,
   Vec.Storage vec [Idx.State]) =>
  Sig.UTSignal vec [Idx.State] ->
  Sig.TSignal vec a -> Sig.TSignal vec a
genOptimalSteppedTime indexSignal time =
  Sig.fromList $ concat $ zipWith f is ts
  where f states (t1, t2) =
          if t1 == t2
             then []
             else concat $ zipWith (\x y -> [x, y]) leftTimes rightTimes

          where
                leftTimes = map (g . convert) [0 .. len]
                rightTimes =
                  case leftTimes of
                       (_:xs) -> xs
                       _ -> error "genOptimalSteppedTime: empty time list"

                len = length states
                convert = Arith.fromRational . fromIntegral
                g cnt = t1 ~+ (cnt ~* (t2 ~- t1) ~/ convert len)

        is = Sig.toList indexSignal
        ts = Sig.toList $ Sig.deltaMap (,) time
genOptimalSteppedTime indexSignal time =
  Sig.fromList $ concat $ zipWith f is ts
  where f states (t1, t2) =
          if t1 == t2
             then []
             else concat $ zipWith (\x y -> [x, y]) leftTimes rightTimes

          where
                leftTimes = map (g . convert) [0 .. len]
                rightTimes =
                  case leftTimes of
                       (_:xs) -> xs
                       _ -> error "genOptimalSteppedTime: empty time list"

                len = length states
                convert = Arith.fromRational . fromIntegral
                g cnt = t1 ~+ (cnt ~* (t2 ~- t1) ~/ convert len)

        is = Sig.toList indexSignal
        ts = Sig.toList $ Sig.deltaMap (,) time