Navigation Menu

Skip to content

Commit

Permalink
just making sure doc is in
Browse files Browse the repository at this point in the history
  • Loading branch information
jfischoff committed Oct 22, 2012
1 parent bddff5e commit bb444b3
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 27 deletions.
70 changes: 70 additions & 0 deletions README.md
@@ -0,0 +1,70 @@
Generate GDiff GADTs and Associated Instances
=============
gdiff-th is a library for generating the necessary apparatus for preforming type safe diffs and patches with the gdiff library.

If you are unfamiliar with the gdiff library it can be found here http://hackage.haskell.org/package/gdiff

###Usage

First install the library from here or from cabal.
#####With git
git clone https://github.com/jfischoff/gdiff-th
cd gdiff-th
cabal install

#####With cabal
cabal update
cabal install gdiff-th

Below is a simple example of how to use the library to view a colored diff.

module Example where
import Data.Generic.Diff
import Data.Generic.Diff.TH
import System.Console.Terminfo.Color
import Text.PrettyPrint.Free hiding (parens)
import System.Console.Terminfo.PrettyPrint

data Exp = Exp :+: Exp
| Exp :*: Exp
| B Integer
deriving(Show, Eq, Typeable)

-- Make the GDiff apparatus
makeGDiff ''Exp

testA :: Exp
testA = foldl1 (:+:) . map B $ [0..20]

testB :: Exp
testB = foldl1 (:+:) . map B $ [0..8] ++ [42] ++ [10..20]

-- Make a type signature to help inference
diffExp :: Type ExpFamily Exp => Exp -> Exp -> EditScript ExpFamily Exp Exp
diffExp = diff

diffAandB = showCompressed $ diffExp testA testB

main = diffAandB

-- Utility functions to show colored diffs
showEdits :: forall (f :: * -> * -> *) txs tys.
EditScriptL f txs tys -> IO ()
showEdits = display . pprEdits

showCompressed :: Family f => EditScriptL f txs tys -> IO ()
showCompressed = display . pprEdits . compress

pprEdits :: EditScriptL f txs tys -> TermDoc
pprEdits x = case x of
Cpy c d -> (text $ string c) + pprEdits d
CpyTree d -> text " ... " + pprEdits d
Del c d -> (with (Foreground Red) . text $ "- " ++ string c) + pprEdits d
Ins c d -> (with (Foreground Green) . text $ "+ " ++ string c) + pprEdits d
End -> line
Running the main function above would result in the following output

:+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: ... B + 42 - 9 ... ... ... ... ... ... ... ... ... ... ...

Except with pretty colors :).

Binary file removed dist/build/Data/Generic/Diff/TH.p_hi
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH.p_o
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Conversion.p_hi
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Conversion.p_o
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Internal.p_hi
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Internal.p_o
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Specialize.p_hi
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Specialize.p_o
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Types.p_hi
Binary file not shown.
Binary file removed dist/build/Data/Generic/Diff/TH/Types.p_o
Binary file not shown.
6 changes: 3 additions & 3 deletions dist/build/autogen/Paths_gdiff_th.hs
Expand Up @@ -14,12 +14,12 @@ catchIO = Exception.catch


version :: Version
version = Version {versionBranch = [0,0,0,1], versionTags = []}
version = Version {versionBranch = [0,1,0,0], versionTags = []}
bindir, libdir, datadir, libexecdir :: FilePath

bindir = "/Users/jfischoff/Library/Haskell/lib//bin"
libdir = "/Users/jfischoff/Library/Haskell/lib//lib/gdiff-th-0.0.0.1/x86_64/ghc-7.6.1"
datadir = "/Users/jfischoff/Library/Haskell/lib//share/gdiff-th-0.0.0.1"
libdir = "/Users/jfischoff/Library/Haskell/lib//lib/gdiff-th-0.1.0.0/x86_64/ghc-7.6.1"
datadir = "/Users/jfischoff/Library/Haskell/lib//share/gdiff-th-0.1.0.0"
libexecdir = "/Users/jfischoff/Library/Haskell/lib//libexec"

