GithubHelp home page GithubHelp logo

inferinstance's Introduction

Library for solving of problem described in the article: "Fully-functional heterogeneous lists - HaskellWiki"

https://wiki.haskell.org/Fully-functional_heterogeneous_lists

Consider we have 2 classes and 2 types. And we will define heterogeneous list for different types. With "InferInstance" library we can infer to which class belongs the instance of the corresponding type. And also we can call functions defined in the corresponding class. See example:

-- Declarations.hs
module Declarations where

class Cls1 a where fn1 :: a -> a

class Cls2 a where fn2 :: a -> a

data A = A deriving Show
instance Cls1 A where fn1 x = x

data B = B deriving Show
instance Cls1 B where fn1 x = x
instance Cls2 B where fn2 x = x
-- Example.hs
{-# LANGUAGE TemplateHaskell #-}
import Data.InferInstance
import Declarations

instance InferInstanceBase A
instance InferInstanceBase B

testData :: [WrappedInstance InferInstanceBase]
testData = [WrappedInstance A, WrappedInstance B]

main = do
    print [w|w<-testData, isCls1 w]
    print [w|w<-testData, isCls2 w]
    print [show$ fn2 a|w<-testData, WrappedInstance a<-[asCls2 w]]
    where
        isCls1 w = $(wrappedInstanceOf ''Cls1) w
        isCls2 w = $(wrappedInstanceOf ''Cls2) w
        asCls2 w = $(asWrappedInstanceOf ''Cls2) w

-- output:
--[A,B]
--[B]
--["B"]

inferinstance's People

Stargazers

Xenia avatar Serhii Khoma avatar Daniel Kahlenberg avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.