Skip to content

Commit

Permalink
Allow compilation under GHC8.
Browse files Browse the repository at this point in the history
This commit makes me sad :-(
  • Loading branch information
nc6 committed Apr 22, 2024
1 parent d690b52 commit 8f01c5d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 69 deletions.
45 changes: 33 additions & 12 deletions cuddle.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,29 @@ extra-doc-files: CHANGELOG.md
common warnings
ghc-options: -Wall

common ghc2021
-- These options are all on by default in GHC2021, so once we drop GHC8 we
-- can remove this section.
default-extensions:
DataKinds
DeriveGeneric
DeriveTraversable
FlexibleContexts
FlexibleInstances
GeneralizedNewtypeDeriving
ImportQualifiedPost
InstanceSigs
MultiParamTypeClasses
NamedFieldPuns
PolyKinds
RankNTypes
ScopedTypeVariables
StandaloneDeriving
TypeApplications
TypeSynonymInstances

library
import: warnings
import: warnings, ghc2021
exposed-modules:
Codec.CBOR.Cuddle.CBOR.Gen
Codec.CBOR.Cuddle.CDDL
Expand All @@ -36,7 +57,7 @@ library

-- other-extensions:
build-depends:
, base ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, base ^>=4.14.3.0 || ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, bytestring
, capability
, cborg
Expand All @@ -54,31 +75,31 @@ library
, text

hs-source-dirs: src
default-language: GHC2021
default-language: Haskell2010

executable example
import: warnings
default-language: GHC2021
import: warnings, ghc2021
default-language: Haskell2010
other-modules: Conway

-- other-extensions:
hs-source-dirs: example
main-is: Main.hs
build-depends:
, base ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, base ^>=4.14.3.0 || ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, cuddle
, megaparsec
, prettyprinter
, random
, text

executable cuddle
import: warnings
default-language: GHC2021
import: warnings, ghc2021
default-language: Haskell2010
hs-source-dirs: ./bin/
main-is: Main.hs
build-depends:
, base ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, base ^>=4.14.3.0 || ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, cborg
, cuddle
, megaparsec
Expand All @@ -88,8 +109,8 @@ executable cuddle
, text

test-suite cuddle-test
import: warnings
default-language: GHC2021
import: warnings, ghc2021
default-language: Haskell2010
other-modules:
Test.Codec.CBOR.Cuddle.CDDL.Gen
Test.Codec.CBOR.Cuddle.CDDL.Parser
Expand All @@ -100,7 +121,7 @@ test-suite cuddle-test
hs-source-dirs: test
main-is: Main.hs
build-depends:
, base ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, base ^>=4.14.3.0 || ^>=4.16.3.0 || ^>=4.18.1.0 || ^>=4.19.0.0
, cuddle
, hspec
, hspec-megaparsec
Expand Down
1 change: 0 additions & 1 deletion src/Codec/CBOR/Cuddle/CBOR/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}

-- | Generate example CBOR given a CDDL specification
Expand Down
9 changes: 4 additions & 5 deletions src/Codec/CBOR/Cuddle/CDDL/CTree.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NoFieldSelectors #-}

module Codec.CBOR.Cuddle.CDDL.CTree where

Expand Down Expand Up @@ -53,19 +52,19 @@ traverseCTree atNode (Map xs) = Map <$> traverse atNode xs
traverseCTree atNode (Array xs) = Array <$> traverse atNode xs
traverseCTree atNode (Group xs) = Group <$> traverse atNode xs
traverseCTree atNode (Choice xs) = Choice <$> traverse atNode xs
traverseCTree atNode (KV k v cut) = do
traverseCTree atNode (KV k v c) = do
k' <- atNode k
v' <- atNode v
pure $ KV k' v' cut
pure $ KV k' v' c
traverseCTree atNode (Occur i occ) = flip Occur occ <$> atNode i
traverseCTree atNode (Range f t inc) = do
f' <- atNode f
t' <- atNode t
pure $ Range f' t' inc
traverseCTree atNode (Control op t c) = do
traverseCTree atNode (Control o t c) = do
t' <- atNode t
c' <- atNode c
pure $ Control op t' c'
pure $ Control o t' c'
traverseCTree atNode (Enum ref) = Enum <$> atNode ref
traverseCTree atNode (Unwrap ref) = Unwrap <$> atNode ref