getBinDir, getLibDir, getDataDir, getLibexecDir :: IO FilePath
Expand Down
Binary file removed dist/build/libHSgdiff-th-0.0.0.1.a
Binary file not shown.
Binary file removed dist/build/libHSgdiff-th-0.0.0.1_p.a
Binary file not shown.
3 changes: 1 addition & 2 deletions dist/package.conf.inplace
@@ -1,2 +1 @@
[InstalledPackageInfo {installedPackageId = InstalledPackageId "gdiff-th-0.0.0.1-inplace", sourcePackageId = PackageIdentifier {pkgName = PackageName "gdiff-th", pkgVersion = Version {versionBranch = [0,0,0,1], versionTags = []}}, license = BSD3, copyright = "", maintainer = "jonathangfischoff@gmail.com", author = "", stability = "", homepage = "", pkgUrl = "", synopsis = "Generate gdiff GADTs and Instances.", description = "Generate gdiff GADTs and Instances. Very Alpha. Does not yet support GADTs among other this I'm sure.", category = "Generics", exposed = True, exposedModules = ["Data.Generic.Diff.TH"], hiddenModules = ["Data.Generic.Diff.TH.Conversion","Data.Generic.Diff.TH.Internal","Data.Generic.Diff.TH.Types","Data.Generic.Diff.TH.Specialize"], trusted = False, importDirs = ["/Users/jfischoff/tools/gdiff-th/dist/build"], libraryDirs = ["/Users/jfischoff/tools/gdiff-th/dist/build"], hsLibraries = ["HSgdiff-th-0.0.0.1"], extraLibraries = [], extraGHCiLibraries = [], includeDirs = [], includes = [], depends = [InstalledPackageId "base-4.6.0.0-6898b0af758ec9881050c7cd4b3d7df3",InstalledPackageId "containers-0.5.0.0-e49be7a240765a4edc5c09f677ec6a81",InstalledPackageId "gdiff-1.0-97a814a189b2afdf44517ae95ca5607f",InstalledPackageId "lens-3.0.2-965fb54790e6f0a032eff3b538a5bcd4",InstalledPackageId "mtl-2.1.2-25dca0019d088a7438da0485cd8b4e7a",InstalledPackageId "pointless-haskell-0.0.8-e4082776f5b4b881920ce54cdacca648",InstalledPackageId "template-haskell-2.8.0.0-9d6abcdc55a03c75b4d8fd14551e6c73",InstalledPackageId "th-expand-syns-0.3.0.3-70d3561e986a14a4bf268611d7a3142d",InstalledPackageId "uniplate-1.6.7-357efbd3ab6434f54ee5fd93aa3353c8"], hugsOptions = [], ccOptions = [], ldOptions = [], frameworkDirs = [], frameworks = [], haddockInterfaces = ["/Users/jfischoff/tools/gdiff-th/dist/doc/html/gdiff-th/gdiff-th.haddock"], haddockHTMLs = ["/Users/jfischoff/tools/gdiff-th/dist/doc/html/gdiff-th"]}
]
[]
4 changes: 2 additions & 2 deletions dist/setup-config

Large diffs are not rendered by default.

30 changes: 22 additions & 8 deletions gdiff-th.cabal
Expand Up @@ -7,12 +7,24 @@ Name: gdiff-th
-- The package version. See the Haskell package versioning policy
-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for
-- standards guiding when and how versions should be incremented.
Version: 0.0.0.1
Version: 0.1.0.0

-- A short (one-line) description of the package.
Synopsis: Generate gdiff GADTs and Instances.
-- A longer description of the package.
Description: Generate gdiff GADTs and Instances. Very Alpha. Does not yet support GADTs among other this I'm sure.
Description:
Generate gdiff GADTs and Instances. Alpha, but suprisingly functional.
Very useful for unit testing large data structures. I have tested it on a
few very large collections of types and it appears to work. Although,
when I tried to compare two versions of a hackage package with src-exts
the (GDiff) performance is terrible. In my personal experience of using
gdiff in unit testing, the performance has be great. Your mileage may vary.
.
I wouldn't use it for sending patches over the wire or anything like that,
I am not convinced there are no bugs in my code yet. There are examples in the @examples@ directory of the
cabal tarball. Also the main module includes an example in the documentation.
.
* New in this version: It's functional.

-- The license under which the package is released.
License: BSD3
Expand All @@ -21,22 +33,27 @@ License: BSD3
License-file: LICENSE

-- The package author(s).
-- Author:
-- Author: Jonathan Fischoff

-- An email address to which users can send suggestions, bug reports,
-- and patches.
Maintainer: jonathangfischoff@gmail.com

-- A copyright notice.
-- Copyright:
-- Copyright: Copyright 2012 Jonathan Fischoff

Category: Generics

Build-type: Simple

-- Extra files to be distributed with the package, such as examples or
-- a README.
-- Extra-source-files:
Extra-source-files: examples/Expr.hs
examples/New.hs
examples/Old.hs
examples/CompareVersions.hs
examples/Parser.hs
examples/Utils.hs

