|
| 1 | +# Status |
| 2 | + |
| 3 | +- [ ] Proposed 2024-05-21 |
| 4 | + |
| 5 | +# Context |
| 6 | + |
| 7 | +In `cardano-api` we have multiple functions performing conversions between one value of the type to the other, for example: |
| 8 | + |
| 9 | +```haskell |
| 10 | +fromShelleyDeltaLovelace :: L.DeltaCoin -> Lovelace -- 'from' at the beginning |
| 11 | +lovelaceToQuantity :: Lovelace -> Quantity -- 'to' in the middle |
| 12 | +toAlonzoScriptLanguage :: AnyPlutusScriptVersion -> Plutus.Language -- 'to' at the beginning |
| 13 | +convReferenceInputs :: TxInsReference build era -> Set Ledger.TxIn -- 'conv' at the beginning |
| 14 | +``` |
| 15 | + |
| 16 | +There are multiple naming conventions for the conversion functions which makes them hard to locate. |
| 17 | +Some conversion functions with lengthy names, are not very convenient to use. |
| 18 | + |
| 19 | +# Decision |
| 20 | + |
| 21 | +## Type classes |
| 22 | + |
| 23 | +For total functions, which are simply converting a value from one type to another, we can use type classes [`Inject` (from `cardano-ledger`)](https://cardano-ledger.cardano.intersectmbo.org/cardano-ledger-core/Cardano-Ledger-BaseTypes.html#t:Inject) & [`Convert`](https://cardano-api.cardano.intersectmbo.org/cardano-api/Cardano-Api-Internal-Eras.html#t:Convert): |
| 24 | +```haskell |
| 25 | +class Inject t s where |
| 26 | + inject :: t -> s |
| 27 | + |
| 28 | +class Convert (f :: a -> Type) (g :: a -> Type) where |
| 29 | + convert :: forall a. f a -> g a |
| 30 | +``` |
| 31 | + |
| 32 | +The use of those conversion functions should be limited to **internal use only**. |
| 33 | +The library should still export conversion functions with explicit type names for better readability. |
| 34 | +An exception to this would be a set of types which are convertible one to the other, like `Eon`s. |
| 35 | +Writing $N \times N$ conversion functions for $N$ types would be cumbersome and using `inject`/`convert` instead is justified here. |
| 36 | + |
| 37 | +Inject instances should be placed near one of the types definition, to make them more discoverable and avoid orphaned instances. |
| 38 | + |
| 39 | +>[!NOTE] |
| 40 | +>The difference between `Inject` and `Convert` class is that `Convert` is better typed for types with `Type -> Type` kind. |
| 41 | +>In other words, when writing `instance Inject (Foo a) (Bar a)` the GHC's typechecker needs some help to understand the code using `inject`: |
| 42 | +>```haskell |
| 43 | +>let x = inject @_ @(Bar Bool) $ Foo True |
| 44 | +>``` |
| 45 | +>That is not needed for `convert`. |
| 46 | + |
| 47 | +### Injection law |
| 48 | + |
| 49 | +The `Inject` and `Convert` classes are meant to be used for trivial conversions only and not for more complex types like polymorphic collections (e.g. `Set a`). |
| 50 | +The `inject` and `convert` implementations should both be injective: |
| 51 | +```math |
| 52 | +\forall_{x,x' \in X} \ \ inject(x) = inject(x') \implies x = x' |
| 53 | +``` |
| 54 | + |
| 55 | +This effectively means that any hashing functions or field accessors for constructors losing information (e.g. `foo (Foo _ a) = a`) should not be implemented as `Inject`/`Convert` instances. |
| 56 | + |
| 57 | +## Explicit conversion functions |
| 58 | + |
| 59 | +For explicit conversion functions, the following naming convention should follow: |
| 60 | + |
| 61 | +```haskell |
| 62 | +fooToBar :: Foo -> Bar |
| 63 | +``` |
| 64 | + |
| 65 | +### Qualified imports |
| 66 | +If the module exporting conversion functions is meant to be imported qualified, and provides functions for operating on a single data type, a shorter name with `from` prefix is allowed: |
| 67 | + |
| 68 | +```haskell |
| 69 | +module Data.Foo where |
| 70 | + |
| 71 | +fromBar :: Bar -> Foo |
| 72 | +``` |
| 73 | + |
| 74 | +where the usage would look like: |
| 75 | +```haskell |
| 76 | +import Data.Foo qualified as Foo |
| 77 | + |
| 78 | +Foo.fromBar bar |
| 79 | +``` |
| 80 | + |
| 81 | +# Consequences |
| 82 | + |
| 83 | +## Advantages |
| 84 | +- An uniform API for total conversions |
| 85 | +- A list of `Inject` instances lists all available conversions for the type |
| 86 | +- Less maintenance burden with regards to the naming conventions of the conversion functions |
| 87 | + |
| 88 | +## Disadvantages |
| 89 | +- It may be a bit surprising how to discover available conversions, because one would have to browse the type's `Inject` instances to find the conversion functions they are looking for - instead of looking for exported functions. |
| 90 | + |
| 91 | + |
| 92 | +[modeline]: # ( vim: set spell spelllang=en: ) |
0 commit comments