소스를 AST로 구문 분석하는 데 많은 Haskell
자습서가 있습니다. AST는 AST 평가를 다시 작성하지만 두 AST 간의 매핑은 작성하지 않습니다. Haskell
에 transformFoo2Bar
을 구현하는 "정식"방법이 있습니까?하스켈에서 AST 변환하기
type FooIdentifier = String
data Foo = Foo FooIdentifier [FooMethod] deriving (Show)
data FooMethod = FooMethod FooIdentifier [FooExpression] deriving (Show)
data FooExpression = FooAB FooIdentifier FooIdentifier | FooZ FooIdentifier deriving (Show)
type BarIdentifier = String
type BarLabel = String
data Bar = Bar BarIdentifier [BarExpression] deriving (Show)
data BarExpression = BarA BarIdentifier BarIdentifier
| BarB BarIdentifier BarIdentifier | BarZ BarIdentifier deriving (Show)
--
-- transformFoo2Bar [Foo "foo" [FooMethod "foo1" [FooAB "a" "b", FooZ "z"], FooMethod "foo2" [FooZ "z"]]]
--
-- to evaluate to
--
-- [Bar "foo_foo1" [BarA "a" "b", BarB "a" "b", BarZ "z"], Bar "foo_foo2" [BarZ "z"]]
--
transformFoo2Bar :: [Foo] -> [Bar]
transformFoo2Bar = undefined
코드 컴파일과 유사하지만 컴파일 된 코드를 내보내는 대신 결과를 AST로 유지합니다.
또한 이러한 매핑 중 일부를 역 매핑으로 다시 사용할 수 있습니까?
감사
저는 질문을 이해하지 못합니다 : 유형이 구조적으로 동등한 다른 AST로 AST를 변형하는 체계적인 방법이 있는지 묻고 싶습니까? – didierc
질문이 매우 광범위하고 상황에 따라 매우 다르게 대답 될 것이므로 많은 답변을 얻지 못했다고 생각합니다. 실제로 광범위하게, 사실, 전체 규율은 물건을 물건으로 번역하는 데 전념합니다 .... 기능적인 프로그래밍. – jamshidh
당신이 AST를 찾고 있지 않다고 생각합니다 -> AST - 구조를 기반으로 유형을 변환하는 방법을 찾고있는 것처럼 보입니다 - 그래서 원하는 것은 구조적 타이핑이나 해결 방법입니다 (맞다면). 이미 https://stackoverflow.com/questions/21071603/why-does-haskell-not-have-records-with-structural-typing에 대한 질문이 있습니다. 그리고 hackage에 대한 몇 가지 실험이 있습니다 : http : // hackage. haskell.org/package/shapely-data-0.0 - 거기에 좋은 것이 있는지 모르겠다. – Carsten