-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/Val.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |