Why Frege doesn't provide a read function

44 views
Skip to first unread message

zhou...@163.com

unread,
Jan 25, 2018, 1:02:57 AM1/25/18
to Frege Programming Language
I want to read a simple literal [Byte] into Frege, to my surprised there is no read available

Ingo W.

unread,
Jan 27, 2018, 12:32:12 PM1/27/18
to Frege Programming Language
Right.
The whole Read class is missing/incomplete.

Marimuthu Madasamy

unread,
Jan 27, 2018, 3:47:17 PM1/27/18
to Frege Programming Language
As a workaround, you can, of course, use the native implementation:

pure native parseByte java.lang.Byte.parseByte :: String -> (NumberFormatException | Byte)


zhou...@163.com

unread,
Jan 27, 2018, 10:54:52 PM1/27/18
to Frege Programming Language
Not a bad thing, instead I use Frege prelude NanoParsec library and quickly build a parser and it is fantasy

zhou...@163.com

unread,
Jan 27, 2018, 11:01:18 PM1/27/18
to Frege Programming Language
In fact, I use NanoParsec and now it is flexible enough to parse things like "apdu #x3F #xbE #xEf", I include the whole source, hope it helps

import froid.util.Log
import frege.data.Char hiding (isNumber)
import frege.data.NanoParsec
import Control.Concurrent as C
import io.github.mchav.tryfrege.Utilities

data Command = Apdu [Byte]
             | Reset
             | Finish
             | Exception

derive Show Command

hexdigit    = satisfy (Char.isHexDigit   :: Char -> Bool)

h1 :: Parser [] Char [Char]
h1 = do
  a <- expect '#'
  b <- expect 'x'
  c <- hexdigit
  d <- hexdigit
  return $ [c, d]

h2 :: Parser [] Char [[Char]]
h2 = do
  spaces *> h1 `sepBy` spaces

h3 :: Parser [] Char [[Char]]
h3 = do
  a <- spaces *> some letter <* spaces
  b <- h2
  return $ a:b

h4 :: Parser [] Char [[Char]]
h4 = do
  spaces *> h3 <* spaces

parseDriver :: Parser [] Char [[Char]] -> ([] Char) -> IO [[Char]]
parseDriver p s = case runid p s of
    (Left msg, str) -> do
      error $ "Parse failed!" ++ (reporterror str msg)

    (Right r, str) -> do
        if (null str) then return r
          else error $ "Parse failed: " ++ (reporterror str "Tokens left")

digitToByte = intToByte . digitToInt

parseJobs input = do
  r <- parseDriver h4 (unpacked input)
  case r of
    ['a','p','d','u']:d -> return $ Apdu $ map (\[a,b]-> (digitToByte a) * 16 + digitToByte b) d
    [['r','e','s','e','t']] -> return Reset
    [['f','i','n','i','s','h']] -> return Finish
    [['e','x','c','e','p','t','i','o','n']] -> return Exception
    _ -> return Exception


在 2018年1月28日星期日 UTC+8上午4:47:17,Marimuthu Madasamy写道:

Tony Morris

unread,
Jan 28, 2018, 9:17:49 PM1/28/18
to frege-program...@googlegroups.com

I concur that the Read type-class is terrible and can always be replaced with an improvement. It was deleted from our default Haskell prelude 10 years, and not a single time have we missed it.

--
You received this message because you are subscribed to the Google Groups "Frege Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-l...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc
Reply all
Reply to author
Forward
0 new messages