Skip to content

Commit

Permalink
Introduce Val class from #1815
Browse files Browse the repository at this point in the history
This commit only adds the class and an instance for `Coin`. It differs
from #1815 in the following ways:

- Class methods are now intended for qualified import (as per Michael's
comment in #1803)
- Added some additional documentation. Some functions still need
significantly more documentation.
- Use the `Semigroup`, `Monoid` and `Group` superclasses. This removes
the need for various functions defined in this class.
- Removed the instance for `Integer` and define the instance for `Coin`
directly.
  • Loading branch information
nc6 committed Sep 2, 2020
1 parent dda669e commit 7b65281
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ library
Shelley.Spec.Ledger.Tx
Shelley.Spec.Ledger.TxData
Shelley.Spec.Ledger.UTxO
Shelley.Spec.Ledger.Val
other-modules: Shelley.Spec.Ledger.API.Mempool
Shelley.Spec.Ledger.API.Wallet
Shelley.Spec.Ledger.API.Types
Expand Down Expand Up @@ -105,6 +106,7 @@ library
cborg,
cborg-json,
containers,
groups,
iproute,
mtl,
network,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ where
import Cardano.Binary (DecoderError (..), FromCBOR (..), ToCBOR (..))
import Cardano.Prelude (NFData, NoUnexpectedThunks (..), cborError)
import Data.Aeson (FromJSON, ToJSON)
import Data.Group (Abelian, Group (..))
import Data.Monoid (Sum (..))
import Data.Text (pack)
import Data.Word (Word64)
import GHC.Generics (Generic)
Expand All @@ -34,6 +36,7 @@ newtype Coin = Coin {unCoin :: Integer}
NFData
)
deriving (Show) via Quiet Coin
deriving (Semigroup, Monoid, Group, Abelian) via Sum Integer

word64ToCoin :: Word64 -> Coin
word64ToCoin w = Coin $ fromIntegral w
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}

-- | This module defines a generalised notion of a "value" - that is, something
-- with which we may quantify a transaction output.
module Shelley.Spec.Ledger.Val where

import Cardano.Prelude (NFData (), NoUnexpectedThunks (..))
import Data.Group (Abelian)
import Data.Typeable (Typeable)
import Shelley.Spec.Ledger.Coin (Coin (..))

data Comparison = Gt | Lt | Gteq | Lteq | Neq | Equal

class
( Abelian t,
Eq t,
-- Do we really need these?
Show t,
Typeable t,
NFData t,
NoUnexpectedThunks t
) =>
Val t
where
-- | TODO This needs documenting. what is it?
scalev :: Integer -> t -> t

-- | Compare two values. Note that we only have a partial ordering; two values
-- may not necessarily be comparible, in which case `False` will be returned
-- for all comparisons.
compare :: Comparison -> t -> t -> Bool

-- | Is the argument zero?
isZero :: t -> Bool
isZero t = t == mempty

coin :: t -> Coin -- get the Coin amount
inject :: Coin -> t -- inject Coin into the Val instance
size :: t -> Integer -- compute size of Val instance
-- TODO add PACK/UNPACK stuff to this class

instance Val Coin where
scalev n (Coin x) = Coin $ n * x
compare Gt x y = x > y
compare Lt x y = x < y
compare Gteq x y = x >= y
compare Lteq x y = x <= y
compare Neq x y = not (x == y)
compare Equal x y = x == y
coin = id
inject = id
size _ = 1

0 comments on commit 7b65281

Please sign in to comment.