Skip to content

Commit

Permalink
mv TTree to A, update related test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
spl committed May 1, 2010
1 parent 8c517d8 commit 1303ada
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 192 deletions.
164 changes: 164 additions & 0 deletions tests/A.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
-----------------------------------------------------------------------------
-- |
-- Module : A
-- Copyright : (c) 2008 - 2010 Universiteit Utrecht
-- License : BSD3
--
-- Maintainer : generics@haskell.org
--
-- An example type representation.
-----------------------------------------------------------------------------

-- {-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE UndecidableInstances #-}

module A where

import Prelude hiding (Read, Show)
import qualified Prelude as P (Read, Show)
import Data.Generics (Data, Typeable)

import Generics.EMGM.Base
import Generics.EMGM.Functions.Collect
import Generics.EMGM.Functions.Everywhere

data A a
= A1 a
| A2 Integer (A a)
| A3 { unA3 :: Double }
| A4 { unA4a :: A a, unA4b :: Int }
| A5 { unA5a :: Char, unA5b :: A a, unA5c :: a }
| A a :^: Float
| (:<>:) { unA7a :: A a, unA7b :: A a }
deriving (P.Show, P.Read, Eq, Ord, Data, Typeable)

infixr 6 :^:
infixl 5 :<>:

type A' a
{- A1 -} = a
{- A2 -} :+: Integer :*: A a
{- A3 -} :+: Double
{- A4 -} :+: A a :*: Int
{- A5 -} :+: Char :*: A a :*: a
{- :^: -} :+: A a :*: Float
{- :<>: -} :+: A a :*: A a

fromA :: A a -> A' a
fromA t = case t of
A1 x1 -> L x1
A2 x1 x2 -> R (L (x1 :*: x2))
A3 x1 -> R (R (L x1))
A4 x1 x2 -> R (R (R (L (x1 :*: x2))))
A5 x1 x2 x3 -> R (R (R (R (L (x1 :*: x2 :*: x3)))))
x1 :^: x2 -> R (R (R (R (R (L (x1 :*: x2))))))
x1 :<>: x2 -> R (R (R (R (R (R (x1 :*: x2))))))

toA :: A' a -> A a
toA s = case s of
L x1 -> A1 x1
R (L (x1 :*: x2)) -> A2 x1 x2
R (R (L x1)) -> A3 x1
R (R (R (L (x1 :*: x2)))) -> A4 x1 x2
R (R (R (R (L (x1 :*: x2 :*: x3))))) -> A5 x1 x2 x3
R (R (R (R (R (L (x1 :*: x2)))))) -> x1 :^: x2
R (R (R (R (R (R (x1 :*: x2)))))) -> x1 :<>: x2

epA :: EP (A a) (A' a)
epA = EP fromA toA

instance Representable (A a) (A' a) where
epOf _ = epA

conA1 = ConDescr "A1" 1 False Prefix
conA2 = ConDescr "A2" 2 False Prefix
conA3 = ConDescr "A3" 1 True Prefix
conA4 = ConDescr "A4" 2 True Prefix
conA5 = ConDescr "A5" 3 True Prefix
conA6 = ConDescr ":^:" 2 False (Infix RightAssoc 6)
conA7 = ConDescr ":<>:" 2 True (Infix LeftAssoc 5)

lblUnA3 = LblDescr "unA3"
lblUnA4a = LblDescr "unA4a"
lblUnA4b = LblDescr "unA4b"
lblUnA5a = LblDescr "unA5a"
lblUnA5b = LblDescr "unA5b"
lblUnA5c = LblDescr "unA5c"
lblUnA7a = LblDescr "unA7a"
lblUnA7b = LblDescr "unA7b"

instance (Generic g, Rep g a, Rep g Char, Rep g Double, Rep g Float, Rep g Integer, Rep g Int) => Rep g (A a) where
rep = rtype epA
$ rcon conA1 rep
`rsum` rcon conA2 (rep `rprod` rep)
`rsum` rcon conA3 (rlabel lblUnA3 rep)
`rsum` rcon conA4 (rlabel lblUnA4a rep `rprod` rlabel lblUnA4b rep)
`rsum` rcon conA5 (rlabel lblUnA5a rep `rprod` rlabel lblUnA5b rep `rprod` rlabel lblUnA5c rep)
`rsum` rcon conA6 (rep `rprod` rep)
`rsum` rcon conA7 (rlabel lblUnA7a rep `rprod` rlabel lblUnA7b rep)

instance (Generic g) => FRep g A where
frep ra = rtype epA
$ rcon conA1 ra
`rsum` rcon conA2 (rinteger `rprod` frep ra)
`rsum` rcon conA3 (rlabel lblUnA3 rdouble)
`rsum` rcon conA4 (rlabel lblUnA4a (frep ra) `rprod` rlabel lblUnA4b rint)
`rsum` rcon conA5 (rlabel lblUnA5a rchar `rprod` rlabel lblUnA5b (frep ra) `rprod` rlabel lblUnA5c ra)
`rsum` rcon conA6 (frep ra `rprod` rfloat)
`rsum` rcon conA7 (rlabel lblUnA7a (frep ra) `rprod` rlabel lblUnA7b (frep ra))

instance (Generic2 g) => FRep2 g A where
frep2 ra = rtype2 epA epA
$ rcon2 conA1 ra
`rsum2` rcon2 conA2 (rinteger2 `rprod2` frep2 ra)
`rsum2` rcon2 conA3 (rlabel2 lblUnA3 rdouble2)
`rsum2` rcon2 conA4 (rlabel2 lblUnA4a (frep2 ra) `rprod2` rlabel2 lblUnA4b rint2)
`rsum2` rcon2 conA5 (rlabel2 lblUnA5a rchar2 `rprod2` rlabel2 lblUnA5b (frep2 ra) `rprod2` rlabel2 lblUnA5c ra)
`rsum2` rcon2 conA6 (frep2 ra `rprod2` rfloat2)
`rsum2` rcon2 conA7 (rlabel2 lblUnA7a (frep2 ra) `rprod2` rlabel2 lblUnA7b (frep2 ra))

instance (Generic3 g) => FRep3 g A where
frep3 ra = rtype3 epA epA epA
$ rcon3 conA1 ra
`rsum3` rcon3 conA2 (rinteger3 `rprod3` frep3 ra)
`rsum3` rcon3 conA3 (rlabel3 lblUnA3 rdouble3)
`rsum3` rcon3 conA4 (rlabel3 lblUnA4a (frep3 ra) `rprod3` rlabel3 lblUnA4b rint3)
`rsum3` rcon3 conA5 (rlabel3 lblUnA5a rchar3 `rprod3` rlabel3 lblUnA5b (frep3 ra) `rprod3` rlabel3 lblUnA5c ra)
`rsum3` rcon3 conA6 (frep3 ra `rprod3` rfloat3)
`rsum3` rcon3 conA7 (rlabel3 lblUnA7a (frep3 ra) `rprod3` rlabel3 lblUnA7b (frep3 ra))

instance Rep (Collect (A a)) (A a) where
rep = Collect (:[])

instance (Rep (Everywhere (A a)) a) => Rep (Everywhere (A a)) (A a) where
rep = Everywhere app
where
app f x =
case x of
A1 x1 -> f (A1 (selEverywhere rep f x1))
A2 x1 x2 -> f (A2 (selEverywhere rep f x1) (selEverywhere rep f x2))
A3 x1 -> f (A3 (selEverywhere rep f x1))
A4 x1 x2 -> f (A4 (selEverywhere rep f x1) (selEverywhere rep f x2))
A5 x1 x2 x3 -> f (A5 (selEverywhere rep f x1) (selEverywhere rep f x2) (selEverywhere rep f x3))
x1 :^: x2 -> f (selEverywhere rep f x1 :^: selEverywhere rep f x2)
x1 :<>: x2 -> f (selEverywhere rep f x1 :<>: selEverywhere rep f x2)

instance Rep (Everywhere' (A a)) (A a) where
rep = Everywhere' ($)

v1 = A1 (5 :: Int)
v2 = A2 37 v1
v3 = A3 9999.9999 :: A Float
v4 = A4 v3 79
v5 = A5 'a' v4 5.0
v6 = v5 :^: 0.12345
v7 = v6 :<>: v6

15 changes: 8 additions & 7 deletions tests/Compare.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{-# LANGUAGE FlexibleContexts #-}

-----------------------------------------------------------------------------
-- |
-- Module : Compare
-- Copyright : (c) 2008, 2009 Universiteit Utrecht
-- Copyright : (c) 2008 - 2010 Universiteit Utrecht
-- License : BSD3
--
-- Maintainer : generics@haskell.org
-----------------------------------------------------------------------------

{-# LANGUAGE FlexibleContexts #-}

module Compare where

import Prelude hiding (Show, show, compare, min, max)
Expand All @@ -17,9 +17,10 @@ import Test.HUnit
import Data.Generics (Data)

import Generics.EMGM
import Generics.EMGM.Functions.Compare

import Base
import TTree
import A

-----------------------------------------------------------------------------
-- Utility functions
Expand Down Expand Up @@ -56,9 +57,9 @@ test_min = test_f min P.min
test_max :: (P.Show a, Data a, Ord a, Rep Compare a) => a -> a -> Test
test_max = test_f max P.max

t1, t2 :: T (T Float)
t1 = L1 (L3 8.8 :^: 9.9) :<>: L4 (L4 (L2 (L3 11.11) (L1 (L1 22.22))) (L3 33.33)) (L5 0.44 (L3 55.55) 0.66)
t2 = L1 (L3 8.8 :^: 9.9) :<>: L4 (L4 (L2 (L3 11.11) (L1 (L3 22.22))) (L3 33.33)) (L5 0.44 (L3 55.55) 0.66)
t1, t2 :: A (A Float)
t1 = A1 (A3 8.8 :^: 9.9) :<>: A4 (A4 (A2 11 (A1 (A1 22.22))) 33) 44
t2 = A1 (A3 8.8 :^: 9.9) :<>: A4 (A4 (A2 11 (A1 (A3 22.22))) 33) 44

-----------------------------------------------------------------------------
-- Test collections
Expand Down
19 changes: 9 additions & 10 deletions tests/Everywhere.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

-----------------------------------------------------------------------------
-- |
-- Module : Everywhere
-- Copyright : (c) 2008, 2009 Universiteit Utrecht
-- Copyright : (c) 2008 - 2010 Universiteit Utrecht
-- License : BSD3
--
-- Maintainer : generics@haskell.org
-----------------------------------------------------------------------------

module Everywhere (tests) where

import TTree
import A
import Generics.EMGM as G

import Test.HUnit
Expand Down Expand Up @@ -60,9 +59,9 @@ f_list_char2 [] = []
f_unit :: () -> ()
f_unit = id

f_ttree1 :: T Int -> T Int
f_ttree1 (L1 4) = L1 7
f_ttree1 (L2 5 (L1 4)) = L1 9
f_ttree1 :: A Int -> A Int
f_ttree1 (A1 4) = A1 7
f_ttree1 (A2 5 (A1 4)) = A1 9
f_ttree1 x = x

-----------------------------------------------------------------------------
Expand Down Expand Up @@ -94,8 +93,8 @@ tests =
, test_e "(,,,,)" f_unit ((),(),(),(),()) id
, test_e "(,,,,,)" f_unit ((),(),(),(),(),()) id
, test_e "(,,,,,,)" f_unit ((),(),(),(),(),(),()) id
, test_e "T1" f_ttree1 (L1 4) f_ttree1
, test_e "T2" f_ttree1 (L2 (5::Int) (L1 4)) (const (L2 5 (L1 7)))
, test_e "T1" f_ttree1 (A1 4) f_ttree1
, test_e "T2" f_ttree1 (A2 5 (A1 4) :: A Int) (const (A2 5 (A1 7)))
]

, "Everywhere'" ~:
Expand All @@ -120,8 +119,8 @@ tests =
, test_e' "(,,,,)" f_unit ((),(),(),(),()) id
, test_e' "(,,,,,)" f_unit ((),(),(),(),(),()) id
, test_e' "(,,,,,,)" f_unit ((),(),(),(),(),(),()) id
, test_e' "T1" f_ttree1 (L1 4) f_ttree1
, test_e' "T2" f_ttree1 (L2 (5::Int) (L1 4)) (const (L1 9))
, test_e' "T1" f_ttree1 (A1 4) f_ttree1
, test_e' "T2" f_ttree1 (A2 5 (A1 4) :: A Int) (const (A1 9))
]

]
Expand Down
36 changes: 19 additions & 17 deletions tests/ReadShow.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module : ReadShow
-- Copyright : (c) 2008, 2009 Universiteit Utrecht
-- Copyright : (c) 2008 - 2010 Universiteit Utrecht
-- License : BSD3
--
-- Maintainer : generics@haskell.org
-----------------------------------------------------------------------------

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}

module ReadShow (tests) where

import Prelude hiding (Read, Show, readsPrec, reads, read, show)
Expand All @@ -18,9 +18,11 @@ import Data.Generics (Data)
import Test.HUnit

import Generics.EMGM
import Generics.EMGM.Functions.Read
import Generics.EMGM.Functions.Show

import Base
import TTree
import A

-----------------------------------------------------------------------------
-- Utility functions
Expand Down Expand Up @@ -59,17 +61,17 @@ tests =
, test_all True (Right '2' :: Either Float Char)
, test_all True (Nothing :: Maybe Double)
, test_all True (Just 256 :: Maybe Int)
, test_all True (L1 5 :: T Int)
, test_all True (L1 (Just 5) :: T (Maybe Int))
, test_all True (L2 88 (L1 99) :: T Int)
, test_all True (L3 654 :: T Int)
, test_all True (Just (L3 654) :: Maybe (T Int))
, test_all True (L4 (L2 1 (L3 2)) 3 :: T Int)
, test_all True (L5 'a' (L4 (L3 102) 103) 104 :: T Int)
, test_all True (L3 8.0 :^: 8.0 :: T Char)
, test_all True ((L3 (-0.2) :^: 0.2) :^: 2.0 :: T Char)
, test_all False (L1 1.1 :<>: L1 1.2 :^: 1.3 :: T Float)
, test_all False (L1 (L3 8.8 :^: 9.9) :<>: L4 (L4 (L2 101 (L1 (L1 22.22))) (-1)) 55 :: T (T Float))
, test_all True (A1 5 :: A Int)
, test_all True (A1 (Just 5) :: A (Maybe Int))
, test_all True (A2 88 (A1 99) :: A Int)
, test_all True (A3 654 :: A Int)
, test_all True (Just (A3 654) :: Maybe (A Int))
, test_all True (A4 (A2 1 (A3 2)) 3 :: A Int)
, test_all True (A5 'a' (A4 (A3 102) 103) 104 :: A Int)
, test_all True (A3 8.0 :^: 8.0 :: A Char)
, test_all True ((A3 (-0.2) :^: 0.2) :^: 2.0 :: A Char)
, test_all False (A1 1.1 :<>: A1 1.2 :^: 1.3 :: A Float)
, test_all False (A1 (A3 8.8 :^: 9.9) :<>: A4 (A4 (A2 101 (A1 (A1 22.22))) (-1)) 55 :: A (A Float))
, test_all True [1,2,3,4,5 :: Int]
, test_all True [[5.3,3.5],[35.0],[0.53 :: Float]]
, test_all True "abcdefgh"
Expand All @@ -80,6 +82,6 @@ tests =
, test_all True (1::Int,2::Float,3::Double,'4')
, test_all True (1::Int,2::Float,3::Double,'4',False)
, test_all True (1::Int,2::Float,3::Double,'4',False,Just (6::Int))
, test_all True (1::Int,2::Float,3::Double,'4',False,Just (6::Int),L1 (7::Float))
, test_all True (1::Int,2::Float,3::Double,'4',False,Just (6::Int),A1 (7::Float))
]

Loading

0 comments on commit 1303ada

Please sign in to comment.