haskelltypeshappstack

How to get value out of nested synonym type


I have defined my own type called CtrlV:

{-# LANGUAGE TemplateHaskell #-}
import Data.Data (Data, Typeable)
import Happstack.Server (Response, ServerPartT)
import Web.Routes (RouteT)
import Web.Routes.TH (derivePathInfo)

type App = ServerPartT IO
type CtrlV'   = RouteT Sitemap App
type CtrlV    = CtrlV' Response

data Sitemap   = Home | User
    deriving (Eq, Ord, Read, Show, Typeable, Data)
$(derivePathInfo ''Sitemap)

I have this function for example:

import Happstack.Foundation
import Happstack.Server (ok, toResponse)
import Web.Routes (showURL)
createResponse :: CtrlV
createResponse = do
     url <- showURL Home
     ok $ toResponse (show url)

I want to write a test for this function and i want to check Response has the correct result. But i can get Response out of my type CtrlV. Is there an easy way to achieve this?


Solution

  • To step back from the specifics of the libraries your using, here is how type works:

    I can declare a type synonym

    type ListOfInts = [Int]
    

    I know I can use ListOfInts anywhere I would use [Int]:

    f :: ListOfInts -> Int
    f xs = sum xs
    
    g :: [Int] -> Int
    g xs = product xs
    
    list1 :: [Int]
    list1 = [1,2,3]
    
    list2 :: ListOfInts
    list2 = [4,5,6]
    
    f list1
    > 6
    f list2
    > 15
    
    g list1
    > 6
    g list2
    > 120
    

    And now I can use f and g on both list1 and list2, despite the fact that they are declared with "different" types. Type just declares a synonym for a type, not a new type (which would use the newtype keyword).

    In summary, You can use CtrlV the same way you would use Ctrlv' Response, which you can use the same way you would use RouteT SiteMap App Response, which you can use the same way you would use RouteT SiteMap (ServerPartT IO) Response, because Ctrlv is RouteT SiteMap (ServerPartT IO) Response.