Skip to content

Improvements to `magicDict`

Motivation

We'd like to clean up and generalize the machinery related to magicDict. The idea for that started on #16586 (closed), have a look there for context.

Proposal

The idea is due to @simonpj from #16586 (closed):

Actually I think we can simplify magicDict yet more:

magicDict :: (dt => r) -> st -> r

where

dt should be a single-method type class whose payload is st; so that there is a (representational) cast from st to dt.

st should be instantiated with only a singleton type. This is to avoid the possiblity of having many value of type dt in scope, but with different and incoherent values

Now the rewrite rule is

  magicDict @dt @st @r k sv  ===>   k (sv |> co)

where co is a newtype coercion that converts type st to constraint dt. This coercion should always be possible (by point (1) above); if not, the rule does not fire. No Wrap needed, at all, I think! But invocations of magicDict would need a type application to instantiate dt.

Edited by Ryan Scott
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information