|
| 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 | +``` |
| 14 | + |
| 15 | +There are multiple naming conventions for the conversion functions which makes them hard to locate. |
| 16 | +Some conversion functions with lengthy names, are not very convenient to use. |
| 17 | + |
| 18 | +# Decision |
| 19 | + |
| 20 | +## Type classes |
| 21 | + |
| 22 | +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): |
| 23 | +```haskell |
| 24 | +class Inject t s where |
| 25 | + inject :: t -> s |
| 26 | + |
| 27 | +class Convert (f :: a -> Type) (g :: a -> Type) where |
| 28 | + convert :: forall a. f a -> g a |
| 29 | +``` |
| 30 | + |
| 31 | +The use of those conversion functions is limited to **internal use only**. |
| 32 | +The library should still export conversion functions with explicit type names for better readability. |
| 33 | +Inject instances should be placed near one of the types definition, to make them more discoverable and avoid orphaned instances. |
| 34 | + |
| 35 | +>[!NOTE] |
| 36 | +>The difference between `Inject` and `Convert` class is that `Convert` is better typed for rank 1 types. |
| 37 | +>In other words, when writing `instance Inject (Foo a) (Bar a)` the GHC's typechecker needs some help to understand the code using `inject`: |
| 38 | +>```haskell |
| 39 | +>let x = inject @_ @(Bar Bool) $ Foo True |
| 40 | +>``` |
| 41 | +>That is not needed for `convert`. |
| 42 | + |
| 43 | +### Injection law |
| 44 | + |
| 45 | +The `Inject` and `Convert` classes are meant to be used for trivial conversions only and not for more complex types like collections (e.g. `Set a`). |
| 46 | +The `inject` and `convert` implementations should be injective: |
| 47 | +```math |
| 48 | +\forall_{x,x' \in X} \ \ inject(x) = inject(x') \implies x = x' |
| 49 | +``` |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +## Explicit conversion functions |
| 54 | + |
| 55 | +For explicit conversion functions, the following naming convention should follow: |
| 56 | + |
| 57 | +```haskell |
| 58 | +fooToBar :: Foo -> Bar |
| 59 | +``` |
| 60 | + |
| 61 | +### Qualified imports |
| 62 | +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: |
| 63 | + |
| 64 | +```haskell |
| 65 | +module Data.Foo where |
| 66 | + |
| 67 | +fromBar :: Bar -> Foo |
| 68 | +``` |
| 69 | + |
| 70 | +where the usage would look like: |
| 71 | +```haskell |
| 72 | +import Data.Foo qualified as Foo |
| 73 | + |
| 74 | +Foo.fromBar bar |
| 75 | +``` |
| 76 | + |
| 77 | +# Consequences |
| 78 | + |
| 79 | +## Advantages |
| 80 | +- An uniform API for total conversions |
| 81 | +- A list of `Inject` instances lists all available conversions for the type |
| 82 | +- Less maintenance burden with regards to the naming conventions of the conversion functions |
| 83 | + |
| 84 | +## Disadvantages |
| 85 | +- 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. |
| 86 | + |
| 87 | + |
| 88 | +[modeline]: # ( vim: set spell spelllang=en: ) |
0 commit comments