-
Notifications
You must be signed in to change notification settings - Fork 7
Total conversion functions' conventions #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,92 @@ | ||||||
# Status | ||||||
|
||||||
- [ ] Proposed 2024-05-21 | ||||||
|
||||||
# Context | ||||||
|
||||||
In `cardano-api` we have multiple functions performing conversions between one value of the type to the other, for example: | ||||||
|
||||||
```haskell | ||||||
fromShelleyDeltaLovelace :: L.DeltaCoin -> Lovelace -- 'from' at the beginning | ||||||
lovelaceToQuantity :: Lovelace -> Quantity -- 'to' in the middle | ||||||
toAlonzoScriptLanguage :: AnyPlutusScriptVersion -> Plutus.Language -- 'to' at the beginning | ||||||
convReferenceInputs :: TxInsReference build era -> Set Ledger.TxIn -- 'conv' at the beginning | ||||||
``` | ||||||
|
||||||
There are multiple naming conventions for the conversion functions which makes them hard to locate. | ||||||
Some conversion functions with lengthy names, are not very convenient to use. | ||||||
|
||||||
# Decision | ||||||
|
||||||
## Type classes | ||||||
|
||||||
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): | ||||||
```haskell | ||||||
class Inject t s where | ||||||
inject :: t -> s | ||||||
carbolymer marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
class Convert (f :: a -> Type) (g :: a -> Type) where | ||||||
convert :: forall a. f a -> g a | ||||||
``` | ||||||
|
||||||
The use of those conversion functions should be limited to **internal use only**. | ||||||
The library should still export conversion functions with explicit type names for better readability. | ||||||
carbolymer marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
An exception to this would be a set of types which are convertible one to the other, like `Eon`s. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I am guessing the |
||||||
Writing $N \times N$ conversion functions for $N$ types would be cumbersome and using `inject`/`convert` instead is justified here. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Because it is a consequence, so this would be more precise |
||||||
|
||||||
Inject instances should be placed near one of the types definition, to make them more discoverable and avoid orphaned instances. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think |
||||||
|
||||||
>[!NOTE] | ||||||
>The difference between `Inject` and `Convert` class is that `Convert` is better typed for types with `Type -> Type` kind. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! I was just asking myself this |
||||||
>In other words, when writing `instance Inject (Foo a) (Bar a)` the GHC's typechecker needs some help to understand the code using `inject`: | ||||||
>```haskell | ||||||
>let x = inject @_ @(Bar Bool) $ Foo True | ||||||
>``` | ||||||
>That is not needed for `convert`. | ||||||
|
||||||
### Injection law | ||||||
|
||||||
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`). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the conversion from and to in this example? |
||||||
The `inject` and `convert` implementations should both be injective: | ||||||
```math | ||||||
\forall_{x,x' \in X} \ \ inject(x) = inject(x') \implies x = x' | ||||||
``` | ||||||
|
||||||
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. | ||||||
|
||||||
## Explicit conversion functions | ||||||
|
||||||
For explicit conversion functions, the following naming convention should follow: | ||||||
|
||||||
```haskell | ||||||
fooToBar :: Foo -> Bar | ||||||
``` | ||||||
|
||||||
### Qualified imports | ||||||
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: | ||||||
|
||||||
```haskell | ||||||
module Data.Foo where | ||||||
|
||||||
fromBar :: Bar -> Foo | ||||||
``` | ||||||
|
||||||
where the usage would look like: | ||||||
```haskell | ||||||
import Data.Foo qualified as Foo | ||||||
|
||||||
Foo.fromBar bar | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||||||
``` | ||||||
|
||||||
# Consequences | ||||||
|
||||||
## Advantages | ||||||
- An uniform API for total conversions | ||||||
- A list of `Inject` instances lists all available conversions for the type | ||||||
- Less maintenance burden with regards to the naming conventions of the conversion functions | ||||||
|
||||||
## Disadvantages | ||||||
- 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. | ||||||
carbolymer marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? |
||||||
|
||||||
|
||||||
[modeline]: # ( vim: set spell spelllang=en: ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.