stylish-haskell icon indicating copy to clipboard operation
stylish-haskell copied to clipboard

Parse error in the absence of -XFlexibleContexts

Open LambdaP opened this issue 5 years ago • 2 comments

The following piece of code (essentially a modified snippet from Thinking with Types by Sandy Maguire) compiles and runs fine (GHC 8.6.3 on macOS 10.14.2 installed with Stack using lts-13.4).

{-# LANGUAGE DataKinds            #-}
{-# LANGUAGE GADTs                #-}
{-# LANGUAGE KindSignatures       #-}
{-# LANGUAGE TypeOperators        #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances    #-}

module Main where

import Data.Kind ( Type )

main :: IO ()
main = do
  let a = True :# "True" :# HNil
      b = a
  print (a == b)

data HList (ts :: [Type]) where
  HNil :: HList '[]
  (:#) :: t -> HList ts -> HList (t ': ts)
infixr 5 :#

instance Eq (HList '[]) where
  HNil == HNil = True

instance (Eq t, Eq (HList ts)) => Eq (HList (t ': ts)) where
  (a :# as) == (b :# bs) = a == b && as == bs

However, stylish-haskell (0.9.2.1) won’t parse it unless -XFlexibleContexts is active:

$ stylish-haskell ../bug/src/Main.hs                                                                                                                    
Language.Haskell.Stylish.Parse.parseModule: could not parse ../bug/src/Main.hs: ParseFailed (SrcLoc "<unknown>.hs" 28 1) "Malformed context: FlexibleContexts is not enabled"

Adding the pragma {-# LANGUAGE FlexibleContexts #-} makes stylish-haskell behave as expected, but it’s unsatisfying.

Given this comment, I realize that the issue may not be with stylish-haskell itself. I’ll investigate.

LambdaP avatar Jan 21 '19 14:01 LambdaP

I have tested compiling stylish-haskell against haskell-src-exts-1.21.0, but the bug remains. If I find the time I will dig into haskell-src-exts directly.

LambdaP avatar Jan 21 '19 15:01 LambdaP

The following config file will work around this and related issues, without forcing a workaround to your main haskell source code:

steps:
  - simple_align: {}
  - imports: {}
  - language_pragmas: {}
  - trailing_whitespace: {}

language_extensions:
  - FlexibleContexts # stylish-haskell bug, see jaspervdj/stylish-haskell#230
  - MultiParamTypeClasses # stylish-haskell bug, see jaspervdj/stylish-haskell#103 and haskell-suite/haskell-src-exts#304

infinity0 avatar Mar 10 '20 16:03 infinity0