shithub: MicroHs

Download patch

ref: 6007c49911b042c24c8a996a11c49a75e2264643
parent: eaa2e8d592b484b40269cf6b104d68cf7b230670
author: Lennart Augustsson <lennart@augustsson.net>
date: Thu Sep 26 06:54:35 EDT 2024

Add missing instance

--- a/lib/Data/Ratio.hs
+++ b/lib/Data/Ratio.hs
@@ -17,6 +17,7 @@
 import Data.Ord
 import Data.Ratio_Type
 import Data.Real
+import Data.RealFrac
 import Text.Show
 
 {- in Data.Ratio_Type
@@ -98,3 +99,16 @@
 
 denominator :: forall a . Ratio a -> a
 denominator (_ :% y) = y
+
+instance (Ord a, Integral a) => RealFrac (Ratio a)  where
+  properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
+    where (q, r) = quotRem x y
+  round r =
+    let
+      (n, f) = properFraction r
+      x = if r < 0 then -1 else 1
+    in  case (compare (abs f) 0.5, odd n) of
+          (LT, _) -> n
+          (EQ, False) -> n
+          (EQ, True) -> n + x
+          (GT, _) -> n + x
--