Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Future a = At Nat a | Never
  2.  
  3. implementation Functor Future where
  4.     map f (At t v) = At t (f v)
  5.     map _ Never = Never
  6.  
  7. data AndOr a b = This a | These a b | That b
  8.  
  9. race : Future a -> Future b -> Future (AndOr a b)
  10. race (At ta a) (At tb b) = At (min ta tb) $ case compare ta tb of
  11.     LT => This a
  12.     EQ => These a b
  13.     GT => That b
  14. race (At t v) Never = At t (This v)
  15. race Never (At t v) = At t (That v)
  16. race Never Never = Never
  17.  
  18. mutual
  19.     record Reactive a where
  20.         constructor MkReactive
  21.         initial : a
  22.         next : Event a
  23.    
  24.     record Event a where
  25.         constructor MkEvent
  26.         unEvent : Future (Reactive a)
  27.  
  28. mutual
  29.     implementation Functor Reactive where
  30.         map f (MkReactive a next) = MkReactive (f a) $ map f next
  31.        
  32.     implementation Functor Event where
  33.         map f (MkEvent a) = assert_total $ MkEvent $ map (map f) a
  34.  
  35. implementation Applicative Reactive where
  36.     pure a = MkReactive a (MkEvent Never)
  37.     rf@(MkReactive f (MkEvent nf)) <*> rv@(MkReactive v (MkEvent nv)) = assert_total $ MkReactive (f v) $ MkEvent $ flip map (race nf nv) $ \x => case x of
  38.         This rf' => rf' <*> rv
  39.         These rf' rv' => rf' <*> rv'
  40.         That rv' => rf <*> rv'
  41.  
  42. implementation Monad Reactive where
  43.     join (MkReactive (MkReactive v vs) rs) = assert_total $ MkReactive v $ MkEvent $
  44.         flip map (race (unEvent vs) (unEvent rs)) $ \x => case x of
  45.             This vr' => join (MkReactive vr' rs)
  46.             These vr' _ => join (MkReactive vr' rs)
  47.             That rs' => join rs'
  48.  
  49. rA : Reactive String
  50. rA = MkReactive "IA" (MkEvent (At 1 (MkReactive "1A" (MkEvent Never))))
  51.  
  52. rB : Reactive String
  53. rB = MkReactive "IB" (MkEvent (At 1 (MkReactive "1B" (MkEvent Never))))
  54.  
  55.  
  56. rC1 : Reactive String
  57. rC1 = (++) <$> rA <*> rB
  58. --rC1 = MkReactive "IAIB" (MkEvent (At 1 (MkReactive "1A1B" (MkEvent Never)))) : Reactive String
  59.  
  60. rC2 : Reactive String
  61. rC2 = rA >>= (\a => (a++) <$> rB)
  62. -- MkReactive "IAIB" (MkEvent (At 1(MkReactive "IA1B" (MkEvent (At 1 (MkReactive "1AIB" (MkEvent (At 1 (MkReactive "1A1B" (MkEvent Never)))))))))) : Reactive String
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement