How does readsPrec and the relative functions return [Red] for read “[Red]” :: [Color]

戏子无情 提交于 2020-01-03 06:51:08

问题


This question is a continuation of what happens when executing (read "[Red]") :: [Color] under ghci?. From user5402's answer, I know that there is a very complex execution path for read "[Red]" :: [Color] , which includes readsPrec and readsPrecList. According to @user5402's comments, readsPrecList call the readsPrec, so readsPrec returns [(Red,"]")] to readsPrecList and then we will get the final result [Red] from readsPrecList. However, I still cannot understand what function of the link corresponds to his readsPrecList and its implementation details.


回答1:


The relevant definitions are available in the Report, and look like this:

read             :: (Read a) => String -> a
read s           =  case [x | (x,t) <- reads s, ("","") <- lex t] of
                         [x] -> x
                         []  -> error "Prelude.read: no parse"
                         _   -> error "Prelude.read: ambiguous parse"

reads            :: (Read a) => ReadS a
reads            =  readsPrec 0

instance  (Read a) => Read [a]  where
    readsPrec p      = readList

class  Read a  where
    readsPrec        :: Int -> ReadS a
    readList         :: ReadS [a]
    readList         = readParen False (\r -> [pr | ("[",s)  <- lex r,
                                                    pr       <- readl s])
                       where readl  s = [([],t)   | ("]",t)  <- lex s] ++
                                        [(x:xs,u) | (x,t)    <- reads s,
                                                    (xs,u)   <- readl' t]
                             readl' s = [([],t)   | ("]",t)  <- lex s] ++
                                        [(x:xs,v) | (",",t)  <- lex s,
                                                    (x,u)    <- reads t,
                                                    (xs,v)   <- readl' u]

readParen        :: Bool -> ReadS a -> ReadS a
readParen b g    =  if b then mandatory else optional
                    where optional r  = g r ++ mandatory r
                          mandatory r = [(x,u) | ("(",s) <- lex r,
                                                 (x,t)   <- optional s,
                                                 (")",u) <- lex t    ]

The implementation of lex is much too large to include here -- it lexes Haskell.

A longish piece of equational reasoning below traces the full evaluation. I assume the implementation of the Read Color instance is the derived one. Since the interest here is the connection between lists and non-lists, I elide the details of expanding and evaluating reads at the base type Color.

reads "[Red]" :: [([Color], String)]
= { definition of reads }
readsPrec 0 "[Red]"
= { definition of readsPrec @[Color] }
readList "[Red]"
= { definition of readList @Color }
readParen False (\r -> [pr | ("[",s)  <- lex r, pr <- readl s]) "[Red]"
    where readl  s = [([],t)   | ("]",t)  <- lex s] ++
                     [(x:xs,u) | (x,t)    <- reads s,
                                 (xs,u)   <- readl' t]
          readl' s = [([],t)   | ("]",t)  <- lex s] ++
                     [(x:xs,v) | (",",t)  <- lex s,
                                 (x,u)    <- reads t, 
                                 (xs,v)   <- readl' u]
= { definition of readParen }
(\r -> [pr | ("[",s) <- lex r, pr <- readl s] ++ mandatory r) "[Red]"
    where readl  s = [([],t)   | ("]",t)  <- lex s] ++
                     [(x:xs,u) | (x,t)    <- reads s,
                                 (xs,u)   <- readl' t]
          readl' s = [([],t)   | ("]",t)  <- lex s] ++
                     [(x:xs,v) | (",",t)  <- lex s,
                                 (x,u)    <- reads t, 
                                 (xs,v)   <- readl' u]
          mandatory r = [(x,u) | ("(",s) <- lex r,
                                 (x,t)   <- optional s,
                                 (")",u) <- lex t]
= { beta reduction }
[pr | ("[",s) <- lex "[Red]", pr <- readl s] ++ mandatory "[Red]"
    where {- same as before -}
= { evaluation of `mandatory "[Red]"` and `(++)` }
[pr | ("[",s) <- lex "[Red]", pr <- readl s]
    where {- same as before -}
= { lex "[Red]" = [("[", "Red]")] }
[pr | pr <- readl "Red]"]
    where {- same as before -}
= { there's a name for this kind of reduction, but I don't know it }
readl "Red]"
    where {- same as before -}
= { definition of readl }
[([],t)   | ("]",t)  <- lex "Red]"] ++
[(x:xs,u) | (x,t)    <- reads "Red]",
            (xs,u)   <- readl' t]
where
          readl' s = [([],t)   | ("]",t)  <- lex s] ++
                     [(x:xs,v) | (",",t)  <- lex s,
                                 (x,u)    <- reads t, 
                                 (xs,v)   <- readl' u]
= { lex "Red]" = [("Red", "]")] plus evaluation of (++) }
[(x:xs,u) | (x,t)    <- reads "Red]",
            (xs,u)   <- readl' t]
where {- same as before -}
= { reads "Red]" = [(Red, "]")] }
[(Red:xs,u) | (xs,u) <- readl' "]"]
where {- same as before -}
= { definition of readl' }
[(Red:xs,u) | (xs,u) <- [([],t)   | ("]",t)  <- lex "]"] ++
                        [(x:xs,v) | (",",t)  <- lex "]",
                                    (x,u)    <- reads t, 
                                    (xs,v)   <- readl' u]]
where {- same as before -}
= { lex "]" = [("]", "")] }
[(Red:xs,u) | (xs,u) <- [([],"")] ++ []]
= { evaluation of (++) and the list comprehension }
[([Red],"")]

We can use this derivation as a building block for evaluating read, since that's your top-level question.

read "[Red]" :: [Color]
= { definition of read }
case [x | (x,t) <- reads "[Red]", ("","") <- lex t] of
    [x] -> x
    []  -> error "Prelude.read: no parse"
    _   -> error "Prelude.read: ambiguous parse"
= { reads "[Red]" = [([Red], "")] }
case [[Red] | ("","") <- lex ""] of
    [x] -> x
    []  -> error "Prelude.read: no parse"
    _   -> error "Prelude.read: ambiguous parse"
= { lex "" = [("", "")] }
case [[Red]] of
    [x] -> x
    []  -> error "Prelude.read: no parse"
    _   -> error "Prelude.read: ambiguous parse"
= { again there's a name for case reduction but I don't know it }
[Red]


来源:https://stackoverflow.com/questions/27465001/how-does-readsprec-and-the-relative-functions-return-red-for-read-red

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!