Skip to content

Commit

Permalink
Merge pull request #20 from obsidiansystems/cg-th-abstraction
Browse files Browse the repository at this point in the history
 Improve/rewrite instance generation using th-abstraction and the reifyInstancesWithRigids/skolemize trick
  • Loading branch information
ali-abrar authored Dec 16, 2019
2 parents c266f14 + 306db8a commit b8bebee
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 160 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision history for aeson-gadt-th

## 0.2.2

* Do a better job determining which variables are rigid when looking for instances
* Unify discovered instance head with argument type and make the same substitution in the context that constrains the instance we're writing

## 0.2.1.2

* Add version bounds to cabal file
Expand Down
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ Example Usage:
> {-# LANGUAGE FlexibleInstances #-}
> {-# LANGUAGE UndecidableInstances #-}
> {-# LANGUAGE MultiParamTypeClasses #-}
>
> {-# OPTIONS_GHC -ddump-splices #-}
>
> import Data.Aeson
> import Data.Aeson.GADT.TH
>
>
> import Data.Dependent.Map (DMap, Some(..))
> import Data.Dependent.Sum (DSum)
> import Data.Functor.Identity
> import Data.GADT.Compare
> import Data.GADT.Show.TH
>
>
> data A :: * -> * where
> A_a :: A a
> A_b :: Int -> A ()
>
>
> deriveJSONGADT ''A
> deriveGShow ''A
>
Expand All @@ -36,40 +37,52 @@ Example Usage:
> B_x :: B c a
>
> deriveJSONGADT ''B
>
>
> data C t :: * -> * where
> C_t :: t -> C t t
>
>
> deriveJSONGADT ''C
>
>
> data D t x :: * -> * where
> D_t :: t -> D t x t
> D_x :: x -> D t x x
> D_i :: Int -> D t x Int
>
> deriveJSONGADT ''D
>
> data Auth token a where
> Auth_Login :: String -> String -> Auth token (Either String token)
>
> deriveJSONGADT ''Auth
>
> -- Some real-world-ish examples.
>
>
> -- | Edit operations for `LabelledGraph`
> data LabelledGraphEdit v vm em :: * -> * where
> LabelledGraphEdit_ClearAll :: LabelledGraphEdit v vm em ()
> LabelledGraphEdit_AddVertex :: vm -> LabelledGraphEdit v vm em v
> LabelledGraphEdit_AddEdge :: v -> v -> em -> LabelledGraphEdit v vm em ()
> LabelledGraphEdit_SetVertexProperties :: v -> vm -> LabelledGraphEdit v vm em ()
> LabelledGraphEdit_SetEdgeProperties :: v -> v -> em -> LabelledGraphEdit v vm em ()
>
>
> -- | PropertyGraphEdit operatios for `PropertyGraph`
> data PropertyGraphEdit v vp ep r where
> PropertyGraphEdit_ClearAll :: PropertyGraphEdit v vp ep ()
> PropertyGraphEdit_AddVertex :: (DMap vp Identity) -> PropertyGraphEdit v vp ep v
> PropertyGraphEdit_AddEdge :: v -> v -> (DMap ep Identity) -> PropertyGraphEdit v vp ep ()
> PropertyGraphEdit_SetVertexProperty :: GCompare vp => v -> DSum vp Identity -> PropertyGraphEdit v vp ep ()
> PropertyGraphEdit_SetEdgeProperty :: GCompare ep => v -> v -> DSum ep Identity -> PropertyGraphEdit v vp ep ()
>
>
> -- | View operations for `LabelledGraph`
> data LabelledGraphView v vm em :: * -> * where
> LabelledGraphView_All :: LabelledGraphView v vm em ()
> LabelledGraphView_GetVertexProperties :: v -> LabelledGraphView v vm em vm
> LabelledGraphView_GetEdgeProperties :: v -> v -> LabelledGraphView v vm em em
>
>
> deriveJSONGADT ''LabelledGraphEdit
> deriveJSONGADT ''PropertyGraphEdit
> deriveJSONGADT ''LabelledGraphView
>
>
> main :: IO ()
> main = do
> putStrLn $ unlines
Expand All @@ -91,7 +104,7 @@ Example Usage:
> , show $ encode (B_a 'a' (A_b 1))
> , "Decoding of encoded (B_a 'a' (A_b 1)):"
> , case (decode $ encode (B_a 'a' (A_b 1)) :: Maybe (Some (B Char))) of
> Just (This (B_a 'a' (A_b 1))) -> "Succeeded"
> Just (Some (B_a 'a' (A_b 1))) -> "Succeeded"
> _-> "Failed"
> ]
```
6 changes: 4 additions & 2 deletions aeson-gadt-th.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.0
name: aeson-gadt-th
version: 0.2.1.2
version: 0.2.2
synopsis: Derivation of Aeson instances for GADTs
category: JSON
description: Template Haskell for generating ToJSON and FromJSON instances for GADTs. See <https://github.com/obsidiansystems/aeson-gadt-th/blob/master/README.md README.md> for examples.
Expand All @@ -23,9 +23,11 @@ library
build-depends: base >= 4.8 && < 4.13
, aeson >= 1.4 && < 1.5
, containers >= 0.5 && < 0.7
, dependent-sum >= 0.6.2 && < 0.7
, dependent-sum >= 0.6.1 && < 0.7
, transformers >= 0.5 && < 0.6
, template-haskell >= 2.11.0 && < 2.15
, th-abstraction >= 0.3.1.0 && < 0.4
, th-extras >= 0.0.0.4 && < 0.1
hs-source-dirs: src
default-language: Haskell2010

Expand Down
Loading

0 comments on commit b8bebee

Please sign in to comment.