From acd3dec41f5dcd12077e635a40262b6defe83095 Mon Sep 17 00:00:00 2001 From: Anupam Jain Date: Wed, 27 Nov 2024 14:27:53 +0530 Subject: [PATCH] Fix warnings (except the one about non exported data constructors - https://github.com/purescript-contrib/purescript-uint/issues/13). Add Eq, Ord, and Show instances. --- src/Data/Tuple/Native.purs | 72 +++++++++++++++++++++++++++++++++++--- test/Main.purs | 19 +++++----- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/Data/Tuple/Native.purs b/src/Data/Tuple/Native.purs index 21b5312..f687e0a 100644 --- a/src/Data/Tuple/Native.purs +++ b/src/Data/Tuple/Native.purs @@ -11,8 +11,14 @@ module Data.Tuple.Native , class TupleSize, class ShowNat ) where +import Data.Eq (class Eq) +import Data.Eq.Generic (genericEq) import Data.Function.Uncurried (Fn2, Fn3, Fn4, Fn5, Fn6, Fn7, Fn8, Fn9, runFn2, runFn3, runFn4, runFn5, runFn6, runFn7, runFn8, runFn9) import Data.Generic.Rep (class Generic, Constructor(..), Argument(..), Product(..)) +import Data.Ord (class Ord) +import Data.Ord.Generic (genericCompare) +import Data.Show (class Show) +import Data.Show.Generic (genericShow) import Data.Tuple as DT import Data.Tuple.Nested as DTN import Data.Typelevel.Num (D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, class Lt, class Nat, toInt, d0, d1, d2, d3, d4, d5, d6, d7, d8) @@ -63,11 +69,9 @@ prj :: forall t t' t'' n n' a size => n -> TupleN t -> a prj n t = runFn2 prjImpl (toInt n) t - - -- | Represented as a heterogeneous array under the hood -foreign import data TupleN :: RowList Type -> Type - +data TupleN :: RowList Type -> Type +data TupleN row type T2 a b = TupleN (Cons "0" a (Cons "1" b Nil)) @@ -86,6 +90,62 @@ type T8 a b c d e f g h = type T9 a b c d e f g h i = TupleN (Cons "0" a (Cons "1" b (Cons "2" c (Cons "3" d (Cons "4" e (Cons "5" f (Cons "6" g (Cons "7" h (Cons "8" i Nil))))))))) +instance (Eq a, Eq b, Eq c) => Eq (T3 a b c) where + eq = genericEq +else instance (Eq a, Eq b) => Eq (T2 a b) where + eq = genericEq +else instance (Eq a, Eq b, Eq c) => Eq (T3 a b c) where + eq = genericEq +else instance (Eq a, Eq b, Eq c, Eq d) => Eq (T4 a b c d) where + eq = genericEq +else instance (Eq a, Eq b, Eq c, Eq d, Eq e) => Eq (T5 a b c d e) where + eq = genericEq +else instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f) => Eq (T6 a b c d e f) where + eq = genericEq +else instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g) => Eq (T7 a b c d e f g) where + eq = genericEq +else instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h) => Eq (T8 a b c d e f g h) where + eq = genericEq +else instance (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, Eq i) => Eq (T9 a b c d e f g h i) where + eq = genericEq + +instance (Show a, Show b, Show c) => Show (T3 a b c) where + show = genericShow +else instance (Show a, Show b) => Show (T2 a b) where + show = genericShow +else instance (Show a, Show b, Show c) => Show (T3 a b c) where + show = genericShow +else instance (Show a, Show b, Show c, Show d) => Show (T4 a b c d) where + show = genericShow +else instance (Show a, Show b, Show c, Show d, Show e) => Show (T5 a b c d e) where + show = genericShow +else instance (Show a, Show b, Show c, Show d, Show e, Show f) => Show (T6 a b c d e f) where + show = genericShow +else instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g) => Show (T7 a b c d e f g) where + show = genericShow +else instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h) => Show (T8 a b c d e f g h) where + show = genericShow +else instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i) => Show (T9 a b c d e f g h i) where + show = genericShow + +instance (Ord a, Ord b, Ord c) => Ord (T3 a b c) where + compare = genericCompare +else instance (Ord a, Ord b) => Ord (T2 a b) where + compare = genericCompare +else instance (Ord a, Ord b, Ord c) => Ord (T3 a b c) where + compare = genericCompare +else instance (Ord a, Ord b, Ord c, Ord d) => Ord (T4 a b c d) where + compare = genericCompare +else instance (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (T5 a b c d e) where + compare = genericCompare +else instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f) => Ord (T6 a b c d e f) where + compare = genericCompare +else instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g) => Ord (T7 a b c d e f g) where + compare = genericCompare +else instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h) => Ord (T8 a b c d e f g h) where + compare = genericCompare +else instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h, Ord i) => Ord (T9 a b c d e f g h i) where + compare = genericCompare t2 :: forall a b . a -> b -> T2 a b t2 = runFn2 t2_ @@ -113,6 +173,7 @@ foreign import t7_ :: forall a b c d e f g . Fn7 a b c d e f g (T7 a b c d e foreign import t8_ :: forall a b c d e f g h . Fn8 a b c d e f g h (T8 a b c d e f g h) foreign import t9_ :: forall a b c d e f g h i. Fn9 a b c d e f g h i (T9 a b c d e f g h i) +class TupleSize :: forall k. k -> RowList Type -> Constraint class TupleSize n (t :: RowList Type) | t -> n instance tupleSizeT2 :: TupleSize D2 (Cons "0" a (Cons "1" b Nil)) @@ -124,7 +185,6 @@ instance tupleSizeT7 :: TupleSize D7 (Cons "0" a (Cons "1" b (Cons "2" c (Cons " instance tupleSizeT8 :: TupleSize D8 (Cons "0" a (Cons "1" b (Cons "2" c (Cons "3" d (Cons "4" e (Cons "5" f (Cons "6" g (Cons "7" h Nil)))))))) instance tupleSizeT9 :: TupleSize D9 (Cons "0" a (Cons "1" b (Cons "2" c (Cons "3" d (Cons "4" e (Cons "5" f (Cons "6" g (Cons "7" h (Cons "8" i Nil))))))))) - instance genericTuple2 :: Generic (TupleN (Cons "0" a (Cons "1" b Nil))) (Constructor "t2" (Product (Argument a) (Argument b))) where @@ -306,6 +366,7 @@ instance genericTuple9 :: Generic (Argument (prj d8 xs)) +class ShowNat :: forall k. k -> Symbol -> Constraint class ShowNat n (s :: Symbol) | n -> s, s -> n instance showNatD0 :: ShowNat D0 "0" @@ -317,3 +378,4 @@ instance showNatD5 :: ShowNat D5 "5" instance showNatD6 :: ShowNat D6 "6" instance showNatD7 :: ShowNat D7 "7" instance showNatD8 :: ShowNat D8 "8" + diff --git a/test/Main.purs b/test/Main.purs index 9a63ac0..dbdd060 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,13 +1,13 @@ module Test.Main where -import Data.Tuple.Native (T2, T3, prj, t3, xt, xt3) +import Data.Ord ((>)) import Data.Tuple (Tuple) -import Data.Tuple.Nested (Tuple3) +import Data.Tuple.Native (T2, T4, prj, t3, t4, xt, xt4) +import Data.Tuple.Nested (Tuple4) import Data.Typelevel.Num (d0, d1, d2) - -import Prelude (Unit, discard, ($)) import Effect (Effect) import Effect.Console (logShow) +import Prelude (Unit, discard, ($)) foreign import lenTupleImpl :: String -> T2 String Int @@ -16,18 +16,21 @@ lenTuple s = xt $ lenTupleImpl s main :: Effect Unit main = do - let x :: T3 Int Int Int - x = t3 1 2 3 + let x :: T4 Int Int Int Int + x = t4 1 2 3 4 + logShow x + logShow $ t3 4 2 1 > t3 2 2 3 logShow $ prj d0 x logShow $ prj d1 x logShow $ prj d2 x - let t :: Tuple3 Int Int Int - t = xt3 x + let t :: Tuple4 Int Int Int Int + t = xt4 x logShow $ t -- test foreign import let l :: Tuple String Int l = lenTuple "Testing" logShow l +