instance-map: Template haskell utilities for helping with deserialization etc. of existential types

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

instance-map provides Template Haskell functions that help go from serialized values with value-level type witnesses (i.e. TypeRep values) to existential types containing type-level evidence of membership in a type class. It is useful for dealing with serialized values when only membership in a certain class (and not the monomorphic type) is known at the site of deserialization.


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0
Change log None available
Dependencies base (>=4.7 && <5), containers, mtl, template-haskell [details]
License BSD-3-Clause
Copyright 2018 Richard Warfield
Author Richard Warfield
Maintainer richard@litx.io
Category Dependent Types
Home page https://github.com/RichardWarfield/instance-map#readme
Source repo head: git clone https://github.com/RichardWarfield/instance-map
Uploaded by rwarfield at 2018-07-23T05:05:06Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for instance-map-0.1.0.0

[back to package description]

instance-map

instance-map provides Template Haskell functions that help go from serialized values with value-level type witnesses (i.e. TypeRep values) to existential types containing type-level evidence of membership in a type class. It is useful for dealing with serialized values when only membership in a certain class (and not the monomorphic type) is known at the site of deserialization.

Because Haskell is not a dependently typed language, there is ordinarily no way to "lift" information at the value level to the type level. This restriction makes dealing with serialized data for which the monomorphic type (or at least a list of the possible types) is not known at the site of use very difficult.

This library works by using Template Haskell to try to generate a map from TypeReps of the the monomorphic types belonging to a given type class to their decoder functions. It does this by recursively looking for instances of the given type class, then instances of any type class mentioned in the instance context, and so on. For example if we have

class Foo a
class Bar a
instance Foo a => Bar (Maybe a))

then mkMap will try to generate instances of Bar by looking for instances of Foo and replacing the a in the instance head.

Because the monomorphic members of a type class are potentially infinite in number (e.g. Maybe (Maybe (Maybe ...))), this library will only attempt to generate monomorphic types up to a given level of nesting (default 2).

Currently only univariate type classes are supported, and type synonyms are not supported. The library operates on a "best-efforts" basis, so if it encounters a multiparameter type class or other constraint it doesn't know how to satisfy, it will give up on that branch but continue to try other ways of finding the monomorphic type class members.

Example: Deserialization of JSON to value with evidence of type class membership

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}

import Type.InstanceMap

import Data.Aeson
import Data.Proxy
import Data.Typeable
import qualified Data.Binary as Binary
import Data.ByteString.Lazy hiding (putStrLn)

class (FromJSON a) => MyClass a where
  foo :: a -> String

instance MyClass Int where
  foo x = "Fooing Int " ++ show x

instance MyClass Bool where
  foo x = "Fooing Bool " ++ show x

-- Creates a function:
-- getSomeMyClass :: TypeRep -> Value -> Result (Some MyClass)
$(mkMap ''MyClass ''Value ''Result [|fromJSON|])

-- Pretend we have read the below values from the network or disk. 
-- There is usually no way to lift the TypeRep evidence to the value level.

mysteryValue :: Value
mysteryValue = toJSON (6 :: Int)

mysteryValueTypeData :: ByteString
mysteryValueTypeData = Binary.encode (typeRep (Proxy :: Proxy Int))

main = do
                  
  let someVal :: Some MyClass  -- Existential type with a single constructor, SomeMyClass
      someVal = getSomeMyClass (Binary.decode mysteryValueTypeData :: TypeRep) mysteryValue
  putStrLn $ case  of
    Error e -> "Whoops"
    Success (SomeMyClass v) -> foo v  -- Inside the pattern match I know v has a MyClass instance