{-# LANGUAGE TypeFamilies, FlexibleInstances, GADTs, StandaloneDeriving #-}
{-# OPTIONS_HADDOCK hide #-}
module ForSyDe.Atom.MoC.DE.Core where
import ForSyDe.Atom.MoC
import ForSyDe.Atom.MoC.TimeStamp
import ForSyDe.Atom.Utility.Tuple (($$),($$$),($$$$))
import Prelude hiding (until)
type SignalBase t a = Stream (DE t a)
type Signal a = SignalBase TimeStamp a
data DE t a where
DE :: (Num t, Ord t, Eq t)
=> { DE t a -> t
tag :: t,
DE t a -> a
val :: a
} -> DE t a
deriving instance (Num t, Ord t, Eq t, Eq t, Eq a) => Eq (DE t a)
instance (Num t, Ord t, Eq t) => MoC (DE t) where
type Fun (DE t) a b = a -> b
type Ret (DE t) b = b
-.- :: Fun (DE t) a b -> Stream (DE t a) -> Stream (DE t b)
(-.-) = (DE t a -> DE t b) -> Stream (DE t a) -> Stream (DE t b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((DE t a -> DE t b) -> Stream (DE t a) -> Stream (DE t b))
-> ((a -> b) -> DE t a -> DE t b)
-> (a -> b)
-> Stream (DE t a)
-> Stream (DE t b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> DE t a -> DE t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
_ -*- :: Stream (DE t (Fun (DE t) a b))
-> Stream (DE t a) -> Stream (DE t b)
-*- NullS = Stream (DE t b)
forall e. Stream e
NullS
NullS -*- _ = Stream (DE t b)
forall e. Stream e
NullS
(f :: DE t (Fun (DE t) a b)
f:-fs :: Stream (DE t (Fun (DE t) a b))
fs) -*- (x :: DE t a
x:-xs :: Stream (DE t a)
xs) = DE t (Fun (DE t) a b)
DE t (a -> b)
f DE t (a -> b) -> DE t a -> DE t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DE t a
x DE t b -> Stream (DE t b) -> Stream (DE t b)
forall e. e -> Stream e -> Stream e
:- DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
forall t a b.
(Num t, Ord t) =>
DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
comb DE t (Fun (DE t) a b)
DE t (a -> b)
f DE t a
x Stream (DE t (Fun (DE t) a b))
Stream (DE t (a -> b))
fs Stream (DE t a)
xs
where comb :: DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
comb pf :: DE t (a -> b)
pf px :: DE t a
px s1 :: Stream (DE t (a -> b))
s1@(f :: DE t (a -> b)
f :- fs :: Stream (DE t (a -> b))
fs) s2 :: Stream (DE t a)
s2@(x :: DE t a
x :- xs :: Stream (DE t a)
xs)
| DE t (a -> b) -> t
forall t a. DE t a -> t
tag DE t (a -> b)
f t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== DE t a -> t
forall t a. DE t a -> t
tag DE t a
x = DE t (a -> b)
f DE t (a -> b) -> DE t (a -> b) -> DE t (a -> b)
forall t a t a. DE t a -> DE t a -> DE t a
%> DE t (a -> b)
f DE t (a -> b) -> DE t a -> DE t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DE t a
x DE t b -> Stream (DE t b) -> Stream (DE t b)
forall e. e -> Stream e -> Stream e
:- DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
comb DE t (a -> b)
f DE t a
x Stream (DE t (a -> b))
fs Stream (DE t a)
xs
| DE t (a -> b) -> t
forall t a. DE t a -> t
tag DE t (a -> b)
f t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< DE t a -> t
forall t a. DE t a -> t
tag DE t a
x = DE t (a -> b)
f DE t (a -> b) -> DE t (a -> b) -> DE t (a -> b)
forall t a t a. DE t a -> DE t a -> DE t a
%> DE t (a -> b)
f DE t (a -> b) -> DE t a -> DE t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DE t a
px DE t b -> Stream (DE t b) -> Stream (DE t b)
forall e. e -> Stream e -> Stream e
:- DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
comb DE t (a -> b)
f DE t a
px Stream (DE t (a -> b))
fs Stream (DE t a)
s2
| DE t (a -> b) -> t
forall t a. DE t a -> t
tag DE t (a -> b)
f t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> DE t a -> t
forall t a. DE t a -> t
tag DE t a
x = DE t a
x DE t a -> DE t (a -> b) -> DE t (a -> b)
forall t a t a. DE t a -> DE t a -> DE t a
%> DE t (a -> b)
pf DE t (a -> b) -> DE t a -> DE t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DE t a
x DE t b -> Stream (DE t b) -> Stream (DE t b)
forall e. e -> Stream e -> Stream e
:- DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
comb DE t (a -> b)
pf DE t a
x Stream (DE t (a -> b))
s1 Stream (DE t a)
xs
comb _ px :: DE t a
px (f :: DE t (a -> b)
f :- fs :: Stream (DE t (a -> b))
fs) NullS = DE t (a -> b)
f DE t (a -> b) -> DE t (a -> b) -> DE t (a -> b)
forall t a t a. DE t a -> DE t a -> DE t a
%> DE t (a -> b)
f DE t (a -> b) -> DE t a -> DE t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DE t a
px DE t b -> Stream (DE t b) -> Stream (DE t b)
forall e. e -> Stream e -> Stream e
:- DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
comb DE t (a -> b)
f DE t a
px Stream (DE t (a -> b))
fs Stream (DE t a)
forall e. Stream e
NullS
comb pf :: DE t (a -> b)
pf _ NullS (x :: DE t a
x :- xs :: Stream (DE t a)
xs) = DE t a
x DE t a -> DE t (a -> b) -> DE t (a -> b)
forall t a t a. DE t a -> DE t a -> DE t a
%> DE t (a -> b)
pf DE t (a -> b) -> DE t a -> DE t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DE t a
x DE t b -> Stream (DE t b) -> Stream (DE t b)
forall e. e -> Stream e -> Stream e
:- DE t (a -> b)
-> DE t a
-> Stream (DE t (a -> b))
-> Stream (DE t a)
-> Stream (DE t b)
comb DE t (a -> b)
pf DE t a
x Stream (DE t (a -> b))
forall e. Stream e
NullS Stream (DE t a)
xs
comb _ _ NullS NullS = Stream (DE t b)
forall e. Stream e
NullS
-* :: Stream (DE t (Ret (DE t) b)) -> Stream (DE t b)
(-*) = Stream (DE t (Ret (DE t) b)) -> Stream (DE t b)
forall a. a -> a
id
(DE _ v :: a
v :- _) -<- :: Stream (DE t a) -> Stream (DE t a) -> Stream (DE t a)
-<- xs :: Stream (DE t a)
xs = a -> DE t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
v DE t a -> Stream (DE t a) -> Stream (DE t a)
forall e. e -> Stream e -> Stream e
:- Stream (DE t a)
xs
(_ :- DE d :: t
d _ :- _) -&- :: Stream (DE t a) -> Stream (DE t a) -> Stream (DE t a)
-&- xs :: Stream (DE t a)
xs = (\(DE t :: t
t v :: a
v) -> t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE (t
t t -> t -> t
forall a. Num a => a -> a -> a
+ t
d) a
v) (DE t a -> DE t a) -> Stream (DE t a) -> Stream (DE t a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream (DE t a)
xs
(_ :- NullS) -&- _ = [Char] -> Stream (DE t a)
forall a. HasCallStack => [Char] -> a
error "[MoC.DE] signal delayed to infinity"
instance (Show a, Show t) => Show (DE t a) where
showsPrec :: Int -> DE t a -> ShowS
showsPrec _ (DE t :: t
t x :: a
x) = [Char] -> ShowS
forall a. [a] -> [a] -> [a]
(++) ( a -> [Char]
forall a. Show a => a -> [Char]
show a
x [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ "@" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ t -> [Char]
forall a. Show a => a -> [Char]
show t
t )
instance (Read a,Read t, Num t, Ord t, Eq t, Eq t) => Read (DE t a) where
readsPrec :: Int -> ReadS (DE t a)
readsPrec _ x :: [Char]
x = [ (t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
tg a
val, [Char]
r2)
| (val :: a
val,r1 :: [Char]
r1) <- ReadS a
forall a. Read a => ReadS a
reads ReadS a -> ReadS a
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/='@') [Char]
x
, (tg :: t
tg, r2 :: [Char]
r2) <- ReadS t
forall a. Read a => ReadS a
reads ReadS t -> ReadS t
forall a b. (a -> b) -> a -> b
$ ShowS
forall a. [a] -> [a]
tail ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/='@') [Char]
x ]
instance (Num t, Ord t, Eq t) => Functor (DE t) where
fmap :: (a -> b) -> DE t a -> DE t b
fmap f :: a -> b
f (DE t :: t
t a :: a
a) = t -> b -> DE t b
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
t (a -> b
f a
a)
instance (Num t, Ord t, Eq t) => Applicative (DE t) where
pure :: a -> DE t a
pure = t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE 0
(DE tf :: t
tf f :: a -> b
f) <*> :: DE t (a -> b) -> DE t a -> DE t b
<*> (DE _ x :: a
x) = t -> b -> DE t b
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
tf (a -> b
f a
x)
unit :: (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit2 :: (Num t, Ord t, Eq t)
=> ((t,a1),(t, a2))
-> (SignalBase t a1, SignalBase t a2)
unit3 :: (Num t, Ord t, Eq t)
=> ((t,a1),(t, a2),(t, a3))
-> (SignalBase t a1, SignalBase t a2, SignalBase t a3)
unit4 :: (Num t, Ord t, Eq t)
=> ((t,a1),(t, a2),(t, a3),(t, a4))
-> (SignalBase t a1, SignalBase t a2, SignalBase t a3, SignalBase t a4)
unit :: (t, a) -> SignalBase t a
unit (t :: t
t,v :: a
v) = (t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE 0 a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
t a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- SignalBase t a
forall e. Stream e
NullS)
unit2 :: ((t, a1), (t, a2)) -> (SignalBase t a1, SignalBase t a2)
unit2 = ((t, a1) -> SignalBase t a1, (t, a2) -> SignalBase t a2)
-> ((t, a1), (t, a2)) -> (SignalBase t a1, SignalBase t a2)
forall a1 b1 a2 b2. (a1 -> b1, a2 -> b2) -> (a1, a2) -> (b1, b2)
($$) ((t, a1) -> SignalBase t a1
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit,(t, a2) -> SignalBase t a2
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit)
unit3 :: ((t, a1), (t, a2), (t, a3))
-> (SignalBase t a1, SignalBase t a2, SignalBase t a3)
unit3 = ((t, a1) -> SignalBase t a1, (t, a2) -> SignalBase t a2,
(t, a3) -> SignalBase t a3)
-> ((t, a1), (t, a2), (t, a3))
-> (SignalBase t a1, SignalBase t a2, SignalBase t a3)
forall t1 a t2 b t3 c.
(t1 -> a, t2 -> b, t3 -> c) -> (t1, t2, t3) -> (a, b, c)
($$$) ((t, a1) -> SignalBase t a1
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit,(t, a2) -> SignalBase t a2
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit,(t, a3) -> SignalBase t a3
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit)
unit4 :: ((t, a1), (t, a2), (t, a3), (t, a4))
-> (SignalBase t a1, SignalBase t a2, SignalBase t a3,
SignalBase t a4)
unit4 = ((t, a1) -> SignalBase t a1, (t, a2) -> SignalBase t a2,
(t, a3) -> SignalBase t a3, (t, a4) -> SignalBase t a4)
-> ((t, a1), (t, a2), (t, a3), (t, a4))
-> (SignalBase t a1, SignalBase t a2, SignalBase t a3,
SignalBase t a4)
forall t1 a t2 b t3 c t4 d.
(t1 -> a, t2 -> b, t3 -> c, t4 -> d)
-> (t1, t2, t3, t4) -> (a, b, c, d)
($$$$) ((t, a1) -> SignalBase t a1
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit,(t, a2) -> SignalBase t a2
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit,(t, a3) -> SignalBase t a3
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit,(t, a4) -> SignalBase t a4
forall t a. (Num t, Ord t, Eq t) => (t, a) -> SignalBase t a
unit)
infinite :: (Num t, Ord t, Eq t) => a -> SignalBase t a
infinite :: a -> SignalBase t a
infinite v :: a
v = t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE 0 a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- SignalBase t a
forall e. Stream e
NullS
signal :: (Num t, Ord t, Eq t) => [(t, a)] -> SignalBase t a
signal :: [(t, a)] -> SignalBase t a
signal = SignalBase t a -> SignalBase t a
forall t a. (Num t, Ord t) => Stream (DE t a) -> Stream (DE t a)
checkSignal (SignalBase t a -> SignalBase t a)
-> ([(t, a)] -> SignalBase t a) -> [(t, a)] -> SignalBase t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [DE t a] -> SignalBase t a
forall a. [a] -> Stream a
stream ([DE t a] -> SignalBase t a)
-> ([(t, a)] -> [DE t a]) -> [(t, a)] -> SignalBase t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((t, a) -> DE t a) -> [(t, a)] -> [DE t a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(t :: t
t, v :: a
v) -> t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
t a
v)
until :: (Num t, Ord t, Eq t) => t -> SignalBase t a -> SignalBase t a
until :: t -> SignalBase t a -> SignalBase t a
until _ NullS = SignalBase t a
forall e. Stream e
NullS
until u :: t
u (DE t :: t
t v :: a
v:-NullS)
| t
t t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
u = t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
t a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
u a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- SignalBase t a
forall e. Stream e
NullS
| Bool
otherwise = t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
u a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- SignalBase t a
forall e. Stream e
NullS
until u :: t
u (DE t :: t
t v :: a
v:-xs :: SignalBase t a
xs)
| t
t t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
u = t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
t a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- t -> SignalBase t a -> SignalBase t a
forall t a.
(Num t, Ord t, Eq t) =>
t -> SignalBase t a -> SignalBase t a
until t
u SignalBase t a
xs
| Bool
otherwise = t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
u a
v DE t a -> SignalBase t a -> SignalBase t a
forall e. e -> Stream e -> Stream e
:- SignalBase t a
forall e. Stream e
NullS
readSignal :: (Num t, Ord t, Eq t, Read t, Read a) => String -> SignalBase t a
readSignal :: [Char] -> SignalBase t a
readSignal s :: [Char]
s = SignalBase t a -> SignalBase t a
forall t a. (Num t, Ord t) => Stream (DE t a) -> Stream (DE t a)
checkSignal (SignalBase t a -> SignalBase t a)
-> SignalBase t a -> SignalBase t a
forall a b. (a -> b) -> a -> b
$ [Char] -> SignalBase t a
forall a. Read a => [Char] -> a
read [Char]
s
checkSignal :: Stream (DE t a) -> Stream (DE t a)
checkSignal NullS = Stream (DE t a)
forall e. Stream e
NullS
checkSignal s :: Stream (DE t a)
s@(x :: DE t a
x:-_)
| DE t a -> t
forall t a. DE t a -> t
tag DE t a
x t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = Stream (DE t a) -> Stream (DE t a)
forall t a. Ord t => Stream (DE t a) -> Stream (DE t a)
checkOrder Stream (DE t a)
s
| Bool
otherwise = [Char] -> Stream (DE t a)
forall a. HasCallStack => [Char] -> a
error "[MoC.DE] signal does not start from global 0"
where
checkOrder :: Stream (DE t a) -> Stream (DE t a)
checkOrder NullS = Stream (DE t a)
forall e. Stream e
NullS
checkOrder (x :: DE t a
x:-NullS) = (DE t a
xDE t a -> Stream (DE t a) -> Stream (DE t a)
forall e. e -> Stream e -> Stream e
:-Stream (DE t a)
forall e. Stream e
NullS)
checkOrder (x :: DE t a
x:-y :: DE t a
y:-xs :: Stream (DE t a)
xs) | DE t a -> t
forall t a. DE t a -> t
tag DE t a
x t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< DE t a -> t
forall t a. DE t a -> t
tag DE t a
y = DE t a
x DE t a -> Stream (DE t a) -> Stream (DE t a)
forall e. e -> Stream e -> Stream e
:-Stream (DE t a) -> Stream (DE t a)
checkOrder (DE t a
yDE t a -> Stream (DE t a) -> Stream (DE t a)
forall e. e -> Stream e -> Stream e
:-Stream (DE t a)
xs)
| Bool
otherwise = [Char] -> Stream (DE t a)
forall a. HasCallStack => [Char] -> a
error "[MoC.DE] malformed signal"
infixl 7 %>
(DE t :: t
t _) %> :: DE t a -> DE t a -> DE t a
%> (DE _ x) = t -> a -> DE t a
forall t a. (Num t, Ord t, Eq t) => t -> a -> DE t a
DE t
t a
x