Skip to content

Commit c9e8e94

Browse files
committed
ADR: Total conversion functions conventions
1 parent 6b2d51f commit c9e8e94

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 should be limited to **internal use only**.
32+
The library should still export conversion functions with explicit type names for better readability.
33+
An exception to this would be a set of types which are convertible one to the other, like `Eon`s.
34+
Writing $N \times N$ conversion functions for $N$ types would be cumbersome and using `inject`/`convert` instead is justified here.
35+
36+
Inject instances should be placed near one of the types definition, to make them more discoverable and avoid orphaned instances.
37+
38+
>[!NOTE]
39+
>The difference between `Inject` and `Convert` class is that `Convert` is better typed for types with `Type -> Type` kind.
40+
>In other words, when writing `instance Inject (Foo a) (Bar a)` the GHC's typechecker needs some help to understand the code using `inject`:
41+
>```haskell
42+
>let x = inject @_ @(Bar Bool) $ Foo True
43+
>```
44+
>That is not needed for `convert`.
45+
46+
### Injection law
47+
48+
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`).
49+
The `inject` and `convert` implementations should both be injective:
50+
```math
51+
\forall_{x,x' \in X} \ \ inject(x) = inject(x') \implies x = x'
52+
```
53+
54+
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.
55+
56+
## Explicit conversion functions
57+
58+
For explicit conversion functions, the following naming convention should follow:
59+
60+
```haskell
61+
fooToBar :: Foo -> Bar
62+
```
63+
64+
### Qualified imports
65+
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:
66+
67+
```haskell
68+
module Data.Foo where
69+
70+
fromBar :: Bar -> Foo
71+
```
72+
73+
where the usage would look like:
74+
```haskell
75+
import Data.Foo qualified as Foo
76+
77+
Foo.fromBar bar
78+
```
79+
80+
# Consequences
81+
82+
## Advantages
83+
- An uniform API for total conversions
84+
- A list of `Inject` instances lists all available conversions for the type
85+
- Less maintenance burden with regards to the naming conventions of the conversion functions
86+
87+
## Disadvantages
88+
- 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.
89+
90+
91+
[modeline]: # ( vim: set spell spelllang=en: )

0 commit comments

Comments
 (0)