-- Constraint on the version of Cabal needed to build this package.
Cabal-version: >=1.8
Expand Down Expand Up @@ -82,9 +99,6 @@ Test-Suite tests
checkers >= 0.2.8,
mtl >= 2.0.1.0,
th-instances >= 0.1.0.14,
specialize-th >= 0.0.0.8,
universe-th >= 0.0.0.6,
type-sub-th >= 0.1.0.5,
gdiff >= 1.0,
tuple >= 0.2.0.1,
pointless-haskell == 0.0.*,
Expand Down
67 changes: 65 additions & 2 deletions src/Data/Generic/Diff/TH.hs
@@ -1,3 +1,66 @@
module Data.Generic.Diff.TH (makeGDiff, makeGDiffWith, defaultFamSuffix,
defaultConstructorRenamer, defaultPrimitives) where
-- | This module exports the Template Haskell functions necessary
-- deriving gdiff GADTs and associated instances. Usage is pretty
-- straightforward.
--
-- @
--module Example where
--import "Data.Generic.Diff"
--import "Data.Generic.Diff.TH"
--import "System.Console.Terminfo.Color"
--import "Text.PrettyPrint.Free hiding (parens)"
--import "System.Console.Terminfo.PrettyPrint"
--
--data Exp = Exp :+: Exp
-- | Exp :*: Exp
-- | B Integer
-- deriving(Show, Eq, Typeable)
--
--{- Make the GDiff apparatus -}
--makeGDiff ''Exp
--
--testA :: Exp
--testA = foldl1 (:+:) . map B $ [0..20]
--
--testB :: Exp
--testB = foldl1 (:+:) . map B $ [0..8] ++ [42] ++ [10..20]
--
--{- Make a type signature to help inference -}
--diffExp :: Type ExpFamily Exp => Exp -> Exp -> EditScript ExpFamily Exp Exp
--diffExp = diff
--
--diffAandB = showCompressed $ diffExp testA testB
--
--main = diffAandB
--
--{- Utility functions to show colored diffs -}
--showEdits :: forall (f :: * -> * -> *) txs tys.
-- EditScriptL f txs tys -> IO ()
--showEdits = display . pprEdits
--
--showCompressed :: Family f => EditScriptL f txs tys -> IO ()
--showCompressed = display . pprEdits . compress
--
--pprEdits :: EditScriptL f txs tys -> TermDoc
--pprEdits x = case x of
-- Cpy c d -> (text $ string c) <+> pprEdits d
-- CpyTree d -> text \" ... \" <+> pprEdits d
-- Del c d -> (with (Foreground Red) . text $ \"- \" ++ string c) <+> pprEdits d
-- Ins c d -> (with (Foreground Green) . text $ \"+ \" ++ string c) <+> pprEdits d
-- End -> line
-- @
--
-- Running the main function above would result in the following output
-- @
--:+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: ... B + 42 - 9 ... ... ... ... ... ... ... ... ... ... ...
-- @
-- Except with pretty colors
module Data.Generic.Diff.TH (
-- * Main Creation Function
makeGDiff,
-- * Customizable Creation
makeGDiffWith,
defaultFamSuffix,
defaultConstructorRenamer,
defaultPrimitives,
ConstructorRenamer) where
import Data.Generic.Diff.TH.Internal
Binary file removed src/Data/Generic/Diff/TH.p_o
Binary file not shown.
31 changes: 21 additions & 10 deletions src/Data/Generic/Diff/TH/Internal.hs
Expand Up @@ -12,15 +12,8 @@ import Data.Generic.Diff.TH.Conversion
import Data.Maybe(fromMaybe)
import Data.Word
import Data.Int
--TODO
--I should check that the root type is monomorphic

--Questions
--performance
--Can I use this to perform a patch that
--Version control stuff?
--Assuming the formatting is fine

-- | Default primitives and expressions for showing them
defaultPrimitives :: [(Name, TH.Exp)]
defaultPrimitives = map (, VarE 'show) defaultNames

Expand Down Expand Up @@ -160,9 +153,24 @@ mkGADT (Fam {..}) = do

dataD (return []) _famName [PlainTV a, PlainTV b] constrs []

-- | The type of function used for naming the GADTs constructors
--
-- Arg0 : The family suffix
--
-- Arg1 : The name of the constructor
--
-- Arg2 : The specialized type the constructor is from
type ConstructorRenamer = (String -> Name -> TH.Type -> Q Name)

--TODO make this take a renameFunctions: for the family, for the constructors
-- | Customizable creation.
--
-- Arg0 : The suffix added to the Family
--
-- Arg1 : Function used for naming constructors of the GADT after specialization
--
-- Arg2 : A list of primitives and an expression for showing them
--
-- Arg3 : The root type
makeGDiffWith :: String -> ConstructorRenamer -> [(Name, TH.Exp)] -> Name -> Q [Dec]
makeGDiffWith familyPrefix constructorRenamer primitives name = do
let familyName = mkName $ nameBase name ++ familyPrefix
Expand All @@ -182,15 +190,18 @@ makeGDiffWith familyPrefix constructorRenamer primitives name = do

return $ gadt : instances

--TODO make something better as a default
-- | Default constructor renamer. Using the family suffix, the
-- name of the constructor and the specialized type of constructor
defaultConstructorRenamer :: String -> Name -> TH.Type -> Q Name
defaultConstructorRenamer prefix n typ = return . mkName $
filter (\x -> x /= '[' && x /= ']') $ prefix ++
typToString typ ++ prettifyName n ++ "C"

-- | Default suffix for the family "Family"
defaultFamSuffix :: String
defaultFamSuffix = "Family"

-- | Create the GADT and instances for GDiff with the defaults
makeGDiff :: Name -> Q [Dec]
makeGDiff = makeGDiffWith defaultFamSuffix defaultConstructorRenamer defaultPrimitives

Expand Down

0 comments on commit bb444b3

Please sign in to comment.