Skip to content

Commit

Permalink
format using ormolu
Browse files Browse the repository at this point in the history
  • Loading branch information
robx committed Jan 3, 2020
1 parent 985227a commit 6a269ca
Show file tree
Hide file tree
Showing 45 changed files with 4,634 additions and 4,398 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ compare:
$(MAKE) -C tests/examples compare DRAW=$(DRAW)

format:
find src -name '*.hs' | xargs brittany --write-mode=inplace
find tests -name '*.hs' | xargs brittany --write-mode=inplace
find src -name '*.hs' | xargs ormolu -m inplace
find tests -name '*.hs' | xargs ormolu -m inplace
36 changes: 22 additions & 14 deletions src/Data/Code.hs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
module Data.Code where

import Data.Map.Strict ( Map )

import Data.Grid
import Data.GridShape
import Data.Grid
import Data.GridShape
import Data.Map.Strict (Map)

type Code = [CodePart]

data CodePart =
Rows' [Int] -- ^ Rows of cells, counted from the bottom.
| Cols [Int] -- ^ Cols of cells, counted from the left.
| RowsN' [Int] -- ^ Rows of nodes, counted from the bottom.
| ColsN [Int] -- ^ Cols of nodes, counted from the left.
| LabelsN (Grid N (Maybe Char)) -- ^ Nodes, labeld by letters.
| LRows' (Map Char Int) -- ^ Rows of cells, counted from the bottom.
| LCols (Map Char Int) -- ^ Cols of cells, counted from the left.
| LRowsN' (Map Char Int) -- ^ Rows of nodes, counted from the bottom.
| LColsN (Map Char Int) -- ^ Cols of nodes, counted from the left.
data CodePart
= -- | Rows of cells, counted from the bottom.
Rows' [Int]
| -- | Cols of cells, counted from the left.
Cols [Int]
| -- | Rows of nodes, counted from the bottom.
RowsN' [Int]
| -- | Cols of nodes, counted from the left.
ColsN [Int]
| -- | Nodes, labeld by letters.
LabelsN (Grid N (Maybe Char))
| -- | Rows of cells, counted from the bottom.
LRows' (Map Char Int)
| -- | Cols of cells, counted from the left.
LCols (Map Char Int)
| -- | Rows of nodes, counted from the bottom.
LRowsN' (Map Char Int)
| -- | Cols of nodes, counted from the left.
LColsN (Map Char Int)
51 changes: 26 additions & 25 deletions src/Data/Component.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module Data.Component where

import qualified Data.Map.Strict as Map
import Data.Elements
import Data.Grid
import Data.GridShape
import qualified Data.Map.Strict as Map

import Data.GridShape
import Data.Grid
import Data.Elements

data Component a =
Grid !GridStyle !(Grid C ())
data Component a
= Grid !GridStyle !(Grid C ())
| Regions !(Grid C Char)
| NodeGrid !(Grid N Decoration)
| CellGrid !(Grid C Decoration)
Expand All @@ -18,53 +17,55 @@ data Component a =
| Note [Decoration]
| RawComponent !Size !a

data Tag =
Puzzle
data Tag
= Puzzle
| Solution
| Code
deriving (Eq, Show)
deriving (Eq, Show)

data TaggedComponent a = TaggedComponent (Maybe Tag) (PlacedComponent a)

data Placement =
Atop
data Placement
= Atop
| West
| North
| TopRight
deriving (Eq, Show)
deriving (Eq, Show)

data PlacedComponent a = PlacedComponent Placement (Component a)

tagged :: Tag -> TaggedComponent a -> Bool
tagged tag component = case component of
TaggedComponent (Just t) _ -> tag == t
_ -> False
_ -> False

untag :: TaggedComponent a -> PlacedComponent a
untag (TaggedComponent _ c) = c

extractPuzzle :: Bool -> [TaggedComponent a] -> [PlacedComponent a]
extractPuzzle code tcs = map untag . filter want $ tcs
where want c = not (tagged Solution c) && (code || not (tagged Code c))
where
want c = not (tagged Solution c) && (code || not (tagged Code c))

