X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FStatusCode%2FInternal.hs;h=e8785c3c106a49a5ba124edd188ddae90e8fc5fd;hb=a122079;hp=21210375bbfa3ae5d34cd84fe759bf8c6ecbe7ad;hpb=5f2ef377345fc47aabc63c1325df82c1cd9da9ed;p=Lucu.git diff --git a/Network/HTTP/Lucu/StatusCode/Internal.hs b/Network/HTTP/Lucu/StatusCode/Internal.hs index 2121037..e8785c3 100644 --- a/Network/HTTP/Lucu/StatusCode/Internal.hs +++ b/Network/HTTP/Lucu/StatusCode/Internal.hs @@ -4,15 +4,15 @@ , MultiParamTypeClasses , OverlappingInstances , TemplateHaskell + , TypeFamilies + , UndecidableInstances , UnicodeSyntax , ViewPatterns #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Network.HTTP.Lucu.StatusCode.Internal ( StatusCode(..) - , SomeStatusCode(..) - , (≈) - , (≉) + , SomeStatusCode , statusCodes ) where @@ -25,6 +25,7 @@ import qualified Data.ByteString.Lazy.Char8 as Lazy import Data.Convertible.Base import Data.Convertible.Instances.Ascii () import Data.Convertible.Utils +import Data.Eq.Indirect import Data.List import Language.Haskell.TH.Lib import Language.Haskell.TH.Syntax @@ -32,13 +33,13 @@ import Language.Haskell.TH.Quote import Network.HTTP.Lucu.Parser import Prelude.Unicode --- |The type class for HTTP status codes. +-- |Type class for HTTP status codes. -- -- Declaring types for each statuses is surely a pain. See: --- 'statusCodes' +-- 'statusCodes' quasi-quoter. -- -- Minimal complete definition: 'numericCode' and 'textualStatus'. -class Show sc ⇒ StatusCode sc where +class (Eq sc, Show sc) ⇒ StatusCode sc where -- |Return the 3-digit integer for this status e.g. @200@ numericCode ∷ sc → Int -- |Return the combination of 3-digit integer and reason phrase @@ -48,31 +49,24 @@ class Show sc ⇒ StatusCode sc where fromStatusCode ∷ sc → SomeStatusCode fromStatusCode = SomeStatusCode --- |Container type for 'StatusCode' type class. +-- |Equivalence of 'StatusCode's. Two 'StatusCode's @α@ and +-- @β@ are said to be equivalent iff @'numericCode' α '==' +-- 'numericCode' β@. +instance StatusCode sc ⇒ Eq' sc where + type Unified sc = Int + {-# INLINE CONLIKE unify #-} + unify = numericCode + +-- |Container type for the 'StatusCode' type class. data SomeStatusCode = ∀sc. StatusCode sc ⇒ SomeStatusCode sc -instance Show SomeStatusCode where - show (SomeStatusCode sc) = show sc - instance Eq SomeStatusCode where - (SomeStatusCode α) == (SomeStatusCode β) = α ≈ β + {-# INLINE CONLIKE (==) #-} + (==) = (≡:) -infix 4 ≈, ≉ --- |Equivalence of 'StatusCode's. Two 'StatusCode's @a@ and @b@ are --- said to be equivalent iff @'numericCode' a '==' 'numericCode' b@. --- --- U+2248, ALMOST EQUAL TO -(≈) ∷ (StatusCode α, StatusCode β) ⇒ α → β → Bool -{-# INLINE (≈) #-} -α ≈ β = numericCode α ≡ numericCode β - --- |@(a ≉ b) '==' 'not' (a ≈ b)@ --- --- U+2249, NOT ALMOST EQUAL TO -(≉) ∷ (StatusCode α, StatusCode β) ⇒ α → β → Bool -{-# INLINE (≉) #-} -(≉) = ((¬) ∘) ∘ (≈) +instance Show SomeStatusCode where + show (SomeStatusCode sc) = show sc instance StatusCode SomeStatusCode where numericCode (SomeStatusCode sc) = numericCode sc @@ -110,17 +104,17 @@ instance StatusCode sc ⇒ ConvertAttempt sc AsciiBuilder where -- becomes: -- -- @ --- data OK = OK deriving ('Show') +-- data OK = OK deriving ('Eq', 'Show') -- instance OK where -- 'numericCode' _ = 200 -- 'textualStatus' _ = 'cs' (\"200 OK\" ∷ Ascii) -- --- data BadRequest = BadRequest deriving ('Show') +-- data BadRequest = BadRequest deriving ('Eq', 'Show') -- instance BadRequest where -- 'numericCode' _ = 400 -- 'textualStatus' _ = 'cs' (\"400 Bad Request\" ∷ Ascii) -- --- data MethodNotAllowed = MethodNotAllowed deriving ('Show') +-- data MethodNotAllowed = MethodNotAllowed deriving ('Eq', 'Show') -- instance MethodNotAllowed where -- 'numericCode' _ = 405 -- 'textualStatus' _ = 'cs' (\"405 Method Not Allowed\" ∷ Ascii) @@ -178,14 +172,14 @@ statusDecl (num, phrase) name = mkName $ concatMap cs phrase dataDecl ∷ Q Dec - dataDecl = dataD (cxt []) name [] [con] [''Show] + dataDecl = dataD (cxt []) name [] [con] [''Eq, ''Show] instanceDecl ∷ Q [Dec] instanceDecl = [d| instance StatusCode $typ where - {-# INLINE numericCode #-} + {-# INLINE CONLIKE numericCode #-} numericCode _ = $(lift num) - {-# INLINE textualStatus #-} + {-# INLINE CONLIKE textualStatus #-} textualStatus _ = $txt |]