I have been learning Haskell for the last several months and am trying to follow along with this tutorial on Haskell + Persistent:
https://www.yesodweb.com/book/persistent#persistent_code_generation
module Main where
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Database.Persist
import Database.Persist.TH
import Database.Persist.Sqlite
import Control.Monad.IO.Class (liftIO)
mkPersist sqlSettings [persistLowerCase|
Person
name String
age Int
deriving Show
|]
I have not come across this syntax before (they talk about 'QuasiQuotes' in the tutorial so I'm guessing that is what this is), but I think as I have taken the code directly from the tutorial it should compile. However, it won't, and I get the following error:
/home/will/programming/learn-haskell/postgres-example/app/Main.hs:15:1: error:
parse error (possibly incorrect indentation or mismatched brackets)
|
15 | Person
| ^
-- While building package postgres-example-0.1.0.0 using:
/home/will/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_3.0.1.0_ghc-8.8.4 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.0.1.0 build exe:simple --ghc-options " -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
Here is my configuration inside of my cabal file:
name: postgres-example
version: 0.1.0.0
synopsis: An example of using Haskell and PostGres with persistent
description: Please see README.md
license: BSD3
license-file: LICENSE
author: Will Taylor
copyright: 2020, Will Taylor
category: Web
build-type: Simple
cabal-version: >=1.10
executable simple
hs-source-dirs: app
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, persistent
, persistent-template
, persistent-postgresql
, monad-logger
default-language: Haskell2010
If anyone could help me out then that would be amazing.
I am also a beginner in Haskell. Following the same documentation, I was trying to move the schema definition to another module and import it in main. It simply didn't work with the same error message.
After 2-3 hours of trying out copy/pasting code here and there, and testing it with postgres, I realized something during the process. In the documentation the comments containing Language features appeared on top of file before anything else.
Simply move the comments to the top before everything else
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
module Sql.Schema where
import Database.Persist
import Database.Persist.TH
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
firstName String
lastName String
age Int Maybe
PersonName firstName lastName
deriving Show
BlogPost
title String
authorId PersonId
deriving Show
|]