Advertisement
TurtlePU

Untitled

Jun 17th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Ring a where
  2.   (+) :: a -> a -> a
  3.   (*) :: a -> a -> a
  4.   zero :: a
  5.   one :: a
  6.   (-) :: a -> a -> a
  7.   fromInteger :: Integer -> a
  8.  
  9. newtype Poly x = Poly { eval :: Ring r => (x -> r) -> r } deriving Functor
  10.  
  11. instance Ring (Poly x) where
  12.   Poly p + Poly q = Poly (\x -> p x + q x)
  13.   Poly p * Poly q = Poly (\x -> p x * q x)
  14.   zero = Poly (const zero)
  15.   one = Poly (const one)
  16.   Poly p - Poly q = Poly (\x -> p x - q x)
  17.   fromInteger n = Poly (const $ fromInteger n)
  18.  
  19. instance Applicative Poly where
  20.   pure x = Poly (\xs -> xs x)
  21.   (<*>) = ap
  22.  
  23. instance Monad Poly where
  24.   Poly p >>= f = p f
  25.  
  26. @ :: Poly () -> Poly a -> Poly a
  27. @ p q = p >>= const q
  28.  
  29. poly :: (forall r . Ring r => (x -> r) -> r) -> Poly x
  30. poly = Poly
  31.  
  32. poly1 :: (forall r . Ring r => r -> r) -> Poly ()
  33. poly1 f = poly (\x -> f (x ()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement