shithub: MicroHs

Download patch

ref: 25faeffbcee9b8006aa5e8083ea81eadcca8367e
parent: 392a92e391d27510e076d3c337425e572e1d6716
author: Lennart Augustsson <lennart.augustsson@epicgames.com>
date: Mon Feb 5 15:20:47 EST 2024

More Read

--- a/lib/Data/Enum.hs
+++ b/lib/Data/Enum.hs
@@ -68,3 +68,13 @@
 instance Enum Char where
   fromEnum = primOrd
   toEnum   = primChr
+
+
+instance Enum Ordering where
+  fromEnum LT = (0::Int)
+  fromEnum EQ = (1::Int)
+  fromEnum GT = (2::Int)
+  toEnum i =      if i `primIntEQ` 0 then LT
+             else if i `primIntEQ` 1 then EQ
+             else if i `primIntEQ` 2 then GT
+             else error "Ord.toEnum: out of range"
--- a/lib/Data/Ord.hs
+++ b/lib/Data/Ord.hs
@@ -46,4 +46,3 @@
 instance Bounded Ordering where
   minBound = LT
   maxBound = GT
-
--- a/lib/Text/Read.hs
+++ b/lib/Text/Read.hs
@@ -14,11 +14,13 @@
 import Control.Error
 import Data.Char
 import Data.Bool
+import Data.Either
 import Data.Eq
 import Data.Function
 import Data.List
 import Data.Maybe_Type
 import Data.Num
+import Data.Ord
 import Data.Int
 import Text.Read.Numeric
 import Text.Read.Lex
@@ -65,5 +67,18 @@
   readsPrec _ = readList
 
 instance Read Bool where
-  readsPrec _ s = [ (False, r) | ("False", r) <- lex s ] ++
-                  [ (True,  r) | ("True",  r) <- lex s ]
+  readsPrec _ = readBoundedEnum
+
+instance Read Ordering where
+  readsPrec _ = readBoundedEnum
+
+instance forall a . Read a => Read (Maybe a) where
+  readsPrec p u = [ (Nothing :: Maybe a, t) | ("Nothing", t) <- lex u ] ++
+                  readParen (p > 10) ( \ r ->
+                    [ (Just a,  t) | ("Just",    s) <- lex r, (a, t) <- readsPrec 11 s ]
+                    ) u
+
+instance forall a b . (Read a, Read b) => Read (Either a b) where
+  readsPrec p = readParen (p > 10) $ \ r ->
+                [ (Left  a, t) | ("Left",  s) <- lex r, (a, t) <- readsPrec 11 s ] ++
+                [ (Right b, t) | ("Right", s) <- lex r, (b, t) <- readsPrec 11 s ]
--- a/src/runtime/eval.c
+++ b/src/runtime/eval.c
@@ -2388,6 +2388,7 @@
       }
 
     case T_FROMUTF8:
+      if (doing_rnf) RET;
       CHECK(1);
       x = evali(ARG(TOP(0)));
       if (GETTAG(x) != T_STR) ERR("FROMUTF8");
--- a/tests/Read.hs
+++ b/tests/Read.hs
@@ -26,7 +26,7 @@
   print (read "-123"  :: Integer)
   print (read "(123)" :: Integer)
   print (read "1234567890123456789012345678901234567890" :: Integer)
-  print (read "[1,2,3]" :: [Int])
+  print (read "[1,2, 3]" :: [Int])
   print (read "False" :: Bool)
   print (read "True" :: Bool)
   print (reads "123 4" :: [(Int, String)])
@@ -37,3 +37,11 @@
   print (read "-1e+5" :: Double)
   print (read "1.5e+5" :: Double)
   print (read "5e-1" :: Double)
+  print (read "[EQ,GT,LT]" :: [Ordering])
+  print (read "Nothing" :: Maybe Int)
+  print (read "Just 123" :: Maybe Int)
+  print (read "Just (Just 123)" :: Maybe (Maybe Int))
+  print (read "Just Nothing" :: Maybe (Maybe Int))
+  print (readMaybe "Just Just 123" :: Maybe (Maybe (Maybe Int)))
+  print (read "Left True" :: Either Bool Int)
+  print (read "Right 123" :: Either Bool Int)
--- a/tests/Read.ref
+++ b/tests/Read.ref
@@ -27,3 +27,11 @@
 -100000.0
 150000.0
 0.5
+[EQ,GT,LT]
+Nothing
+Just 123
+Just (Just 123)
+Just Nothing
+Nothing
+Left True
+Right 123
--