Type-machine
4 days ago
- #Haskell
- #Structural Subtyping
- #Template Haskell
- Haskell's algebraic data types model data with constructors, but handling many fields can be cumbersome.
- Record syntax in GHC 7.4.1 allows naming fields, simplifying functions like getPath.
- Haskell's nominal type system lacks structural subtyping, making constraints on record fields verbose.
- TypeScript's structural subtyping inspired the type-machine library for Haskell.
- type-machine uses Template Haskell to derive record structures and simulate structural subtyping.
- The library features type-transformers like pick, omit, and intersection, similar to TypeScript's utility types.
- Infix operators <:> and <::> simplify chaining type-transformers and converting Template Haskell Names to Types.
- defineIs and deriveIs generate typeclasses for structural subtyping, with getters, setters, and conversion functions.
- Limitations include needing DuplicateRecordFields, handling ambiguity, and familiarity with Template Haskell.
- Example use-case shows deriving UserResponse and UserForm from UserRecord for web APIs.
- Microbenchmarks show type-machine outperforms extensible and superrecord in build and traversal times.
- Future work includes improving structural subtyping support with deriveConstraint.