0

I'm trying to learn to use plated to transform and search through the python AST generated by language-python (https://hackage.haskell.org/package/language-python-0.5.4/docs/Language-Python-Common-AST.html)

To briefly summarize the types:

  • Modules is a list of statements
  • A statement can contain further statements or expressions
  • An expression can contain further expressions, identities or operators

I created a hello world to get started with these two libraries. Here's my code that defines plate for a subset of the python AST types:

{-# LANGUAGE FlexibleInstances#-}
module Lib
    ( someFunc
    ) where
import Language.Python.Version3.Parser
import Language.Python.Common.Token
import Language.Python.Common.AST
import Language.Python.Common.SrcLocation
import Control.Lens.Plated
import Data.Data.Lens
import Language.Python.Common.Pretty
import Language.Python.Common.PrettyAST


instance Plated (Statement SrcSpan) where
  plate = uniplate

instance Plated (Expr SrcSpan) where
  plate = uniplate

instance Plated (Ident SrcSpan) where
  plate = uniplate

instance Plated (Op SrcSpan) where
  plate = uniplate

extract (Right (x, _)) = x

someFunc :: IO ()
someFunc = do
  putStrLn $ show $ concatMap (map prettyText) $ universe $ extract $ parseStmt "2*(1+x)" "file.py"
  putStrLn $ show $ map prettyText $ universe $ extract $ parseExpr "2*(1+x)" "file.py"

The output of this program is

["2 * (1 + x)"]
["2 * (1 + x)","2","(1 + x)","1 + x","1","x"]

The problem I'm seeing is that plated only sees the type consistent with the root node. If I start with a statement as my root node it doesn't look at expressions which are children of the statement. In the second case where I look at expressions it seems to descend and find child expressions. But, it's not showing me the operators or identity types that are children of expressions.

How do get plate to descend into a data structure with mixed types? Or am I using the wrong tool for this?

willtalmadge
  • 406
  • 3
  • 7
  • The type of `plate` requires the children to be the same type as the parent. You'll need to find a different approach. – Carl Nov 20 '17 at 23:32
  • You haven't actually stated what you hope to achieve so it's hard to say what technology you should use. Your types all have `Data` instances; I'd suggest looking at that. However, a function which returns all subexpressions (even with differing types) as a list cannot exists, since lists are homogeneous. – user2407038 Nov 20 '17 at 23:51
  • Sorry the goal is kind of fuzzy, it's a research project idea. Ultimately what I'm interested in is converting the AST into a dataflow graph. Nodes would be expressions and edges would represent data dependencies between expressions. – willtalmadge Nov 21 '17 at 01:15

0 Answers0