ast-path 0.1.2 → 0.2.0
raw patch · 5 files changed
+43/−2 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.ASTPath: nullPath :: a -> String -> ([ASTPath], [HalfPath])
+ Data.ASTPath: toList :: ASTPath -> [String]
Files
- ChangeLog.md +5/−0
- README.md +4/−0
- ast-path.cabal +2/−2
- src/Data/ASTPath.hs +12/−0
- test/Main.hs +20/−0
ChangeLog.md view
@@ -1,3 +1,8 @@+## 0.2.0++- Add `nullPath` for auxiliary types.+- Add `toList :: ASTPath -> [String]`.+ ## 0.1.0 Initial version
README.md view
@@ -5,6 +5,10 @@ AST-path is described in the paper "A General Path-Based Representation for Predicting Program Properties" (PLDI'2018) <https://arxiv.org/abs/1803.09544> and used at <https://code2vec.org/> and at <https://code2seq.org/>. +This package is a third-party implementation of AST-path.+But the idea of AST-path is not restricted to AST.+So the implementation of this package accepts any algebraic data types.+ ``` {-# LANGUAGE DeriveGeneric #-} import Data.ASTPath
ast-path.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 7286c954518553459704368e3c5eb84afe6a8f7181d03ce1aba6dda8e981cc73+-- hash: 967d6a87ca5791c1bad521afc79041930ac7ac90edf72e935cfce4feef04943e name: ast-path-version: 0.1.2+version: 0.2.0 synopsis: vocabulary representation for predicting program properties description: Please see the README on GitHub at <https://github.com/ijaketak/ast-path#readme>
src/Data/ASTPath.hs view
@@ -12,10 +12,12 @@ module Data.ASTPath ( type HalfPath , type ASTPath+ , toList , astPath , AST(..) , treePath , terminalPath+ , nullPath , AST'(..) ) where @@ -26,6 +28,12 @@ -- | (top node, [..., start node], [..., end node]) type ASTPath = (String, HalfPath, HalfPath) +toList :: ASTPath -> [String]+toList (t, ls, rs) = revApp ls $ t : rs+ where+ revApp [] ys = ys+ revApp (x:xs) ys = revApp xs $ x : ys+ -- | Typeclass for tree or terminal node. class AST a where {-# MINIMAL astPathWithHalf #-}@@ -44,6 +52,10 @@ -- | Typical implementation for terminal node type. terminalPath :: (a -> String) -> a -> String -> ([ASTPath], [HalfPath]) terminalPath f x _ = ([], [[f x]])++-- | No path implementation for auxiliary data type.+nullPath :: a -> String -> ([ASTPath], [HalfPath])+nullPath _ _ = ([], []) -- | Generate AST-paths from tree. astPath :: AST a => a -> [ASTPath]
test/Main.hs view
@@ -21,12 +21,18 @@ | Mul [Exp1 a] deriving (Eq, Show, Generic) +data AuxList a = Nil () | Cons a (AuxList a)+ deriving (Eq, Show, Generic)+ instance AST a => AST (Tree a) instance AST a => AST (Exp1 a) instance AST a => AST (Exp2 a)+instance AST a => AST (AuxList a) instance AST Int where astPathWithHalf = terminalPath show+instance AST () where+ astPathWithHalf = nullPath main :: IO () main = do@@ -88,4 +94,18 @@ (m, [p, a, v, "3"], [p, a, v, "2"]) `elem` path assertBool "path 10" $ (m, [p, a, v, "3"], [p, a, v, "1"]) `elem` path+ , testCase "list with auxiliary type" $ do+ let l :: AuxList Int+ l = Cons 1 $ Cons 2 $ Cons 3 $ Nil ()+ path = astPath l+ n = "Nil"+ c = "Cons"+ assertBool "path 1" $+ (c, ["2"], [c, "3"]) `elem` path+ assertBool "path 2" $+ (c, ["1"], [c, "2"]) `elem` path+ assertBool "path 3" $+ (c, ["1"], [c, c, "3"]) `elem` path+ assertBool "not contains ()" $+ "()" `notElem` concatMap toList path ]