Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 31bbcfc

Browse files
committed
Tidy up Eq/Ord constraints, add some missing Eq1/Ord1
1 parent f8a0b98 commit 31bbcfc

File tree

5 files changed

+54
-71
lines changed

5 files changed

+54
-71
lines changed

src/Text/Markdown/SlamDown/Syntax.purs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@ module Text.Markdown.SlamDown.Syntax
99

1010
import Prelude
1111

12+
import Data.Eq (class Eq1)
1213
import Data.List as L
14+
import Data.Ord (class Ord1)
1315
import Test.StrongCheck.Arbitrary as SCA
1416
import Test.StrongCheck.Gen as Gen
15-
1617
import Text.Markdown.SlamDown.Syntax.Block (Block(..), CodeBlockType(..), ListType(..)) as SDB
1718
import Text.Markdown.SlamDown.Syntax.FormField (class Value, Expr(..), FormField, FormFieldP(..), TextBox(..), TimePrecision(..), getLiteral, getUnevaluated, renderValue, stringValue, transFormField, transTextBox, traverseFormField, traverseTextBox) as SDF
1819
import Text.Markdown.SlamDown.Syntax.Inline (Inline(..), LinkTarget(..)) as SDI
1920

2021
-- | `SlamDownP` is the type of SlamDown abstract syntax trees which take values in `a`.
21-
data SlamDownP a = SlamDown (L.List (SDB.Block a))
22+
newtype SlamDownP a = SlamDown (L.List (SDB.Block a))
2223

2324
type SlamDown = SlamDownP String
2425

25-
instance functorSlamDownPFunctor SlamDownP where
26-
map f (SlamDown bs) = SlamDown (map f <$> bs)
26+
derive instance functorSlamDownPFunctor SlamDownP
2727

2828
instance showSlamDownP ∷ (Show a) Show (SlamDownP a) where
2929
show (SlamDown bs) = "(SlamDown " <> show bs <> ")"
3030

31-
derive instance eqSlamDownP(Eq a, Ord a) Eq (SlamDownP a)
32-
derive instance ordSlamDownP(Eq a, Ord a) Ord (SlamDownP a)
31+
derive newtype instance eqSlamDownPEq a Eq (SlamDownP a)
32+
derive instance eq1SlamDownPEq1 SlamDownP
3333

34-
instance semigroupSlamDownPSemigroup (SlamDownP a) where
35-
append (SlamDown bs1) (SlamDown bs2) = SlamDown (bs1 <> bs2)
34+
derive newtype instance ordSlamDownPOrd a Ord (SlamDownP a)
35+
derive instance ord1SlamDownPOrd1 SlamDownP
3636

37-
instance monoidSlamDownPMonoid (SlamDownP a) where
38-
mempty = SlamDown mempty
37+
derive newtype instance semigroupSlamDownPSemigroup (SlamDownP a)
38+
derive newtype instance monoidSlamDownPMonoid (SlamDownP a)
3939

4040
instance arbitrarySlamDownP ∷ (SCA.Arbitrary a, Eq a) SCA.Arbitrary (SlamDownP a) where
4141
arbitrary = SlamDown <<< L.fromFoldable <$> Gen.arrayOf SCA.arbitrary

src/Text/Markdown/SlamDown/Syntax/Block.purs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ module Text.Markdown.SlamDown.Syntax.Block
66

77
import Prelude
88

9+
import Data.Eq (class Eq1)
910
import Data.List as L
10-
11+
import Data.Ord (class Ord1)
1112
import Test.StrongCheck.Arbitrary as SCA
1213
import Test.StrongCheck.Gen as Gen
13-
1414
import Text.Markdown.SlamDown.Syntax.Inline (Inline)
1515

1616
data Block a
@@ -22,18 +22,9 @@ data Block a
2222
| LinkReference String String
2323
| Rule
2424

