Code Monkey home page Code Monkey logo

Comments (2)

pepeiborra avatar pepeiborra commented on August 17, 2024

Hi Sjoerd,
Thanks for the input. I have uploaded to Hackage a new version including induce.
I cooked an example based on Swierstra calculator:

import Control.Monad.Free
import Control.Monad.State

data CalcF a = Incr Int a
             | Recall (Int -> a)
             | Clear a
  deriving Functor

type Calc a = Free CalcF a

clear :: Calc ()
incr  :: Int -> Calc ()
clear  = Impure $ Clear (Pure ())
incr i = Impure $ Incr i (Pure ())
recall = Impure (Recall Pure)

runCalc :: Calc a -> Int
runCalc = (`execState` 0) . induce f where
  f (Incr i r) = modify (+i) >> return r
  f (Clear r)  = put 0 >> return r
  f (Recall k) = get >>= \i -> return (k i)

Do you have any other examples in mind?

from control-monad-free.

sjoerdvisscher avatar sjoerdvisscher commented on August 17, 2024

I like your example a lot!

This was the Agda code I wanted to implement in Haskell:
http://personal.cis.strath.ac.uk/~ng/MonadicIR.agda

It now becomes:

class Reflective f where
reflect :: Monad m => f i o -> m i -> m o

decode :: (Reflective f, Functor (f i), Monad m) => Free (f i) o -> m i -> m o
decode = runReaderT . induce (ReaderT . reflect)

Pretty nice, but totally useless, as it all has to be done at type level to be of any use for induction recursion.

from control-monad-free.

Related Issues (4)

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.