unwitch: converts between primitives

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

Removes the magic from witch. This provides safe conversions like witch does. But it doesn't use type classes or exceptions. This has a couple of advantages:

  1. No need to use type application for function selection.

  2. Functions get names that describe what they do. This allows ctags to work as well.

  3. No trouble with orphans.

  4. Custom errors instead of the prelude based ones allow client code to recover with typesafety even on partial conversions.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0, 2.0.0, 2.0.1, 2.0.2
Change log Changelog.md
Dependencies base (>=4.18.0.0 && <4.23), bytestring (>=0.10 && <0.13), ghc-bignum (<2), text (>=1.2 && <2.2) [details]
License MIT
Copyright 2026 Jappie Klooster
Author Jappie Klooster
Maintainer jappieklooster@hotmail.com
Uploaded by Jappie at 2026-03-09T18:58:03Z
Category Data
Home page https://github.com/jappeace/unwitch#readme
Bug tracker https://github.com/jappeace/unwitch/issues
Source repo head: git clone https://github.com/jappeace/unwitch
Distributions Stackage:2.0.2
Downloads 0 total (0 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 unwitch-2.0.2

[back to package description]

https://jappieklooster.nl Jappiejappie Jappiejappie

Three hundred and twenty years have passed since the coven sank in the dark.

Removes the magic from witch. This provides safe conversions like witch does. But it doesn't use type classes or exceptions. This has a couple of advantages:

  1. No need to use type application for function selection.
  2. Functions get names that describe what they do. This allows ctags to work as well.
  3. No trouble with orphans.
  4. Custom errors instead of the prelude based ones allow client code to recover with typesafety even on partial conversions.

Usage

The idea is to select some module from which type you want to convert. So if you have a double you do:

import qualified Unwitch.Convert.Double as Double

Next you simply use the functions within the module to convert to something you want, for example:

spec = do
  Double.toFloat 5.6 `shouldBe` 5.6

Although dubious since float holds less information then double, this can never fail in the current implementation. But there are situation where conversion can fail, such as when converting to an integer:

spec = do
  Double.toInteger 5.0 `shouldBe` Right 5
  Double.toInteger 5.6 `shouldBe` Left $ RationalConversion $ DenomNotOne (5 % 6)

In these situation you'll Right for success, or Left for an error. The library will attempt to give you as much information as possible, in this case the denominator wasn't 1 so the rational conversion failed.

Comparison

Witch

Type applications are unnecessary because there is no polymorphism. Since there are no polymorphic types we don't need to distinct between conversions that fail and conversions that don't, This is encoded in types. Furthermore on failure, if the client can figure out how to go on with a Rational, they can. In witch this information gets squashed, we happily provide it. It's not an exception like witch implied.

Hacking

Tools

Enter the nix shell.

nix-shell

You can checkout the makefile to see what's available:

cat makefile

Running

make run

Fast filewatch which runs tests

make ghcid

Design decisions

  • Modules names indicate where you're converting from.
  • Errors should be packed with information to allow recovery
  • Only restricted to primitives in base. The original witch had support for Map, and Text. I feel this is out of scope.

https://serokell.io/blog/introduction-to-template-haskell