desugaring arrow语法后如何获得haskell代码?

时间:2014-03-28 09:15:45

标签: haskell ghc arrows

我目前正在尝试解决我的问题HXT: Can an input change with the arrow syntax?,因此希望在ghc编译器去掉Arrow syntax之后看到haskell代码。我怎么能这样做?

我已经尝试了-ddump-ds但是有了这个标志,我得到了一个可怕的长代码,因为所有类型都被解析了。有没有办法看到代码只用箭头语法desugaring?

1 个答案:

答案 0 :(得分:6)

原始arrow project提供了一个名为arrowp的解析器available on Hackage,并将箭头语法转换为Haskell98:

cabal install arrowp
arrowp --help
arrowp source.hs > desugared.hs

实施例

来源取自your other question

source.hs

{-# LANGUAGE Arrows #-}
import Text.XML.HXT.Core

data Person = Person { forname :: String, surname :: String } deriving (Show)

parseXml :: IOSArrow XmlTree Person
parseXml = proc x -> do
    forname <- x >- this /> this /> hasName "fn" /> getText
    surname <- x >- this /> this /> hasName "sn" /> getText
    returnA -< Person forname surname

main :: IO ()
main = do
    person <- runX (readString [withValidate no]
                               "<p><fn>John</fn><sn>Smith</sn></p>"
                    >>> parseXml)
    putStrLn $ show person
    return ()

arrowp source.hs

{-# LINE 2 "source.hs" #-}
module Main (main) where
{-# LINE 2 "source.hs" #-}
import Text.XML.HXT.Core

{-# LINE 4 "source.hs" #-}
data Person = Person{forname :: String, surname :: String}
            deriving Show

{-# LINE 6 "source.hs" #-}
parseXml :: IOSArrow XmlTree Person
{-# LINE 7 "source.hs" #-}
parseXml
  = (arr (\ x -> (x, x)) >>>
       (first (this /> this /> hasName "fn" /> getText) >>>
          arr (\ (forname, x) -> (x, forname)))
         >>>
         (first (this /> this /> hasName "sn" /> getText) >>>
            arr (\ (surname, forname) -> Person forname surname)))

{-# LINE 12 "source.hs" #-}
main :: IO ()
{-# LINE 13 "source.hs" #-}
main
  = do person <- runX
                   (readString [withValidate no] "<p><fn>John</fn><sn>Smith</sn></p>"
                      >>> parseXml)
       putStrLn $ show person
       return ()