Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gelisam/hs-promote

C++-style type promotion for Haskell's numeric hierarchy
https://github.com/gelisam/hs-promote

Last synced: 22 days ago
JSON representation

C++-style type promotion for Haskell's numeric hierarchy

Awesome Lists containing this project

README

        

Type promotion in haskell
=========================

Like in Haskell, the arithmetic operations of C++ always act on identical types (ex: 2 + 2 or 2.0 + 2.0, but not 2 + 2.0).

Unlike Haskell, C++ automatically promotes the arguments of arithmetic operations to the most general type. Lest it be thought that C++ is superior to Haskell in this regard, here is an implementation of type promotion in Haskell.

Implementation
--------------

I use type families to compute the resulting type, e.g. Promote Int Double = Double because the result of 2 + 2.0 is a Double.

One downside of this implementation strategy is that we need to define a quadratic number of instances, one for each pair of compatible types. To avoid doing this, I use Template Haskell to generate all the instances.