25-
instance functorBlockFunctor Block where
26-
map f x =
27-
case x of
28-
Paragraph is → Paragraph (map f <$> is)
29-
Header n is → Header n (map f <$> is)
30-
Blockquote bs → Blockquote (map f <$> bs)
31-
Lst ty bss → Lst ty (map (map f) <$> bss)
32-
CodeBlock ty ss → CodeBlock ty ss
33-
LinkReference l uri → LinkReference l uri
34-
RuleRule
35-
36-
instance showBlock ∷ (Show a) Show (Block a) where
25+
derive instance functorBlockFunctor Block
26+
27+
instance showBlockShow a Show (Block a) where
3728
show (Paragraph is) = "(Paragraph " <> show is <> ")"
3829
show (Header n is) = "(Header " <> show n <> " " <> show is <> ")"
3930
show (Blockquote bs) = "(Blockquote " <> show bs <> ")"
@@ -42,8 +33,10 @@ instance showBlock ∷ (Show a) ⇒ Show (Block a) where
4233
show (LinkReference l uri) = "(LinkReference " <> show l <> " " <> show uri <> ")"
4334
show Rule = "Rule"
4435

45-
derive instance eqBlock ∷ (Eq a, Ord a) Eq (Block a)
46-
derive instance ordBlock ∷ (Ord a) Ord (Block a)
36+
derive instance eqBlockEq a Eq (Block a)
37+
derive instance eq1BlockEq1 Block
38+
derive instance ordBlockOrd a Ord (Block a)
39+
derive instance ord1BlockOrd1 Block
4740

4841
-- | Nota bene: this does not generate any recursive structure
4942
instance arbitraryBlock ∷ (SCA.Arbitrary a, Eq a) SCA.Arbitrary (Block a) where

src/Text/Markdown/SlamDown/Syntax/FormField.purs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,18 @@ module Text.Markdown.SlamDown.Syntax.FormField
1515
import Prelude
1616

1717
import Data.Array as A
18-
import Data.Eq (class Eq1)
18+
import Data.Eq (class Eq1, eq1)
1919
import Data.Functor.Compose (Compose(..))
2020
import Data.Identity (Identity(..))
2121
import Data.List as L
2222
import Data.Maybe as M
2323
import Data.Newtype (unwrap)
24-
import Data.Ord (class Ord1)
25-
import Data.Set as Set
24+
import Data.Ord (class Ord1, compare1)
2625
import Data.Traversable as TR
2726
import Data.Tuple (uncurry)
28-
2927
import Partial.Unsafe (unsafePartial)
30-
3128
import Test.StrongCheck.Arbitrary as SCA
3229
import Test.StrongCheck.Gen as Gen
33-
3430
import Text.Markdown.SlamDown.Syntax.TextBox (TextBox(..), TimePrecision(..), transTextBox, traverseTextBox) as TB
3531
import Text.Markdown.SlamDown.Syntax.Value (class Value, renderValue, stringValue) as Value
3632

