nOptimalSteppedSignal  ::
  (Vec.Storage vec [Idx.State], Eq a,Vec.Storage vec (a, a), Show (vec a),
   Vec.Singleton vec,Show a,
   Vec.Zipper vec, Vec.Walker vec,
   Vec.Storage vec a, Vec.FromList vec,
   Vec.Storage vec (Map Idx.State a)) =>
  Sig.UTSignal vec [Idx.State] ->
  Sig.TSignal vec a ->
  Map Idx.State (Sig.PSignal vec a) ->
  Sig.PSignal vec a
genOptimalSteppedSignal indexSignal time signalMap =
  Sig.fromList $ concat $ zipWith3 g is ts signalOfMaps
  where
    g states (t1, t2) m
      = if t1 == t2 then [] else concatMap (h m) states

    h m st = let x = fromMaybe (error $ err st m) (Map.lookup st m) in [x, x]

    err x m = "genOptimalSteppedSignal: Element "
              ++ show x ++ " not found in " ++ show m

{-
    wofür ist das denn da?

    xlast =
      case (signalOfMaps, is) of
           (m:_, xs@(x:_):_) ->
             let x = last xs in fromMaybe (error $ err x m) (Map.lookup x m)
           _ -> error "genOptimalSteppedSignal: empty list"
-}

    signalOfMaps = Sig.toList $
      Map.foldrWithKey' (Sig.zipWith . Map.insert) emptySig signalMap

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

    emptySig = Sig.map (const Map.empty) $ snd $ Map.findMin signalMap



genOptimalSteppedSignal  ::
  (Vec.Storage vec [Idx.State], Eq a,Vec.Storage vec (a, a), Show (vec a),
   Vec.Singleton vec,Show a,
   Vec.Zipper vec, Vec.Walker vec,
   Vec.Storage vec a, Vec.FromList vec,
   Vec.Storage vec (Map Idx.State a)) =>
  Sig.UTSignal vec [Idx.State] ->
  Sig.TSignal vec a ->
  Map Idx.State (Sig.PSignal vec a) ->
  Sig.PSignal vec a
genOptimalSteppedSignal indexSignal time signalMap =
  Sig.fromList $ concat $ zipWith3 g is ts signalOfMaps
  where
    g states (t1, t2) m
      = if t1 == t2 then [] else concatMap (h m) states

    h m st = let x = fromMaybe (error $ err st m) (Map.lookup st m) in [x, x]

    err x m = "genOptimalSteppedSignal: Element "
              ++ show x ++ " not found in " ++ show m

{-
    wofür ist das denn da?

    xlast =
      case (signalOfMaps, is) of
           (m:_, xs@(x:_):_) ->
             let x = last xs in fromMaybe (error $ err x m) (Map.lookup x m)
           _ -> error "genOptimalSteppedSignal: empty list"
-}

    signalOfMaps = Sig.toList $
      Map.foldrWithKey' (Sig.zipWith . Map.insert) emptySig signalMap

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

    emptySig = Sig.map (const Map.empty) $ snd $ Map.findMin signalMap
genOptimalSteppedSignal indexSignal time signalMap =
  Sig.fromList $ concat $ zipWith3 g is ts signalOfMaps
  where
    g states (t1, t2) m
      = if t1 == t2 then [] else concatMap (h m) states

    h m st = let x = fromMaybe (error $ err st m) (Map.lookup st m) in [x, x]

    err x m = "genOptimalSteppedSignal: Element "
              ++ show x ++ " not found in " ++ show m

{-
    wofür ist das denn da?

    xlast =
      case (signalOfMaps, is) of
           (m:_, xs@(x:_):_) ->
             let x = last xs in fromMaybe (error $ err x m) (Map.lookup x m)
           _ -> error "genOptimalSteppedSignal: empty list"
-}

    signalOfMaps = Sig.toList $
      Map.foldrWithKey' (Sig.zipWith . Map.insert) emptySig signalMap

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

    emptySig = Sig.map (const Map.empty) $ snd $ Map.findMin signalMap