extractSolution :: Bool -> [TaggedComponent a] -> Maybe [PlacedComponent a]
extractSolution code tcs = if haveSol
then Just . map untag . filter want $ tcs
else Nothing
where
haveSol = not . null . filter (tagged Solution) $ tcs
want c = not (tagged Puzzle c) && (code || not (tagged Code c))
extractSolution code tcs =
if haveSol
then Just . map untag . filter want $ tcs
else Nothing
where
haveSol = not . null . filter (tagged Solution) $ tcs
want c = not (tagged Puzzle c) && (code || not (tagged Code c))

data GridStyle =
GridDefault
data GridStyle
= GridDefault
| GridDefaultIrregular
| GridDashed
| GridDots
| GridPlain
| GridPlainDashed

data Decoration =
Blank
data Decoration
= Blank
| Letter !Char
| Letters String
| InvertedLetters String
Expand Down
212 changes: 108 additions & 104 deletions src/Data/Compose.hs
Original file line number Diff line number Diff line change
@@ -1,135 +1,139 @@
{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts, RankNTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}

-- |
-- Helpers to string together parser and renderer by puzzle type.

module Data.Compose
( compose
( compose,
)
where

import Data.Yaml ( Parser
, Value
)

import Parse.Puzzle
import Data.Lib
import Draw.Draw
import Draw.Lib
import Data.PuzzleTypes

import qualified Parse.PuzzleTypes as R
import qualified Draw.PuzzleTypes as D
import Data.Lib
import Data.PuzzleTypes
import Data.Yaml
( Parser,
Value,
)
import Draw.Draw
import Draw.Lib
import qualified Draw.PuzzleTypes as D
import Parse.Puzzle
import qualified Parse.PuzzleTypes as R

compose
:: Backend' b
=> PuzzleType
-> ((Value, Maybe Value) -> Parser (Drawing b, Maybe (Drawing b)))
compose ::
Backend' b =>
PuzzleType ->
((Value, Maybe Value) -> Parser (Drawing b, Maybe (Drawing b)))
compose = handle drawPuzzleMaybeSol

-- | A function to compose an arbitrary matching pair of parser and renderer.
-- In @PuzzleHandler b a@, @b@ is the rendering backend type, while @a@ is
-- the result type of the composition.
type PuzzleHandler b a = forall p q.
ParsePuzzle p q -> Drawers b p q -> a
type PuzzleHandler b a =
forall p q.
ParsePuzzle p q ->
Drawers b p q ->
a

-- | @handle h t@ composes the parser and renderer for the puzzle
-- type @t@ with the handler @h@.
handle :: Backend' b => PuzzleHandler b a -> PuzzleType -> a
handle f LITS = f R.lits D.lits
handle f Geradeweg = f R.geradeweg D.geradeweg
handle f Fillomino = f R.fillomino D.fillomino
handle f Masyu = f R.masyu D.masyu
handle f Nurikabe = f R.nurikabe D.nurikabe
handle f LatinTapa = f R.latintapa D.latintapa
handle f Sudoku = f R.sudoku D.sudoku
handle f ThermoSudoku = f R.thermosudoku D.thermosudoku
handle f Pyramid = f R.pyramid D.pyramid
handle f RowKropkiPyramid = f R.kpyramid D.kpyramid
handle f SlitherLink = f R.slither D.slither
handle f SlitherLinkLiar = f R.liarslither D.liarslither
handle f WordLoop = f R.wordloop D.wordloop
handle f WordSearch = f R.wordsearch D.wordsearch
handle f CurveData = f R.curvedata D.curvedata
handle f DoubleBack = f R.doubleback D.doubleback
handle f Slalom = f R.slalom D.slalom
handle f Compass = f R.compass D.compass
handle f MeanderingNumbers = f R.meanderingnumbers D.meanderingnumbers
handle f Tapa = f R.tapa D.tapa
handle f JapaneseSums = f R.japanesesums D.japanesesums
handle f Coral = f R.coral D.coral
handle f MaximalLengths = f R.maximallengths D.maximallengths
handle f Labyrinth = f R.labyrinth D.labyrinth
handle f Bahnhof = f R.bahnhof D.bahnhof
handle f BlackoutDominos = f R.blackoutDominos D.blackoutDominos
handle f TwilightTapa = f R.tapa D.tapa
handle f TapaCave = f R.tapa D.tapa
handle f DominoPillen = f R.dominoPills D.dominoPills
handle f AngleLoop = f R.angleLoop D.angleLoop
handle f Shikaku = f R.shikaku D.shikaku
handle f SlovakSums = f R.slovaksums D.slovaksums
handle f Anglers = f R.anglers D.anglers
handle f Dominos = f R.dominos D.dominos
handle f FillominoCheckered = f R.fillomino D.fillominoCheckered
handle f FillominoLoop = f R.fillominoLoop D.fillominoLoop
handle f Cave = f R.cave D.cave
handle f Numberlink = f R.numberlink D.numberlink
handle f Skyscrapers = f R.skyscrapers D.skyscrapers
handle f SkyscrapersStars = f R.skyscrapersStars D.skyscrapersStars
handle f SkyscrapersFrac = f R.tightfitskyscrapers D.tightfitskyscrapers
handle f LITS = f R.lits D.lits
handle f Geradeweg = f R.geradeweg D.geradeweg
handle f Fillomino = f R.fillomino D.fillomino
handle f Masyu = f R.masyu D.masyu
handle f Nurikabe = f R.nurikabe D.nurikabe
handle f LatinTapa = f R.latintapa D.latintapa
handle f Sudoku = f R.sudoku D.sudoku
handle f ThermoSudoku = f R.thermosudoku D.thermosudoku
handle f Pyramid = f R.pyramid D.pyramid
handle f RowKropkiPyramid = f R.kpyramid D.kpyramid
handle f SlitherLink = f R.slither D.slither
handle f SlitherLinkLiar = f R.liarslither D.liarslither
handle f WordLoop = f R.wordloop D.wordloop
handle f WordSearch = f R.wordsearch D.wordsearch
handle f CurveData = f R.curvedata D.curvedata
handle f DoubleBack = f R.doubleback D.doubleback
handle f Slalom = f R.slalom D.slalom
handle f Compass = f R.compass D.compass
handle f MeanderingNumbers = f R.meanderingnumbers D.meanderingnumbers
handle f Tapa = f R.tapa D.tapa
handle f JapaneseSums = f R.japanesesums D.japanesesums
handle f Coral = f R.coral D.coral
handle f MaximalLengths = f R.maximallengths D.maximallengths
handle f Labyrinth = f R.labyrinth D.labyrinth
handle f Bahnhof = f R.bahnhof D.bahnhof
handle f BlackoutDominos = f R.blackoutDominos D.blackoutDominos
handle f TwilightTapa = f R.tapa D.tapa
handle f TapaCave = f R.tapa D.tapa
handle f DominoPillen = f R.dominoPills D.dominoPills
handle f AngleLoop = f R.angleLoop D.angleLoop
handle f Shikaku = f R.shikaku D.shikaku
handle f SlovakSums = f R.slovaksums D.slovaksums
handle f Anglers = f R.anglers D.anglers
handle f Dominos = f R.dominos D.dominos
handle f FillominoCheckered = f R.fillomino D.fillominoCheckered
handle f FillominoLoop = f R.fillominoLoop D.fillominoLoop
handle f Cave = f R.cave D.cave
handle f Numberlink = f R.numberlink D.numberlink
handle f Skyscrapers = f R.skyscrapers D.skyscrapers
handle f SkyscrapersStars = f R.skyscrapersStars D.skyscrapersStars
handle f SkyscrapersFrac = f R.tightfitskyscrapers D.tightfitskyscrapers
handle f SkyscrapersTightfit = f R.tightfitskyscrapers D.tightfitskyscrapers
handle f TurningFences = f R.slither D.slither
handle f Summon = f R.summon D.summon
handle f Baca = f R.baca D.baca
handle f Buchstabensalat = f R.buchstabensalat D.buchstabensalat
handle f Doppelblock = f R.doppelblock D.doppelblock
handle f SudokuDoppelblock = f R.sudokuDoppelblock D.sudokuDoppelblock
handle f Loopki = f R.loopki D.loopki
handle f Scrabble = f R.scrabble D.scrabble
handle f Neighbors = f R.neighbors D.neighbors
handle f Starbattle = f R.starbattle D.starbattle
handle f Heyawake = f R.heyawake D.heyawake
handle f Pentominous = f R.pentominous D.pentominous
handle f ColorAkari = f R.colorakari D.colorakari
handle f TurningFences = f R.slither D.slither
handle f Summon = f R.summon D.summon
handle f Baca = f R.baca D.baca
handle f Buchstabensalat = f R.buchstabensalat D.buchstabensalat
handle f Doppelblock = f R.doppelblock D.doppelblock
handle f SudokuDoppelblock = f R.sudokuDoppelblock D.sudokuDoppelblock
handle f Loopki = f R.loopki D.loopki
handle f Scrabble = f R.scrabble D.scrabble
handle f Neighbors = f R.neighbors D.neighbors
handle f Starbattle = f R.starbattle D.starbattle
handle f Heyawake = f R.heyawake D.heyawake
handle f Pentominous = f R.pentominous D.pentominous
handle f ColorAkari = f R.colorakari D.colorakari
handle f PersistenceOfMemory = f R.persistenceOfMemory D.persistenceOfMemory
handle f ABCtje = f R.abctje D.abctje
handle f Kropki = f R.kropki D.kropki
handle f StatuePark = f R.statuepark D.statuepark
handle f PentominousBorders = f R.pentominousBorders D.pentominousBorders
handle f NanroSignpost = f R.nanroSignpost D.nanroSignpost
handle f TomTom = f R.tomTom D.tomTom
handle f Illumination = f R.illumination D.illumination
handle f Pentopia = f R.pentopia D.pentopia
handle f GreaterWall = f R.greaterWall D.greaterWall
handle f Galaxies = f R.galaxies D.galaxies
handle f Mines = f R.mines D.mines
handle f Tents = f R.tents D.tents
handle f PentominoSums = f R.pentominoSums D.pentominoSums
handle f CoralLITS = f R.coralLits D.coralLits
handle f CoralLITSO = f R.coralLitso D.coralLitso
handle f Snake = f R.snake D.snake
handle f CountryRoad = f R.countryRoad D.countryRoad
handle f KillerSudoku = f R.killersudoku D.killersudoku
handle f JapaneseSumsMasyu = f R.japsummasyu D.japsummasyu
handle f ArrowSudoku = f R.arrowsudoku D.arrowsudoku
handle f DualLoop = f R.dualloop D.dualloop
handle _ t = if isGeneric t
then impossible
else error $ "puzzle type unhandled: " ++ show t
handle f ABCtje = f R.abctje D.abctje
handle f Kropki = f R.kropki D.kropki
handle f StatuePark = f R.statuepark D.statuepark
handle f PentominousBorders = f R.pentominousBorders D.pentominousBorders
handle f NanroSignpost = f R.nanroSignpost D.nanroSignpost
handle f TomTom = f R.tomTom D.tomTom
handle f Illumination = f R.illumination D.illumination
handle f Pentopia = f R.pentopia D.pentopia
handle f GreaterWall = f R.greaterWall D.greaterWall
handle f Galaxies = f R.galaxies D.galaxies
handle f Mines = f R.mines D.mines
handle f Tents = f R.tents D.tents
handle f PentominoSums = f R.pentominoSums D.pentominoSums
handle f CoralLITS = f R.coralLits D.coralLits
handle f CoralLITSO = f R.coralLitso D.coralLitso
handle f Snake = f R.snake D.snake
handle f CountryRoad = f R.countryRoad D.countryRoad
handle f KillerSudoku = f R.killersudoku D.killersudoku
handle f JapaneseSumsMasyu = f R.japsummasyu D.japsummasyu
handle f ArrowSudoku = f R.arrowsudoku D.arrowsudoku
handle f DualLoop = f R.dualloop D.dualloop
handle _ t =
if isGeneric t
then impossible
else error $ "puzzle type unhandled: " ++ show t

-- | Handler that parses puzzle and an optional solution from a pair of
-- corresponding YAML values, and renders both individually, optionally
-- for the solution.
drawPuzzleMaybeSol
:: PuzzleHandler
b
((Value, Maybe Value) -> Parser (Drawing b, Maybe (Drawing b)))
drawPuzzleMaybeSol ::
PuzzleHandler
b
((Value, Maybe Value) -> Parser (Drawing b, Maybe (Drawing b)))
drawPuzzleMaybeSol (pp, ps) (Drawers dp ds) (p, s) = do
p' <- pp p
s' <- traverse ps s
let mps = case s' of
Nothing -> Nothing
Nothing -> Nothing
Just s'' -> Just (p', s'')
return (dp p', ds <$> mps)
Loading

0 comments on commit 6a269ca

Please sign in to comment.