polytyped: Alternative to `Dynamic` with type guarantees

[ bsd3, data, library ] [ Propose Tags ] [ Report a vulnerability ]

With Dynamic you can store a value of any type in the same container, but you have to track manually what kind of types can possibly be inside. Poly carries this information in a type level list, so you can be sure that it's container contains a value of one of those types.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.17 && <4.22) [details]
Tested with ghc ==9.4.8 || ==9.6.7 || ==9.8.4 || ==9.10.2 || ==9.12.2
License BSD-3-Clause
Author Ondřej Janošík
Maintainer j.ondra14@gmail.com
Category Data
Home page https://github.com/zlondrej/polytyped
Bug tracker https://github.com/zlondrej/polytyped/issues
Uploaded by zlondrej at 2025-09-08T16:11:35Z
Distributions
Downloads 3 total (3 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for polytyped-0.1.0.0

[back to package description]

Polytyped - Alternative to Dynamic with type guarantees

With Dynamic you can store a value of any type in the same container, but you have to track manually what kind of types can possibly be inside.

import Data.Dynamic
import Data.MysteryBox (getSomeDynamic)

main :: IO ()
main = do
  let dyn = getSomeDynamic
  -- getSomeDynamic :: Dynamic
  putStrLn $ show ((fromDynamic dyn) :: a?) -- What type can this possibly be?

Poly carries this information in a type level list, so you can be sure that it contains a value of one of the listed types.

import Data.Polytyped
import Data.MysteryBox (getSomePoly)

main :: IO ()
main = do
  let poly = getSomePoly
  -- getSomePoly :: Poly '[Car, Bike, Goat, Money]
  putStrLn $ show ((fromPoly poly) :: Car) -- This is no longer a guesswork.

Poly supplies a similar function as custom sum-type without actually needing to define the type. However unlike sum-types, Poly doesn't allow specifying same type multiple times. Representing type such as SumOp is not possible.

data SumOp
  = Add Int
  | Sub Int

You will need to help the type system by using tagged types:


import Data.Tagged

data AddOp
data SubOp

-- Representation of `SumOp` sum-type with `Poly` and tagged types.
type MyTaggedPoly = Poly '[Tagged AddOp Int, Tagged SubOpInt]

or newtype wrappers:


newtype AddOp = Add Int
newtype SubOp = Sub Int

-- Representation of `SumOp` sum-type with `Poly` and newtypes.
type MyNewtypePoly = Poly '[AddOp, SubOp]