ap-reflect 0.1.0.0 → 0.2
raw patch · 4 files changed
+209/−4 lines, 4 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
- Debug.Reflect: Fn :: String -> (a -> b) -> ~> a b
+ Debug.Reflect: Fn :: String -> (a -> b) -> (~>) a b
Files
- CHANGELOG.md +4/−0
- README.md +57/−0
- ap-reflect.cabal +10/−4
- examples/examples.hs +138/−0
+ CHANGELOG.md view
@@ -0,0 +1,4 @@+0.2+---+* switched to new `base` (>= 4.7 && < 5)+
+ README.md view
@@ -0,0 +1,57 @@+# ap-reflect++[](https://travis-ci.org/cmc-msu-ai/ap-reflect)++Partial evaluation reflection a la simple-reflect.++## Installation++You can install the `ap-reflect` from Hackage:++ $ cabal install ap-reflect++or directly from the repository:++ $ git clone https://github.com/cmc-msu-ai/ap-reflect+ $ cd ap-reflect+ $ cabal install++For some possibilities of using this library+you may need to install `simple-reflect`.+It is available from+[Hackage](http://hackage.haskell.org/package/simple-reflect).+Install it, by typing:++ cabal install simple-reflect++## Documentation++Haddock documentation is available at+http://cmc-msu-ai.github.io/ap-reflect/doc/html/ap-reflect/++## Usage++For example:++```haskell+let (.+) = makeBinOp "+" (+)+mapM_ print . reductions $ (.+) -$- Just a -*- Just b+```++Result:++ (+) <$> Just a <*> Just b+ Just (a +) <*> Just b+ Just (a + b)++You can find another examples of using this library at+https://github.com/cmc-msu-ai/ap-reflect/tree/master/examples++## Contributors++* Oleg Baev+* Nickolay Kudasov++## Contribution++Contact me (Oleg Baev): odbaev@yandex.ru
ap-reflect.cabal view
@@ -1,7 +1,10 @@ name: ap-reflect -version: 0.1.0.0 +version: 0.2 synopsis: Partial evaluation reflection a la simple-reflect. --- description: +description: The library provides a simple reflection technique, + substituting functions like @fmap@ and @\<*\>@ with reflection-aware analogues. + + This library can be used with <http://hackage.haskell.org/package/simple-reflect simple-reflect package>. license: BSD3 license-file: LICENSE author: Oleg Baev @@ -9,9 +12,12 @@ homepage: https://github.com/cmc-msu-ai/ap-reflect bug-reports: https://github.com/cmc-msu-ai/ap-reflect/issues -- copyright: --- category: +category: Debug build-type: Simple cabal-version: >=1.10 +extra-source-files: README.md + , CHANGELOG.md + , examples/*.hs library default-language: Haskell2010 @@ -19,7 +25,7 @@ ghc-options: -Wall exposed-modules: Debug.Reflect -- other-modules: - build-depends: base ==4.6.* + build-depends: base >= 4.7 && < 5 Source-Repository head Type: git
+ examples/examples.hs view
@@ -0,0 +1,138 @@+{-# LANGUAGE TypeOperators #-} + +import Control.Applicative +import Control.Monad.Identity + +import Debug.SimpleReflect +import Debug.Reflect + +-- внутреннее представление операций + +-- (+) +(.+) :: (Num a, Show a) => a ~> a ~> a +(.+) = makeBinOp "+" (+) + +-- (-) +(.-) :: (Num a, Show a) => a ~> a ~> a +(.-) = makeBinOp "-" (-) + +-- (*) +(.*) :: (Num a, Show a) => a ~> a ~> a +(.*) = makeBinOp "*" (*) + +-- (/) +(./) :: (Fractional a, Show a) => a ~> a ~> a +(./) = makeBinOp "/" (/) + +-- (++) +(.++) :: (Show a) => [a] ~> [a] ~> [a] +(.++) = makeBinOp "++" (++) + +-- (:) +(.:) :: (Show a) => a ~> [a] ~> [a] +(.:) = makeBinOp ":" (:) + +sequenceA :: (Show a, Show (f a), Show (f [a]), Show (f ([a] ~> [a])), Applicative f) => [f a] -> Ap (f [a]) +sequenceA [] = pure'' [] +sequenceA (x:xs) = Val ap' :$ ((.:) -$- x) :$ sequenceA xs + +traverse :: (Show a, Show b, Show (f b), Show (f [b]), Show (f ([b] ~> [b])), Applicative f) => (a -> f b) -> [a] -> Ap (f [b]) +traverse _ [] = pure'' [] +traverse f (x:xs) = Val ap' :$ (Val fmap' :$ Val (.:) :$ fx) :$ traverse f xs + where fx = Val (Fn "f" f) :$ Val x + +instance Show m => Show (Const m a) where + show (Const x) = "Const " ++ addParens (show x) + where addParens s = if ' ' `elem` s then parens s else s + +instance Show a => Show (Identity a) where + show (Identity x) = "Identity " ++ addParens (show x) + where addParens s@(x:_) = if x /= '(' && ' ' `elem` s then parens s else s + +main :: IO () +main = do + + let line = putStrLn "------------------------------------------" + + let + g' = makeBinOp "g" (g :: Expr -> Expr -> Expr) + h' = makeBinOp "h" (h :: Expr -> Expr -> Expr) + mapM_ print . reductions $ Val [fromFn g' 1, fromFn h' 2] -*- [a, b, c] + line + + mapM_ print . reductions $ makeBinOp "f" (f :: Expr -> Expr -> Expr) -$- Just a -*- Just b + line + mapM_ print . reductions $ Val (Just (fromFn (.+) a)) -*- Just b + line + mapM_ print . reductions $ Val (Just (Fn "(+ a)" (+ a))) -*- Just b + line + + mapM_ print . reductions $ fmap'' (.+) (Just a) + line + mapM_ print . reductions $ fmap'' (.+) (Just a) -*- Just b + line + mapM_ print . reductions $ pure'' (.+) -*- Just a -*- Just b + line + + mapM_ print . reductions $ (.+) -$- Identity a + line + mapM_ print . reductions $ (.+) -$- Identity a -*- Identity b + line + + mapM_ print . reductions $ (.+) -$- Const (a + b) + line + mapM_ print . reductions $ (.+) -$- Const a -*- Const b + line + + mapM_ print . reductions $ (.+) -$- ZipList [1,2,3] -*- ZipList [100,100,100] + line + mapM_ print . reductions $ makeBinOp "max" max -$- ZipList [1,2,3,4,5,3] -*- ZipList [5,3,1,2] + line + mapM_ print . reductions $ Val (ZipList [fromFn (.+) 1, fromFn (.*) 100]) -*- ZipList [a,b] + line + + let f x = if even (length x) then Just (head x) else Nothing + mapM_ print . reductions $ traverse f ["ab","cdef","gh"] + line + mapM_ print . reductions $ sequenceA [Just a, Just b, Just c] + line + mapM_ print . reductions $ sequenceA [Just a, Nothing, Just c] + line + mapM_ print . reductions $ makeBinOp "f" (+) -$- [x, y] + line + + mapM_ print . reductions $ (.++) -$- Just "hello " -*- Just "world" + line + mapM_ print . reductions $ (.+) -$- ("1 + ", 1) -*- ("6 =", 6) + line + mapM_ print . reductions $ (.*) -$- Just 3 -*- Just 2 + line + mapM_ print . reductions $ (./) -$- Just 3 -*- Just 2 + line + mapM_ print . reductions $ (.+) -$- Just 1 + line + mapM_ print . reductions $ (.+) -$- Just 1 -*- Just 3 + line + mapM_ print . reductions $ (.+) -$- Just a -*- Just b + line + mapM_ print . reductions $ (.-) -$- Just a -*- Just b + line + mapM_ print . reductions $ (.+) -$- Nothing + line + mapM_ print . reductions $ (.+) -$- Just a -*- Nothing + line + mapM_ print . reductions $ (.+) -$- Nothing -*- Just b + line + mapM_ print . reductions $ (.+) -$- [1, 2, 3] + line + mapM_ print . reductions $ (.+) -$- [a, b] -*- [x, y] + line + mapM_ print . reductions $ (.+) -$- [] + line + mapM_ print . reductions $ (.+) -$- [1,2] -*- [] + line + mapM_ print . reductions $ (.+) -$- Right a -*- Left b + line + mapM_ print . reductions $ (.+) -$- Left a -*- Right b + line +