add ArrowChoice=>GArrowSum instance
authorAdam Megacz <megacz@cs.berkeley.edu>
Fri, 8 Apr 2011 04:05:35 +0000 (04:05 +0000)
committerAdam Megacz <megacz@cs.berkeley.edu>
Tue, 31 May 2011 21:59:08 +0000 (14:59 -0700)
GHC/HetMet/Arrow.hs
GHC/HetMet/GArrow.hs

index 5bead2c..08b75b3 100644 (file)
@@ -1,4 +1,4 @@
-{-# OPTIONS -XRankNTypes -XMultiParamTypeClasses -XNoMonomorphismRestriction -XTypeOperators -XFlexibleInstances -XFunctionalDependencies #-}
+{-# OPTIONS -XRankNTypes -XMultiParamTypeClasses -XNoMonomorphismRestriction -XTypeOperators -XFlexibleInstances -XFunctionalDependencies -XEmptyDataDecls #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  GHC.HetMet.Arrow
@@ -55,8 +55,43 @@ instance ArrowApply a => GArrowApply a (,) () a where
   ga_applyl    = ga_swap >>> app
   ga_applyr    = app
 
-
-
+-- The uninhabited type
+data Void
+
+-- In Coq we could simply prove that these cases are impossible; in Haskell we need to have some faith.
+voidImpossible :: Void -> a
+voidImpossible = error "this is impossible; you have a bug in your compiler"
+
+instance ArrowChoice a => GArrow a Either Void where
+  ga_first     =  left
+  ga_second    =  right
+  ga_uncancell =  arr Right
+  ga_uncancelr =  arr Left
+  ga_cancell   =  arr unVoidLeft
+                   where
+                    unVoidLeft  (Left  v) = voidImpossible v
+                    unVoidRight (Right x) = x
+  ga_cancelr   =  arr unVoidRight
+                   where
+                    unVoidRight (Left  x) = x
+                    unVoidRight (Right v) = voidImpossible v
+  ga_assoc     =  arr eitherAssoc
+                   where
+                    eitherAssoc (Left (Left  x)) = Left         x
+                    eitherAssoc (Left (Right y)) = Right (Left  y)
+                    eitherAssoc (Right       z ) = Right (Right z)
+  ga_unassoc   =  arr eitherUnAssoc
+                   where
+                    eitherUnAssoc (Left         x ) = Left  (Left  x)
+                    eitherUnAssoc (Right (Left  y)) = Left  (Right y)
+                    eitherUnAssoc (Right (Right z)) = Right        z
+
+instance ArrowChoice a => GArrowSum a (,) () Void Either where
+  ga_never = arr voidImpossible
+  ga_merge = arr merge
+              where
+               merge (Left  x) = x
+               merge (Right x) = x
 
 
 
index 88f84fb..9f8aab1 100644 (file)
@@ -83,8 +83,8 @@ class (GArrow     g (**)  u,
 class (GArrow     g (**)  u,
        GArrow     g (<+>) v) => 
        GArrowSum  g (**)  u v (<+>) where
-  ga_merge :: g (x**x) x
-  ga_never :: g v      x
+  ga_merge :: g (x<+>x) x
+  ga_never :: g v       x