summaryrefslogtreecommitdiff
path: root/testsuite/tests/generics/Uniplate
diff options
context:
space:
mode:
authorDavid Terei <davidterei@gmail.com>2011-07-20 11:09:03 -0700
committerDavid Terei <davidterei@gmail.com>2011-07-20 11:26:35 -0700
commit16514f272fb42af6e9c7674a9bd6c9dce369231f (patch)
treee4f332b45fe65e2a7a2451be5674f887b42bf199 /testsuite/tests/generics/Uniplate
parentebd422aed41048476aa61dd4c520d43becd78682 (diff)
downloadhaskell-16514f272fb42af6e9c7674a9bd6c9dce369231f.tar.gz
Move tests from tests/ghc-regress/* to just tests/*
Diffstat (limited to 'testsuite/tests/generics/Uniplate')
-rw-r--r--testsuite/tests/generics/Uniplate/GUniplate.hs53
-rw-r--r--testsuite/tests/generics/Uniplate/GUniplate1.stdout1
-rw-r--r--testsuite/tests/generics/Uniplate/Main.hs20
-rw-r--r--testsuite/tests/generics/Uniplate/Makefile3
-rw-r--r--testsuite/tests/generics/Uniplate/test.T3
5 files changed, 80 insertions, 0 deletions
diff --git a/testsuite/tests/generics/Uniplate/GUniplate.hs b/testsuite/tests/generics/Uniplate/GUniplate.hs
new file mode 100644
index 0000000000..76f387d636
--- /dev/null
+++ b/testsuite/tests/generics/Uniplate/GUniplate.hs
@@ -0,0 +1,53 @@
+{-# LANGUAGE MultiParamTypeClasses #-}
+{-# LANGUAGE TypeSynonymInstances #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE DefaultSignatures #-}
+{-# LANGUAGE IncoherentInstances #-} -- necessary, unfortunately
+
+module GUniplate where
+
+import GHC.Generics
+
+--------------------------------------------------------------------------------
+-- Generic Uniplate
+--------------------------------------------------------------------------------
+
+class Uniplate' f b where
+ children' :: f a -> [b]
+
+instance Uniplate' U1 a where
+ children' U1 = []
+
+instance Uniplate' (K1 i a) a where
+ children' (K1 a) = [a]
+
+instance Uniplate' (K1 i a) b where
+ children' (K1 _) = []
+
+instance (Uniplate' f b) => Uniplate' (M1 i c f) b where
+ children' (M1 a) = children' a
+
+instance (Uniplate' f b, Uniplate' g b) => Uniplate' (f :+: g) b where
+ children' (L1 a) = children' a
+ children' (R1 a) = children' a
+
+instance (Uniplate' f b, Uniplate' g b) => Uniplate' (f :*: g) b where
+ children' (a :*: b) = children' a ++ children' b
+
+
+class Uniplate a where
+ children :: a -> [a]
+ default children :: (Generic a, Uniplate' (Rep a) a) => a -> [a]
+ children = children' . from
+
+
+-- Base types instances
+instance Uniplate Char where children _ = []
+instance Uniplate Int where children _ = []
+instance Uniplate Float where children _ = []
+
+instance Uniplate [a] where
+ children [] = []
+ children (_:t) = [t]
diff --git a/testsuite/tests/generics/Uniplate/GUniplate1.stdout b/testsuite/tests/generics/Uniplate/GUniplate1.stdout
new file mode 100644
index 0000000000..f560e40162
--- /dev/null
+++ b/testsuite/tests/generics/Uniplate/GUniplate1.stdout
@@ -0,0 +1 @@
+("",[],[Leaf,Leaf])
diff --git a/testsuite/tests/generics/Uniplate/Main.hs b/testsuite/tests/generics/Uniplate/Main.hs
new file mode 100644
index 0000000000..95d84244fa
--- /dev/null
+++ b/testsuite/tests/generics/Uniplate/Main.hs
@@ -0,0 +1,20 @@
+{-# LANGUAGE DeriveGeneric #-}
+
+module Main where
+
+import GHC.Generics
+import GUniplate
+
+
+data Tree = Leaf | Node Int Tree Tree deriving (Show, Generic)
+data Pair a b = Pair a b deriving (Show, Generic)
+
+instance Uniplate Tree
+instance Uniplate (Pair a b)
+
+-- Tests
+t1 = children ('p')
+t2 = children (Pair "abc" (Pair "abc" 2))
+t3 = children (Node 2 Leaf Leaf)
+
+main = print (t1, t2, t3)
diff --git a/testsuite/tests/generics/Uniplate/Makefile b/testsuite/tests/generics/Uniplate/Makefile
new file mode 100644
index 0000000000..9101fbd40a
--- /dev/null
+++ b/testsuite/tests/generics/Uniplate/Makefile
@@ -0,0 +1,3 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
diff --git a/testsuite/tests/generics/Uniplate/test.T b/testsuite/tests/generics/Uniplate/test.T
new file mode 100644
index 0000000000..a1e610726e
--- /dev/null
+++ b/testsuite/tests/generics/Uniplate/test.T
@@ -0,0 +1,3 @@
+setTestOpts(only_compiler_types(['ghc']))
+
+test('GUniplate1', normal, multimod_compile_and_run, ['Main', '']) \ No newline at end of file