-3

I'm trying to build this Hakyll blog. On trying to compile site.hs, the site-building program...

root@AR:/usr/local# git clone https://github.com/relrod/blog elrod.me
root@AR:/usr/local# cd elrod.me
root@AR:/usr/local/elrod.me# ghc --make site.hs

... I get the following type error:

[1 of 1] Compiling Main ( site.hs, site.o ) site.hs:28:70: error:
  Couldn't match type ‘unordered-containers-0.2.7.2:Data.HashMap.Base.HashMap Data.Text.Internal.Text aeson-1.1.0.0:Data.Aeson.Types.Internal.Value’ with ‘M.Map [Char] a1’
  Expected type: M.Map [Char] a1
    Actual type: Metadata

  In the third argument of ‘M.findWithDefault’, namely ‘metadata’
  In the expression: M.findWithDefault "No title" "title" metadata
  In an equation for ‘title’: title = M.findWithDefault "No title" "title" metadata

  Relevant bindings include title :: a1 (bound at site.hs:28:25)

How can I fix it? Below are the relevant parts of site.hs:

{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Map as M
import Data.Monoid ((<>))
import Hakyll
import Text.Pandoc.Options (readerSmart)

main :: IO ()
main = hakyll $ do
    -- etc.
    match "posts/*" $ do
        route $ setExtension "html"
        compile $ do
            let safetitle = field "safetitle" $ \item -> do
                    metadata <- getMetadata (itemIdentifier item)
                    let title = M.findWithDefault "No title" "title" metadata
                    return $ concatMap (\x -> if x == '\'' then "\\'" else [x]) title
            pandocCompilerWith defaultHakyllReaderOptions {readerSmart = False} defaultHakyllWriterOptions
                >>= saveSnapshot "content"
                >>= loadAndApplyTemplate "templates/post.html"    (postCtx tags <> safetitle)
                >>= loadAndApplyTemplate "templates/default.html" defaultContext
                >>= relativizeUrls
   -- etc.
apaderno
  • 28,547
  • 16
  • 75
  • 90
fisherman
  • 10,739
  • 5
  • 20
  • 16
  • Please edit your question to add enough explanation so that we don't have to guess what you are trying to do ("making `ghc --make site.hs` work" just isn't specific enough). If you are unsure about how to prepare and organise your question, consider having a look at recent questions with positive score in the [tag:haskell] tag to see what works well here. – duplode Feb 02 '17 at 15:07
  • The issue is that `getMetadata` returns a type of `Metadata` which is an alias for an `aeson` `Object` which is an alias for `HashMap`, but there's an attempt to access the value using the `Map` `findWithDefault` instead of the appropriate `HashMap` lookup function (`lookupDefault`?). – ryachza Feb 02 '17 at 17:03
  • @duplode I think the original question included the exact error message that was displayed, albeit formatted unpleasantly. I formatted things and included the relevant code snippet from the repository. – ryachza Feb 02 '17 at 18:43
  • @ryachza The point is that the OP isn't supposed to simply paste raw output from their terminal with zero explanation or formatting and expect us to pick up the pieces. I'm all for being patient with imperfect questions, but this is a bit much. – duplode Feb 02 '17 at 18:51
  • 1
    @duplode I guess I just struggled with the "unclear what you're asking" vote, since I found it to be perfectly clear what was being asked. I completely agree that some effort should have been made to format the question, but "unclear what your asking" and general advice to review other questions seems much less helpful than either directly addressing the formatting, or demonstrating how this question in particular could be improved. – ryachza Feb 02 '17 at 18:54
  • @ryachza "This piece of code somewhere in another site is broken, how to fix it?" is not a proper question here. It is not clear at all without undue effort from the readers. The formatting is far less serious problem, and I wouldn't have voted to close just because of it. I wouldn't have voted to close as unclear if the OP had at least attempted to explain what is going on, or to pinpoint the relevant piece of code. – duplode Feb 02 '17 at 19:15
  • @duplode "I performed this sequence of actions, I don't understand the error message I'm seeing". The fact that the code was on another site I'll admit was maybe not ideal but GitHub is common enough that it wasn't bad, and honestly I preferred that over potentially copying the 100+ lines and if the asker didn't understand the error linking to the repository allows readers to see the full context. For me (and you're of course welcome to feel differently), the *only* issue was the formatting (so I tried to correct it). I didn't even give anything else a second thought. – ryachza Feb 02 '17 at 19:53
  • @ryachza GitHub isn't better. With 100+ lines, we ask the OP to trim it down ("Your error says line 29. What is it in your code?"). With everything on GitHub, we ask the OP to copy the key parts here so that the question stands on its own (cf. the "Help others reproduce the problem" section [here](http://stackoverflow.com/help/how-to-ask)). Before that, though, there has to be some explanation, so that visitors other than us three can land here and have a general idea of the issue immediately -- "I'm trying to build a Hakyll site, and I'm getting this error from this piece of code." – duplode Feb 02 '17 at 20:44
  • @duplode I suppose we can simply disagree. I would consider many of your points to be subjective, and the items where we agree there seems to be a difference in preference for method of response/education. The question appears to no longer be "on hold" so, until next time I guess. – ryachza Feb 03 '17 at 13:16
  • @ryachza, could u pls tell me concretely how should i modify site.hs? – fisherman Feb 03 '17 at 15:05
  • @fisherman I think my earliest comment indicates a possible solution (use `lookupDefault` in `Data.HashMap`), but if there have been other API breaking changes you may encounter additional errors. I think the answer provided below provides another possibility - use an older version of Hakyll. – ryachza Feb 03 '17 at 15:34
  • @ryachza We can agree to disagree. I should note, though, that my points are not subjective; they come straight from the site rules on what is expected of questions here. It's fine if you e.g. dislike the rules or think I'm a pedant for insisting this much on them, but in any case I'm not making stuff up. – duplode Feb 03 '17 at 22:14
  • @fisherman Please take my comments here into account next time you ask a question. A little more care in preparing your question will lead to quicker replies and better answers, as well as making things easier for those trying to help you. – duplode Feb 03 '17 at 22:22
  • @fisherman Replying to your comment to gallais' answer: That's good to hear. [This page at the Help Center](http://stackoverflow.com/help/how-to-ask) has good advice on what you should include in your future questions. – duplode Feb 05 '17 at 02:38
  • Possible duplicate of [M.Map sudden expected type error](https://stackoverflow.com/questions/37801054/m-map-sudden-expected-type-error) – epsilonhalbe Jul 09 '17 at 16:27

1 Answers1

3

The problem here is that the representation of Metadata has changed since version 4.4.2.0 of hakyll (the lower bound in the .cabal of the project you're trying to build).

In 4.4.2.0, Metadata is a Map String String

In 4.9.5.0 (the latest), Metadata is an Object.

You need to either use an older version of Hakyll (e.g. by adding an upper bound in the .cabal file ensuring that Metadata's representation matches the one in version 4.4.2.0) or update the code to take into account the newer interface and bump the lower bound.

gallais
  • 11,823
  • 2
  • 30
  • 63
  • 1
    The change to what `Metadata` is happened in *hakyll* 4.8.0.0. *aeson*'s `Object` is involved because Hakyll now uses the *yaml* package to handle the YAML metadata, and that, in turn, is built upon the *aeson* infrastructure. – duplode Feb 03 '17 at 22:43
  • @duplode, ok,later i will describe my questions in detail.for the current question,could u ALSO pls tell me concretely how should i modify site.hs? THANK U VERY MUCH. – fisherman Feb 05 '17 at 02:25