What is wrong with this piece of code?

Hello all,
I am trying to serve html with servant using lucid,

Here is the code

{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module ServantLucidExample where

import Data.Proxy ( Proxy(Proxy) )
import qualified Lucid
import qualified Network.Wai.Handler.Warp as Warp
import Servant ( Handler, serve, Server )
import Servant.API ( Get, JSON, type (:<|>)(..), type (:>) )
import qualified Servant.HTML.Lucid

type NumberAPI
  = Get '[JSON] Int

type LoginAPI
  = "login" :> Get '[Servant.HTML.Lucid.HTML] (Lucid.Html ())

type API = NumberAPI :<|> LoginAPI

numberHandler :: Server NumberAPI
numberHandler = pure 42

loginHandler :: Server LoginAPI
loginHandler = pure someHtml
  where
  someHtml :: Lucid.Html ()
  someHtml = 
    Lucid.div_ do
      Lucid.p_ "Hello"
      Lucid.p_ "World"

handler :: Server API
handler = numberHandler :<|> loginHandler

main :: IO ()
main = Warp.run 8080 (serve (Proxy @API) handler)

using ghc 9.4.8
and latest servant, servant-lucid, lucid2, wai, warp libraries

it throws the following error

app/Main.hs:40:23: error:
    • No instance for (lucid-2.11.20230408:Lucid.Base.ToHtml
                         (Lucid.HtmlT Data.Functor.Identity.Identity ()))
        arising from a use of ‘serve’
      There are instances for similar types:
        instance (a ~ (), m ~ Data.Functor.Identity.Identity) =>
                 lucid-2.11.20230408:Lucid.Base.ToHtml
                   (lucid-2.11.20230408:Lucid.Base.HtmlT m a)
          -- Defined in ‘lucid-2.11.20230408:Lucid.Base’
    • In the second argument of ‘Warp.run’, namely
        ‘(serve (Proxy @API) handler)’
      In the expression: Warp.run 8080 (serve (Proxy @API) handler)
      In an equation for ‘main’:
          main = Warp.run 8080 (serve (Proxy @API) handler)
   |
40 | main = Warp.run 8080 (serve (Proxy @API) handler)
   |                       ^^^^^

I have no idea why this is throwing this error and how to fix it.
Please help

servant-lucid uses lucid not lucid2.

1 Like

One of the drawbacks of @chrisdone 's way of versioning packages, I guess :thinking:

2 Likes

The scheme will certainly cause confusing if the module names don’t change when the package name changes.

uhhh this is the right hint. took me a while to spot the clue in the type error. It appears you use HtmlT from ‘lucid2’ whereas the servant bindings expect the corresponding definition from ‘lucid’. These are two different libraries, not a version mismatch.

Yep, this solved it.
Thanks,
Btw what is the difference between lucid and lucid2.?

1 Like

From here:


…as for the versioning policy:

https://chrisdone.com/posts/ipp