From b5ee9f2c0546b9dad795ce69e424684a46cd58dd Mon Sep 17 00:00:00 2001 From: Wolfgang Witt Date: Tue, 16 Mar 2021 12:34:03 +0100 Subject: [PATCH] refactor: make ExamOccurrenceCapacity a newtype use pattern synonyms for convenience, so usage doesn't change --- src/Model/Types/Exam.hs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Model/Types/Exam.hs b/src/Model/Types/Exam.hs index b63f21159..509b01538 100644 --- a/src/Model/Types/Exam.hs +++ b/src/Model/Types/Exam.hs @@ -19,7 +19,7 @@ module Model.Types.Exam , _examOccurrenceMappingRule , _examOccurrenceMappingMapping , traverseExamOccurrenceMapping - , ExamOccurrenceCapacity(..) + , ExamOccurrenceCapacity(.., Unrestricted, Restricted) , _examOccurrenceCapacityIso , ExamGrade(..) , numberGrade @@ -231,15 +231,16 @@ traverseExamOccurrenceMapping = _examOccurrenceMappingMapping . iso Map.toList ( -- -- Maybe doesn't work, because the 'Ord' instance puts 'Nothing' below 0 -- instead of above every other number. -data ExamOccurrenceCapacity = Unrestricted | Restricted Natural - deriving (Show, Eq) +newtype ExamOccurrenceCapacity = EOCapacity (Maybe Natural) + deriving stock (Show) + deriving (Eq, Ord) via (NTop (Maybe Natural)) --- | Unrestricted is bigger then everything else, otherwise use instance from 'Natural'. -instance Ord ExamOccurrenceCapacity where - compare Unrestricted Unrestricted = EQ - compare Unrestricted (Restricted _n) = GT - compare (Restricted _n) Unrestricted = LT - compare (Restricted a) (Restricted b) = compare a b +pattern Unrestricted :: ExamOccurrenceCapacity +pattern Unrestricted = EOCapacity Nothing +pattern Restricted :: Natural -> ExamOccurrenceCapacity +pattern Restricted n = EOCapacity (Just n) + +{-# COMPLETE Unrestricted, Restricted #-} -- | Addition monoid with 'Unrestricted' interpreted as infinity. instance Semigroup ExamOccurrenceCapacity where