8000 GitHub - liamzee/objective-lz: Purely functional objects, attempt to hack in STM
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

liamzee/objective-lz

 
 

Repository files navigation

objective

Hackage Build Status

Paper: https://fumieval.github.io/papers/en/2015-Haskell-objects.html

This package provides composable objects and instances.

Introduction

The primal construct, Object, models object-oriented objects. Object f g represents an object.

newtype Object f g = Object { runObject :: forall x. f x -> g (x, Object f g) }

An object interprets a message f a and returns the result a and the next object Object f g, on g.

data Counter a where
  Increment :: Counter ()
  Print :: Counter Int

counter :: Int -> Object Counter IO
counter n = Object $ \case
  Increment -> return ((), counter (n + 1))
  Print -> print n >> return (n, counter n)

new :: Object f g -> IO (Instance f g) creates an instance of an object.

(.-) :: (MonadIO m, MonadMask m) => Instance f m -> f a -> m a sends a message to an instance. This can be used to handle instances in the typical OOP fashion.

> i <- new (counter 0)
> i .- Increment
> i .- Print
1
> i .- Increment
> i .- Print
2

Interestingly, Object (Skeleton t) m and Object t m are isomorphic (Skeleton is an operational monad). cascading lets objects to handle an operational monad.

About

Purely functional objects, attempt to hack in STM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Haskell 100.0%
0