|
1 | | --- editorconfig-checker-disable-file |
2 | | - |
3 | | -module PlutusTx.Ord (Ord (..), Ordering (..)) where |
4 | | - |
5 | | -{- |
6 | | -We export off-chain Haskell's Ordering type as on-chain Plutus's Ordering type since they are the same. |
7 | | --} |
8 | | - |
9 | | -import PlutusTx.Bool (Bool (..)) |
10 | | -import PlutusTx.Builtins qualified as Builtins |
11 | | -import PlutusTx.Either (Either (..)) |
12 | | -import PlutusTx.Eq |
13 | | -import Prelude (Maybe (..), Ordering (..)) |
14 | | - |
15 | | -{- HLINT ignore -} |
16 | | - |
17 | | -infix 4 <, <=, >, >= |
18 | | - |
19 | | --- Copied from the GHC definition |
20 | | - |
21 | | -{-| The 'Ord' class is used for totally ordered datatypes. |
22 | | -
|
23 | | -Minimal complete definition: either 'compare' or '<='. |
24 | | -Using 'compare' can be more efficient for complex types. -} |
25 | | -class Eq a => Ord a where |
26 | | - compare :: a -> a -> Ordering |
27 | | - (<), (<=), (>), (>=) :: a -> a -> Bool |
28 | | - max, min :: a -> a -> a |
29 | | - |
30 | | - {-# INLINEABLE compare #-} |
31 | | - compare x y = |
32 | | - if x == y |
33 | | - then EQ |
34 | | - -- NB: must be '<=' not '<' to validate the |
35 | | - -- above claim about the minimal things that |
36 | | - -- can be defined for an instance of Ord: |
37 | | - else |
38 | | - if x <= y |
39 | | - then LT |
40 | | - else GT |
41 | | - |
42 | | - {-# INLINEABLE (<) #-} |
43 | | - x < y = case compare x y of LT -> True; _ -> False |
44 | | - {-# INLINEABLE (<=) #-} |
45 | | - x <= y = case compare x y of GT -> False; _ -> True |
46 | | - {-# INLINEABLE (>) #-} |
47 | | - x > y = case compare x y of GT -> True; _ -> False |
48 | | - {-# INLINEABLE (>=) #-} |
49 | | - x >= y = case compare x y of LT -> False; _ -> True |
50 | | - |
51 | | - -- These two default methods use '<=' rather than 'compare' |
52 | | - -- because the latter is often more expensive |
53 | | - {-# INLINEABLE max #-} |
54 | | - max x y = if x <= y then y else x |
55 | | - {-# INLINEABLE min #-} |
56 | | - min x y = if x <= y then x else y |
57 | | - {-# MINIMAL compare | (<=) #-} |
58 | | - |
59 | | -instance Ord Builtins.Integer where |
60 | | - {-# INLINEABLE (<) #-} |
61 | | - (<) = Builtins.lessThanInteger |
62 | | - {-# INLINEABLE (<=) #-} |
63 | | - (<=) = Builtins.lessThanEqualsInteger |
64 | | - {-# INLINEABLE (>) #-} |
65 | | - (>) = Builtins.greaterThanInteger |
66 | | - {-# INLINEABLE (>=) #-} |
67 | | - (>=) = Builtins.greaterThanEqualsInteger |
68 | | - |
69 | | -instance Ord Builtins.BuiltinByteString where |
70 | | - {-# INLINEABLE (<) #-} |
71 | | - (<) = Builtins.lessThanByteString |
72 | | - {-# INLINEABLE (<=) #-} |
73 | | - (<=) = Builtins.lessThanEqualsByteString |
74 | | - {-# INLINEABLE (>) #-} |
75 | | - (>) = Builtins.greaterThanByteString |
76 | | - {-# INLINEABLE (>=) #-} |
77 | | - (>=) = Builtins.greaterThanEqualsByteString |
78 | | - |
79 | | -instance Ord a => Ord [a] where |
80 | | - {-# INLINEABLE compare #-} |
81 | | - compare [] [] = EQ |
82 | | - compare [] (_ : _) = LT |
83 | | - compare (_ : _) [] = GT |
84 | | - compare (x : xs) (y : ys) = |
85 | | - case compare x y of |
86 | | - EQ -> compare xs ys |
87 | | - c -> c |
88 | | - |
89 | | -instance Ord Bool where |
90 | | - {-# INLINEABLE compare #-} |
91 | | - compare b1 b2 = case b1 of |
92 | | - False -> case b2 of |
93 | | - False -> EQ |
94 | | - True -> LT |
95 | | - True -> case b2 of |
96 | | - False -> GT |
97 | | - True -> EQ |
98 | | - |
99 | | -instance Ord a => Ord (Maybe a) where |
100 | | - {-# INLINEABLE compare #-} |
101 | | - compare (Just a1) (Just a2) = compare a1 a2 |
102 | | - compare Nothing (Just _) = LT |
103 | | - compare (Just _) Nothing = GT |
104 | | - compare Nothing Nothing = EQ |
105 | | - |
106 | | -instance (Ord a, Ord b) => Ord (Either a b) where |
107 | | - {-# INLINEABLE compare #-} |
108 | | - compare (Left a1) (Left a2) = compare a1 a2 |
109 | | - compare (Left _) (Right _) = LT |
110 | | - compare (Right _) (Left _) = GT |
111 | | - compare (Right b1) (Right b2) = compare b1 b2 |
112 | | - |
113 | | -instance Ord () where |
114 | | - {-# INLINEABLE compare #-} |
115 | | - compare _ _ = EQ |
116 | | - |
117 | | -instance (Ord a, Ord b) => Ord (a, b) where |
118 | | - {-# INLINEABLE compare #-} |
119 | | - compare (a, b) (a', b') = |
120 | | - case compare a a' of |
121 | | - EQ -> compare b b' |
122 | | - c -> c |
| 1 | +{-# LANGUAGE TemplateHaskell #-} |
| 2 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 3 | +module PlutusTx.Ord (Ord (..), Ordering (..), deriveOrd) where |
| 4 | + |
| 5 | +import PlutusTx.Bool |
| 6 | +import PlutusTx.Either |
| 7 | +import PlutusTx.Ord.Class |
| 8 | +import PlutusTx.Ord.TH |
| 9 | +import Prelude (Maybe (..)) |
| 10 | + |
| 11 | +deriveOrd ''[] |
| 12 | +deriveOrd ''Bool |
| 13 | +deriveOrd ''Maybe |
| 14 | +deriveOrd ''Either |
| 15 | +deriveOrd ''Ordering |
| 16 | +deriveOrd ''() |
| 17 | +deriveOrd ''(,) |
| 18 | +deriveOrd ''(,,) |
| 19 | +deriveOrd ''(,,,) |
| 20 | +deriveOrd ''(,,,,) |
| 21 | +deriveOrd ''(,,,,,) |
| 22 | +deriveOrd ''(,,,,,,) |
| 23 | +deriveOrd ''(,,,,,,,) |
| 24 | +deriveOrd ''(,,,,,,,,) |
| 25 | +deriveOrd ''(,,,,,,,,,) |
| 26 | +deriveOrd ''(,,,,,,,,,,) |
| 27 | +deriveOrd ''(,,,,,,,,,,,) |
| 28 | +deriveOrd ''(,,,,,,,,,,,,) |
| 29 | +deriveOrd ''(,,,,,,,,,,,,,) |
| 30 | +deriveOrd ''(,,,,,,,,,,,,,,) |
| 31 | +deriveOrd ''(,,,,,,,,,,,,,,,) |
| 32 | +deriveOrd ''(,,,,,,,,,,,,,,,,) |
| 33 | +deriveOrd ''(,,,,,,,,,,,,,,,,,) |
| 34 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,) |
| 35 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,) |
| 36 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,,) |
| 37 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,,,) |
| 38 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,,,,) |
| 39 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,,,,,) |
| 40 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,,,,,,) |
| 41 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,,,,,,,) |
| 42 | +deriveOrd ''(,,,,,,,,,,,,,,,,,,,,,,,,,,) |
0 commit comments