How to get haskell code after desugaring arrow syntax?

这一生的挚爱 提交于 2019-12-10 17:07:12

问题


I currently try to solve my problem HXT: Can an input change with the arrow syntax? and therefore and want to see the haskell code after the ghc compiler desugars the Arrow syntax. How can I do this?

I already tried -ddump-ds but with this flag I get a horrible long code because also all types are resolved. Is there a way to see the code with just arrow syntax desugaring?


回答1:


The original arrow project provided a parser, called arrowp, which is available on Hackage and translates the arrow syntax to Haskell98:

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

Example

Source has been taken from 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 ()


来源:https://stackoverflow.com/questions/22708509/how-to-get-haskell-code-after-desugaring-arrow-syntax

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