@@ -80,31 +76,39 @@ instance showFormField ∷ (Functor f, Show (f a), Show (TB.TextBox (Compose M.M
8076
CheckBoxes sel ls → "(CheckBoxes " <> show sel <> " " <> show ls <> ")"
8177
DropDown sel ls → "(DropDown " <> show sel <> " " <> show ls <> ")"
8278

83-
instance ordFormField ∷ (Functor f, Ord (f a), Ord (TB.TextBox (Compose M.Maybe f)), Eq (TB.TextBox (Compose M.Maybe f)), Ord (f (L.List a)), Ord (f (Set.Set a)), Ord a) Ord (FormFieldP f a) where
84-
compare =
79+
instance eq1FormFieldEq1 f Eq1 (FormFieldP f) where
80+
eq1 = case _, _ of
81+
TextBox tb1, TextBox tb2 -> tb1 == tb2
82+
RadioButtons sel1 ls1, RadioButtons sel2 ls2 -> sel1 `eq1` sel2 && ls1 `eq1` ls2
83+
CheckBoxes sel1 ls1, CheckBoxes sel2 ls2 -> sel1 `eq1` sel2 && ls1 `eq1` ls2
84+
DropDown M.Nothing ls1, DropDown M.Nothing ls2 -> ls1 `eq1` ls2
85+
DropDown (M.Just sel1) ls1, DropDown (M.Just sel2) ls2 -> sel1 `eq1` sel2 && ls1 `eq1` ls2
86+
_, _ -> false
87+
88+
instance eqFormField :: (Eq1 f, Eq a) => Eq (FormFieldP f a) where
89+
eq = eq1
90+
91+
instance ord1FormFieldOrd1 f Ord1 (FormFieldP f) where
92+
compare1 =
8593
case _, _ of
8694
TextBox tb1, TextBox tb2 → compare tb1 tb2
8795
TextBox _, _ → LT
8896
_, TextBox _ → GT
8997

90-
RadioButtons sel1 ls1, RadioButtons sel2 ls2 → compare sel1 sel2 <> compare ls1 ls2
98+
RadioButtons sel1 ls1, RadioButtons sel2 ls2 → compare1 sel1 sel2 <> compare1 ls1 ls2
9199
RadioButtons _ _, _ → LT
92100
_, RadioButtons _ _ → GT
93101

94-
CheckBoxes sel1 ls1, CheckBoxes sel2 ls2 → compare (Set.fromFoldable <$> sel1) (Set.fromFoldable <$> sel2) <> compare ls1 ls2
102+
CheckBoxes sel1 ls1, CheckBoxes sel2 ls2 → compare1 sel1 sel2 <> compare1 ls1 ls2
95103
CheckBoxes _ _, _ → LT
96104
_, CheckBoxes _ _ → GT
97105

98-
DropDown sel1 ls1, DropDown sel2 ls2 → compare sel1 sel2 <> compare ls1 ls2
106+
DropDown M.Nothing ls1, DropDown M.Nothing ls2 → compare1 ls1 ls2
107+
DropDown (M.Just sel1) ls1, DropDown (M.Just sel2) ls2 → compare1 sel1 sel2 <> compare1 ls1 ls2
108+
_, _ -> EQ
99109

100-
instance eqFormField ∷ (Functor f, Eq (f a), Eq (TB.TextBox (Compose M.Maybe f)), Eq (f (L.List a)), Eq (f (Set.Set a)), Ord a) Eq (FormFieldP f a) where
101-
eq =
102-
case _, _ of
103-
TextBox tb1, TextBox tb2 → tb1 == tb2
104-
RadioButtons sel1 ls1, RadioButtons sel2 ls2 → sel1 == sel2 && ls1 == ls2
105-
CheckBoxes sel1 ls1, CheckBoxes sel2 ls2 → ((Set.fromFoldable <$> sel1) == (Set.fromFoldable <$> sel2)) && ls1 == ls2
106-
DropDown sel1 ls1, DropDown sel2 ls2 → sel1 == sel2 && ls1 == ls2
107-
_, _ → false
110+
instance ordFormField :: (Ord1 f, Ord a) => Ord (FormFieldP f a) where
111+
compare = compare1
108112

109113
newtype ArbIdentity a = ArbIdentity a
110114

@@ -302,13 +306,10 @@ instance showExpr ∷ (Show a) ⇒ Show (Expr a) where
302306
Unevaluated e → "(Unevaluated " <> show e <> ")"
303307

304308
derive instance eqExprEq a Eq (Expr a)
305-
derive instance ordExprOrd a Ord (Expr a)
309+
derive instance eq1Eq1 Expr
306310

307-
instance eq1Eq1 Expr where
308-
eq1 = eq
309-
310-
instance ord1ExprOrd1 Expr where
311-
compare1 = compare
311+
derive instance ord1ExprOrd1 Expr
312+
derive instance ordExprOrd a Ord (Expr a)
312313

313314
genExpr a. Gen.Gen a Gen.Gen (Expr a)
314315
genExpr g = do

src/Text/Markdown/SlamDown/Syntax/Inline.purs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ module Text.Markdown.SlamDown.Syntax.Inline
55

66
import Prelude
77

8+
import Data.Eq (class Eq1)
89
import Data.List as L
910
import Data.Maybe as M
10-
11+
import Data.Ord (class Ord1)
1112
import Test.StrongCheck.Arbitrary as SCA
1213
import Test.StrongCheck.Gen as Gen
13-
1414
import Text.Markdown.SlamDown.Syntax.FormField (FormField)
1515

1616
data Inline a
@@ -26,20 +26,7 @@ data Inline a
2626
| Image (L.List (Inline a)) String
2727
| FormField String Boolean (FormField a)
2828

29-
instance functorInlineFunctor Inline where
30-
map f =
31-
case _ of
32-
Str s → Str s
33-
Entity s → Entity s
34-
SpaceSpace
35-
SoftBreakSoftBreak
36-
LineBreakLineBreak
37-
Emph is → Emph (map f <$> is)
38-
Strong is → Strong (map f <$> is)
39-
Code b s → Code b s
40-
Link is tgt → Link (map f <$> is) tgt
41-
Image is tgt → Image (map f <$> is) tgt
42-
FormField str b ff → FormField str b (f <$> ff)
29+
derive instance functorInlineFunctor Inline
4330

4431
instance showInline ∷ (Show a) Show (Inline a) where
4532
show (Str s) = "(Str " <> show s <> ")"
@@ -54,8 +41,10 @@ instance showInline ∷ (Show a) ⇒ Show (Inline a) where
5441
show (Image is uri) = "(Image " <> show is <> " " <> show uri <> ")"
5542
show (FormField l r f) = "(FormField " <> show l <> " " <> show r <> " " <> show f <> ")"
5643

57-
derive instance eqInline ∷ (Eq a, Ord a) Eq (Inline a)
44+
derive instance eqInlineEq a Eq (Inline a)
45+
derive instance eq1InlineEq1 Inline
5846
derive instance ordInlineOrd a Ord (Inline a)
47+
derive instance ord1InlineOrd1 Inline
5948

6049
-- | Nota bene: this does not generate any recursive structure
6150
instance arbitraryInline ∷ (SCA.Arbitrary a, Eq a) SCA.Arbitrary (Inline a) where

src/Text/Markdown/SlamDown/Syntax/TextBox.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ traverseTextBox eta = case _ of
6262
Time prec def → Time prec <$> eta def
6363
DateTime prec def → DateTime prec <$> eta def
6464

65-
instance showTextBox ∷ (Functor f, Show (f String), Show (f HN.HugeNum), Show (f DT.Time), Show (f DT.Date), Show (f DT.DateTime)) Show (TextBox f) where
65+
instance showTextBox ∷ (Show (f String), Show (f HN.HugeNum), Show (f DT.Time), Show (f DT.Date), Show (f DT.DateTime)) Show (TextBox f) where
6666
show = case _ of
6767
PlainText def → "(PlainText " <> show def <> ")"
6868
Numeric def → "(Numeric " <> show def <> ")"
6969
Date def → "(Date " <> show def <> ")"
7070
Time prec def → "(Time " <> show prec <> " " <> show def <> ")"
7171
DateTime prec def → "(DateTime " <> show prec <> " " <> show def <> ")"
7272

73-
derive instance eqTextBox(Functor f, Eq1 f) Eq (TextBox f)
74-
derive instance ordTextBox(Functor f, Ord1 f) Ord (TextBox f)
73+
derive instance eqTextBoxEq1 f Eq (TextBox f)
74+
derive instance ordTextBoxOrd1 f Ord (TextBox f)
7575

7676
instance arbitraryTextBox ∷ (Functor f, SCA.Arbitrary (f String), SCA.Arbitrary (f Number), SCA.Arbitrary (f ADT.ArbTime), SCA.Arbitrary (f ADT.ArbDate), SCA.Arbitrary (f ADT.ArbDateTime)) SCA.Arbitrary (TextBox f) where
7777
arbitrary = do

0 commit comments

Comments
 (0)