Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1.  
  2. -- Lab2
  3.  
  4. --------------------------------------------------------------------------------
  5. -- H2(h,Op,op) =∀x.∀y.h(Op x y)op(h x) (h y)
  6.  
  7. -- 1.
  8. -- The evaluation of the second derivative is given by: eval'' = eval' . derive = eval . derive . derive
  9.  
  10. -- (a) Show that eval'' is not a homomorphism from FunExp to FunSem = R -> R.
  11.  
  12. {-
  13.  
  14. eval'' :: FunExp -> FunSem
  15. eval'' = eval' . derive == eval . derive . derive
  16.  
  17. -}
  18.  
  19. {-# LANGUAGE FlexibleInstances #-}
  20. -- Allows instance declarations to take synonym types as argument
  21. -- (Tri a) instead of (a,a,a)
  22. -------------------------------------------------------------------------------
  23. -- Test FunExps
  24.  
  25. fe1 = Const 5 -- 5
  26. fe2 = Id :+: (Id :*: Const 3) :+: Const 7 -- x + 3x + 7 = 4x + 7
  27. fe3 = (Id :*: Id :*: Id) :+: (Const 3 :*: Id :*: Id) :+: Id :+: Const 12 -- x^3 + 3x^2 + x + 12
  28. fe4 = Id :*: Id :*: Id :*: Id -- x^4
  29.  
  30. -------------------------------------------------------------------------------
  31. -- Types
  32.  
  33. type Tri a = (a, a, a)
  34. type TriFun a = Tri (a -> a) -- ((a -> a), (a -> a), (a -> a))
  35. type FunTri a = a -> Tri a -- a -> (a, a, a)
  36.  
  37. -------------------------------------------------------------------------------
  38. -- Data
  39.  
  40. data FunExp = Const Rational
  41. | Id
  42. | FunExp :+: FunExp
  43. | FunExp :*: FunExp
  44. | Exp FunExp
  45. deriving (Eq, Show)
  46.  
  47. -------------------------------------------------------------------------------
  48. -- Instance declarations
  49.  
  50. instance Num a => Num (Tri a) where
  51. (+) = plusT
  52. (*) = mulT
  53. (-) = minT
  54. negate = negateT
  55. abs = absT
  56. signum = signumT
  57. fromInteger = undefined
  58.  
  59. instance Fractional a => Fractional (Tri a) where
  60. (/) = divT
  61. recip = recipT
  62. fromRational = fromRationalT
  63.  
  64. instance Floating a => Floating (Tri a) where
  65. pi = undefined
  66. exp = expT
  67. log = undefined
  68. sqrt = undefined
  69. (**) = undefined
  70. logBase = undefined
  71. sin = sinT
  72. cos = cosT
  73. tan = undefined
  74. asin = undefined
  75. acos = undefined
  76. atan = undefined
  77. sinh = undefined
  78. cosh = undefined
  79. tanh = undefined
  80. asinh = undefined
  81. acosh = undefined
  82. atanh = undefined
  83.  
  84.  
  85. -------------------------------------------------------------------------------
  86. -- Num Instances
  87.  
  88.  
  89. plusT :: Num a => (a,a,a) -> (a,a,a) -> (a,a,a)
  90. plusT (x, x', x'') (y, y', y'') = (x + y, x' + y', x'' + y'')
  91.  
  92. mulT :: Num a => (a,a,a) -> (a,a,a) -> (a,a,a)
  93. mulT (x, x', x'') (y, y', y'') = ( x * y,
  94. (x * y') + (x' * y),
  95. (x'' * y) + (x' * y') + (x' * y') + (x * y'')
  96. )
  97.  
  98. minT :: Num a => (a,a,a) -> (a,a,a) -> (a,a,a)
  99. minT (x, x', x'') (y, y', y'') = (x - y, x' - y', x'' - y'')
  100.  
  101. negateT :: Num a => (a,a,a) -> (a,a,a)
  102. negateT (x, x', x'') = (negate x, negate x', negate x'')
  103.  
  104. absT :: Num a => (a,a,a) -> (a,a,a)
  105. absT (x, x', x'') = (abs x, abs x', abs x'')
  106.  
  107. signumT :: Num a => (a,a,a) -> (a,a,a)
  108. signumT (x, x', x'') = (signum x, signum x', signum x'')
  109.  
  110. -------------------------------------------------------------------------------
  111. -- Fractional Instances
  112.  
  113. divT :: Fractional a => (a,a,a) -> (a,a,a) -> (a,a,a)
  114. divT (x, x', x'') (y, y', y'') = ( (x / y),
  115. (x'*y - x*y') / (y*y),
  116. (x''*y*y - 2*y*y'*x' - y''*y*x + 2*y'*y'*x) / (y*y*y))
  117.  
  118. recipT :: Fractional a => (a,a,a) -> (a,a,a)
  119. recipT (x, x', x'') = (recip x, recip x', recip x'')
  120.  
  121. fromRationalT :: Fractional a => Rational -> Tri a
  122. fromRationalT x = (fromRational x, 0, 0)
  123.  
  124. -------------------------------------------------------------------------------
  125. -- Floating Instances
  126.  
  127. expT :: Floating a => (a,a,a) -> (a,a,a)
  128. expT (x, x', x'') = (exp x, exp x', exp x'')
  129.  
  130. sinT :: Floating a => (a,a,a) -> (a,a,a)
  131. sinT (x, x', x'') = (sin x, x' * cos x, (x'' * cos x) + (x' * x' * (- sin x)) )
  132.  
  133. cosT :: Floating a => (a,a,a) -> (a,a,a)
  134. cosT (x, x', x'') = (cos x, x' * (- sin x), - ((x'' * sin x) + (x'*x'*cos x) ))
  135.  
  136. -------------------------------------------------------------------------------
  137. -- derive FunExp
  138.  
  139. derive :: FunExp -> FunExp
  140. derive (Const c) = Const 0
  141. derive Id = Const 1
  142. derive (x :+: y) = derive x :+: derive y
  143. derive (x :*: y) = (derive x :*: y) :+: (derive y :*: x)
  144. derive (Exp x) = derive x :*: Exp x
  145.  
  146.  
  147. simplify :: FunExp -> FunExp
  148. simplify x = if ((simplify' x) /= x)
  149. then simplify (simplify' x)
  150. else x
  151.  
  152. simplify' :: FunExp -> FunExp
  153. simplify' (Const x) = Const x
  154. simplify' (Id) = Id
  155. simplify' (Exp x) = Exp (simplify' x)
  156.  
  157. simplify' (Const 0 :+: x) = simplify' x
  158. simplify' (x :+: Const 0) = simplify' x
  159. simplify' (Const x :+: Const y) = Const (x + y)
  160. simplify' (Id :+: Id) = (Const 2 :*: Id)
  161. simplify' (Id :+: Const x) = (Id :+: Const x)
  162. simplify' (Const x :+: Id) = (Id :+: Const x)
  163.  
  164.  
  165. simplify' (Const 0 :*: _) = Const 0
  166. simplify' (_ :*: Const 0) = Const 0
  167. simplify' (Const 1 :*: x) = simplify' x
  168. simplify' (x :*: Const 1) = simplify' x
  169. simplify' (Const x :*: Const y) = Const (x * y)
  170. simplify' (Id :*: Id) = Id :*: Id
  171. simplify' (Const x :*: Id) = Const x :*: Id
  172.  
  173. simplify' (x :*: (y :+: z)) = simplify' (simplify' (x :*: y)) :+: (simplify' (x :*: z))
  174. simplify' ((x :+: y) :*: z) = simplify' (simplify' (x :*: z)) :+: (simplify' (y :*: z))
  175. simplify' (Id :*: Const x) = Const x :*: Id
  176.  
  177. simplify' (x :*: y) = (simplify' x :*: simplify' y)
  178. simplify' (x :+: y) = if (simplify' x == simplify' y)
  179. then (Const 2 :*: simplify' x)
  180. else (simplify' x :+: simplify' y)
  181.  
  182. show' :: FunExp -> String
  183. show' (Const x) = "(" ++ show x ++ ")"
  184. show' (Id) = "x"
  185. show' (Exp f) = "e^" ++ "(" ++ show' f ++ ")"
  186. show' (x :+: y) = (show' x) ++ " + " ++ (show' y)
  187. show' (x :*: y) = (show' x) ++ " * " ++ (show' y)
  188.  
  189. d :: FunExp -> FunExp
  190. d = simplify . derive
  191.  
  192. sh :: FunExp -> String
  193. sh = show' . d
  194. -------------------------------------------------------------------------------
  195. -- EvalDD
  196.  
  197. evalDD :: (Floating a, Num a) => FunExp -> FunTri a -- FunExp -> (a -> (a,a,a))
  198. evalDD (Const c) = \x -> fromRationalT c
  199. evalDD Id = \x -> (x, 1, 0)
  200. evalDD (a :+: b) = (evalDD (a + b))
  201. evalDD (a :*: b) = undefined
  202. evalDD (Exp a) = undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement