git.iko.soy/typeable/postgresql-simple
on master help sign in
Add a preliminary draft of the Ok type
commit82b3b18373c9c34d8a501ec4121c7f4b65af447d authorLeon P Smith <leon@melding-monads.com> dateThu 23 Feb 2012, 17:45 (15 years ago) parentc576a8c
2 files changed+46−1
postgresql-simple.cabal+2−1 src/Database/PostgreSQL/Simple/Ok.hs+44
postgresql-simple.cabal+2−1
@@ -1,5 +1,5 @@
11Name: postgresql-simple
2−Version: 0.0.5
2+Version: 0.0.99
33Synopsis: Mid-Level PostgreSQL client library
44Description:
55 Mid-Level PostgreSQL client library, forked from mysql-simple.
@@ -22,6 +22,7 @@ Library
2222 Database.PostgreSQL.Simple.Field
2323 Database.PostgreSQL.Simple.LargeObjects
2424 Database.PostgreSQL.Simple.Notification
25+ Database.PostgreSQL.Simple.Ok
2526 Database.PostgreSQL.Simple.Param
2627 Database.PostgreSQL.Simple.QueryParams
2728 Database.PostgreSQL.Simple.QueryResults
src/Database/PostgreSQL/Simple/Ok.hsnew file+44
@@ -0,0 +1,44 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveFunctor #-}
3+
4+module Database.PostgreSQL.Simple.Ok where
5+
6+import Control.Applicative
7+import Control.Exception
8+import Data.Typeable
9+
10+-- FIXME: [SomeException] should probably be a difference list
11+
12+data Ok a = Errors [SomeException] | Ok !a
13+ deriving(Show, Typeable, Functor)
14+
15+instance Eq a => Eq (Ok a) where
16+ Errors _ == Errors _ = True
17+ Ok a == Ok b = a == b
18+ _ == _ = False
19+
20+instance Applicative Ok where
21+ pure = Ok
22+
23+ Errors es <*> _ = Errors es
24+ _ <*> Errors es = Errors es
25+ Ok f <*> Ok a = Ok (f a)
26+
27+instance Alternative Ok where
28+ empty = Errors []
29+
30+ a@(Ok _) <|> _ = a
31+ Errors _ <|> b@(Ok _) = b
32+ Errors as <|> Errors bs = Errors (as ++ bs)
33+
34+instance Monad Ok where
35+ return = Ok
36+
37+ Errors es >>= _ = Errors es
38+ Ok a >>= f = f a
39+ -- TODO: add a definition for "fail", akin to
40+
41+ -- fail str = Errors [SomeException (error str)]
42+
43+ -- but *correct*, as this will throw an exception if you try to
44+ -- examine the exception.