Expand Down
34 changes: 17 additions & 17 deletions src/Codec/CBOR/Cuddle/CDDL/Resolve.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedRecordDot #-}
-- {-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoFieldSelectors #-}

-- | Module containing tools for 'resolving' CDDL
--
Expand Down Expand Up @@ -37,7 +36,8 @@ where

import Capability.Accessors (Field (..), Lift (..))
import Capability.Error (HasThrow, MonadError (..), throw)
import Capability.Reader (HasReader, MonadReader (..), ask, local)
import Capability.Reader (HasReader, MonadReader (..), ask)
import qualified Capability.Reader as Reader (local)
import Capability.Sink (HasSink)
import Capability.Source (HasSource)
import Capability.State (HasState, MonadState (..), modify)
Expand Down Expand Up @@ -94,14 +94,14 @@ asMap (CDDL rules) = foldl' assignOrExtend Map.empty (stripComment <$> rules)
Maybe GenericParam ->
Maybe (Parametrised TypeOrGroup) ->
Maybe (Parametrised TypeOrGroup)
extend tog _gps (Just existing) = case (existing.underlying, tog) of
extend tog _gps (Just existing) = case (underlying existing, tog) of
(TOGType _, TOGType (Type0 new)) ->
Just $
existing
& field @"underlying"
% _Ctor @"TOGType"
% _Ctor @"Type0"
%~ (`NE.append` new)
%~ (<> new)
-- From the CDDL spec, I don't see how one is meant to extend a group.
-- According to the description, it's meant to add a group choice, but the
-- assignment to a group takes a 'GrpEntry', not a Group, and there is no
Expand Down Expand Up @@ -334,15 +334,15 @@ resolveRef env (Ref n args) = case Map.lookup n postludeBinding of
Just pterm -> case args of
[] -> Right . DIt $ CTree.Postlude pterm
xs -> Left $ ArgsToPostlude pterm xs
Nothing -> case Map.lookup n env.global of
Just (parameters -> params) ->
if length params == length args
Nothing -> case Map.lookup n (global env) of
Just (parameters -> params') ->
if length params' == length args
then
let localBinds = Map.fromList $ zip params args
let localBinds = Map.fromList $ zip params' args
newEnv = env & field @"local" %~ Map.union localBinds
in RuleRef n <$> traverse (resolveRef newEnv) args
else Left $ MismatchingArgs n params
Nothing -> case Map.lookup n env.local of
else Left $ MismatchingArgs n params'
Nothing -> case Map.lookup n (local env) of
Just _ -> Right $ GenericRef n
Nothing -> Left $ UnboundReference n

Expand Down Expand Up @@ -460,14 +460,14 @@ synthMono n@(Name origName) args =
globalBinds <- ask @"global"
case Map.lookup n globalBinds of
Just (Unparametrised _) -> throwNR $ MismatchingArgs n []
Just (Parametrised r params) ->
if length params == length args
Just (Parametrised r params') ->
if length params' == length args
then
let localBinds = Map.fromList $ zip params args
in local @"local" (Map.union localBinds) $ do
let localBinds = Map.fromList $ zip params' args
in Reader.local @"local" (Map.union localBinds) $ do
foo <- resolveGenericRef r
modify @"synth" $ Map.insert fresh foo
else throwNR $ MismatchingArgs n params
else throwNR $ MismatchingArgs n params'
Nothing -> throwNR $ UnboundReference n
pure fresh

Expand Down Expand Up @@ -508,7 +508,7 @@ buildMonoCTree ::
CTreeRoot DistRef ->
Either NameResolutionFailure (CTreeRoot' Identity MonoRef)
buildMonoCTree (CTreeRoot ct) = do
let a1 = runExceptT (monoCTree monoC).runMonoM
let a1 = runExceptT $ runMonoM (monoCTree monoC)
a2 = runStateT a1 mempty
(er, newBindings) = runReader a2 initBindingEnv
CTreeRoot r <- er
Expand Down
Loading

0 comments on commit 8f01c5d

Please sign in to comment.