如何在desugaring箭头语法后获取haskell代码? [英] How to get haskell code after desugaring arrow syntax?

查看:141
本文介绍了如何在desugaring箭头语法后获取haskell代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前尝试解决我的问题 HXT:Can an输入更改为箭头语法?,因此希望在ghc编译器解析箭头语法。我怎么做到这一点?

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?

我已经尝试过 -ddump-ds 因为所有类型都解决了。有没有办法只用箭头语法desugaring来查看代码?

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?

推荐答案

原始arrow项目提供了一个解析器,称为 arrowp ,它是可用于Hackage 并将箭头语法翻译为Haskell98:

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



示例



已从您的其他问题中获取。

{-# 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



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 ()

这篇关于如何在desugaring箭头语法后获取haskell代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