Warm tip: This article is reproduced from serverfault.com, please click

cabal-Haskell + Persistent:解析错误,可能是缩进不正确或括号不匹配

(cabal - Haskell + Persistent: parse error possibly incorrect indentation or mismatched brackets)

发布于 2020-10-11 13:11:47

在过去的几个月中,我一直在学习Haskell,并尝试遵循有关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
|]

我以前没有遇到过这种语法(他们在本教程中谈论的是“ QuasiQuotes”,所以我猜这就是这个意思),但是我认为由于我直接从本教程中获取了代码,因此应该对其进行编译。但是,不会,并且出现以下错误:

/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

这是我的阴谋文件中的配置:

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

如果有人可以帮助我,那将是惊人的。

Questioner
Will Taylor
Viewed
11
Sudip Bhattarai 2020-11-29 10:53:42

我也是Haskell的初学者。遵循相同的文档,我想将模式定义移至另一个模块并将其导入main。它根本无法与相同的错误消息一起使用。

在2-3小时到处尝试复制/粘贴代码并使用postgres对其进行测试之后,我在此过程中意识到了一些问题。在文档中,包含语言功能的注释首先出现在文件顶部。

只需将评论移至其他所有内容的顶部

{-# 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
|]