on master help sign in
Add PureScript syntax test
4d6b2fesharkdp
tests/syntax-tests/highlighted/PureScript/test.pursnew file+89
@@ -0,0 +1,89 @@
1+-- | This module defines a datatype `Pair` together with a few useful instances
2+-- | and helper functions. Note that this is not just `Tuple a a` but rather a
3+-- | list with exactly two elements. Specifically, the `Functor` instance maps
4+-- | over both values (in contrast to the `Functor` instance for `Tuple a`).
5+module Data.Pair
6+ ( Pair(..)
7+ , (~)
8+ , fst
9+ , snd
10+ , curry
11+ , uncurry
12+ , swap
13+ ) where
14+
15+import Prelude
16+
17+import Data.Foldable (class Foldable)
18+import Data.Traversable (class Traversable)
19+import Data.Distributive (class Distributive)
20+
21+import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
22+
23+-- | A pair simply consists of two values of the same type.
24+data Pair a = Pair a a
25+
26+infixl 6 Pair as ~
27+
28+-- | Returns the first component of a pair.
29+fst ∷ ∀ a. Pair a → a
30+fst (x ~ _) = x
31+
32+-- | Returns the second component of a pair.
33+snd ∷ ∀ a. Pair a → a
34+snd (_ ~ y) = y
35+
36+-- | Turn a function that expects a pair into a function of two arguments.
37+curry ∷ ∀ a b. (Pair a → b) → a → a → b
38+curry f x y = f (x ~ y)
39+
40+-- | Turn a function of two arguments into a function that expects a pair.
41+uncurry ∷ ∀ a b. (a → a → b) → Pair a → b
42+uncurry f (x ~ y) = f x y
43+
44+-- | Exchange the two components of the pair
45+swap ∷ ∀ a. Pair a → Pair a
46+swap (x ~ y) = y ~ x
47+
48+derive instance eqPair ∷ Eq a ⇒ Eq (Pair a)
49+
50+derive instance ordPair ∷ Ord a ⇒ Ord (Pair a)
51+
52+instance showPair ∷ Show a ⇒ Show (Pair a) where
53+ show (x ~ y) = "(" <> show x <> " ~ " <> show y <> ")"
54+
55+instance functorPair ∷ Functor Pair where
56+ map f (x ~ y) = f x ~ f y
57+
58+instance applyPair ∷ Apply Pair where
59+ apply (f ~ g) (x ~ y) = f x ~ g y
60+
61+instance applicativePair ∷ Applicative Pair where
62+ pure x = x ~ x
63+
64+instance bindPair ∷ Bind Pair where
65+ bind (x ~ y) f = fst (f x) ~ snd (f y)
66+
67+instance monadPair ∷ Monad Pair
68+
69+instance semigroupPair ∷ Semigroup a ⇒ Semigroup (Pair a) where
70+ append (x1 ~ y1) (x2 ~ y2) = (x1 <> x2) ~ (y1 <> y2)
71+
72+instance monoidPair ∷ Monoid a ⇒ Monoid (Pair a) where
73+ mempty = mempty ~ mempty
74+
75+instance foldablePair ∷ Foldable Pair where
76+ foldr f z (Pair x y) = x `f` (y `f` z)
77+ foldl f z (Pair x y) = (z `f` x) `f` y
78+ foldMap f (Pair x y) = f x <> f y
79+
80+instance traversablePair ∷ Traversable Pair where
81+ traverse f (Pair x y) = Pair <$> f x <*> f y
82+ sequence (Pair mx my) = Pair <$> mx <*> my
83+
84+instance distributivePair ∷ Distributive Pair where
85+ distribute xs = map fst xs ~ map snd xs
86+ collect f xs = map (fst <<< f) xs ~ map (snd <<< f) xs
87+
88+instance arbitraryPair ∷ Arbitrary a ⇒ Arbitrary (Pair a) where
89+ arbitrary = Pair <$> arbitrary <*> arbitrary
tests/syntax-tests/source/PureScript/test.pursnew file+89
@@ -0,0 +1,89 @@
1+-- | This module defines a datatype `Pair` together with a few useful instances
2+-- | and helper functions. Note that this is not just `Tuple a a` but rather a
3+-- | list with exactly two elements. Specifically, the `Functor` instance maps
4+-- | over both values (in contrast to the `Functor` instance for `Tuple a`).
5+module Data.Pair
6+ ( Pair(..)
7+ , (~)
8+ , fst
9+ , snd
10+ , curry
11+ , uncurry
12+ , swap
13+ ) where
14+
15+import Prelude
16+
17+import Data.Foldable (class Foldable)
18+import Data.Traversable (class Traversable)
19+import Data.Distributive (class Distributive)
20+
21+import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
22+
23+-- | A pair simply consists of two values of the same type.
24+data Pair a = Pair a a
25+
26+infixl 6 Pair as ~
27+
28+-- | Returns the first component of a pair.
29+fst ∷ ∀ a. Pair a → a
30+fst (x ~ _) = x
31+
32+-- | Returns the second component of a pair.
33+snd ∷ ∀ a. Pair a → a
34+snd (_ ~ y) = y
35+
36+-- | Turn a function that expects a pair into a function of two arguments.
37+curry ∷ ∀ a b. (Pair a → b) → a → a → b
38+curry f x y = f (x ~ y)
39+
40+-- | Turn a function of two arguments into a function that expects a pair.
41+uncurry ∷ ∀ a b. (a → a → b) → Pair a → b
42+uncurry f (x ~ y) = f x y
43+
44+-- | Exchange the two components of the pair
45+swap ∷ ∀ a. Pair a → Pair a
46+swap (x ~ y) = y ~ x
47+
48+derive instance eqPair ∷ Eq a ⇒ Eq (Pair a)
49+
50+derive instance ordPair ∷ Ord a ⇒ Ord (Pair a)
51+
52+instance showPair ∷ Show a ⇒ Show (Pair a) where
53+ show (x ~ y) = "(" <> show x <> " ~ " <> show y <> ")"
54+
55+instance functorPair ∷ Functor Pair where
56+ map f (x ~ y) = f x ~ f y
57+
58+instance applyPair ∷ Apply Pair where
59+ apply (f ~ g) (x ~ y) = f x ~ g y
60+
61+instance applicativePair ∷ Applicative Pair where
62+ pure x = x ~ x
63+
64+instance bindPair ∷ Bind Pair where
65+ bind (x ~ y) f = fst (f x) ~ snd (f y)
66+
67+instance monadPair ∷ Monad Pair
68+
69+instance semigroupPair ∷ Semigroup a ⇒ Semigroup (Pair a) where
70+ append (x1 ~ y1) (x2 ~ y2) = (x1 <> x2) ~ (y1 <> y2)
71+
72+instance monoidPair ∷ Monoid a ⇒ Monoid (Pair a) where
73+ mempty = mempty ~ mempty
74+
75+instance foldablePair ∷ Foldable Pair where
76+ foldr f z (Pair x y) = x `f` (y `f` z)
77+ foldl f z (Pair x y) = (z `f` x) `f` y
78+ foldMap f (Pair x y) = f x <> f y
79+
80+instance traversablePair ∷ Traversable Pair where
81+ traverse f (Pair x y) = Pair <$> f x <*> f y
82+ sequence (Pair mx my) = Pair <$> mx <*> my
83+
84+instance distributivePair ∷ Distributive Pair where
85+ distribute xs = map fst xs ~ map snd xs
86+ collect f xs = map (fst <<< f) xs ~ map (snd <<< f) xs
87+
88+instance arbitraryPair ∷ Arbitrary a ⇒ Arbitrary (Pair a) where
89+ arbitrary = Pair <$> arbitrary <*> arbitrary