This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3083044
commit 677b96a
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module Lunarbox.Control.Monad.Dataflow.Interpreter | ||
( Interpreter(..) | ||
, InterpreterContext(..) | ||
, _location | ||
, runInterpreter | ||
) where | ||
|
||
import Prelude | ||
import Control.Monad.Reader (class MonadAsk, class MonadReader, Reader, runReader) | ||
import Control.Monad.Writer (class MonadTell, class MonadWriter, WriterT, runWriterT) | ||
import Data.Lens (Lens') | ||
import Data.Lens.Record (prop) | ||
import Data.Newtype (class Newtype) | ||
import Data.Symbol (SProxy(..)) | ||
import Lunarbox.Data.Dataflow.Runtime.TermEnvironment (TermEnvironment) | ||
import Lunarbox.Data.Dataflow.Runtime.ValueMap (ValueMap) | ||
import Lunarbox.Data.Lens (newtypeIso) | ||
|
||
-- The Interpreter context is the env all interpreting occurs in. | ||
newtype InterpreterContext l | ||
= InterpreterContext | ||
{ location :: l | ||
, typeEnv :: TermEnvironment | ||
} | ||
|
||
derive instance newtypeInterpreterContent :: Newtype (InterpreterContext l) _ | ||
|
||
_location :: forall l. Lens' (InterpreterContext l) l | ||
_location = newtypeIso <<< prop (SProxy :: _ "location") | ||
|
||
-- Monad used to Interpret expressions | ||
newtype Interpreter l a | ||
= Interpreter (WriterT (ValueMap l) (Reader (InterpreterContext l)) a) | ||
|
||
-- Takes a Interpreter monad and runs it | ||
runInterpreter :: forall l a. Ord l => Interpreter l a -> ValueMap l | ||
runInterpreter (Interpreter m) = runReader mempty $ runWriterT m | ||
|
||
-- Typeclasses | ||
derive instance newtypeInterpreter :: Newtype (Interpreter l a) _ | ||
|
||
derive newtype instance functorInterpreter :: Ord l => Functor (Interpreter l) | ||
|
||
derive newtype instance applyInterpreter :: Ord l => Apply (Interpreter l) | ||
|
||
derive newtype instance applicativeInterpreter :: Ord l => Applicative (Interpreter l) | ||
|
||
derive newtype instance bindInterpreter :: Ord l => Bind (Interpreter l) | ||
|
||
derive newtype instance monadInterpreter :: Ord l => Monad (Interpreter l) | ||
|
||
derive newtype instance monadAskInterpreter :: Ord l => MonadAsk (InterpreterContext l) (Interpreter l) | ||
|
||
derive newtype instance monadReaderInterpreter :: Ord l => MonadReader (InterpreterContext l) (Interpreter l) | ||
|
||
derive newtype instance monadTellInterpreter :: Ord l => MonadTell (ValueMap l) (Interpreter l) | ||
|
||
derive newtype instance monadWriterInterpreter :: Ord l => MonadWriter (ValueMap l) (Interpreter l) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Lunarbox.Data.Dataflow.Runtime.TermEnvironment | ||
( TermEnvironment(..) | ||
) where | ||
|
||
import Prelude | ||
import Data.Map as Map | ||
import Data.Newtype (class Newtype) | ||
import Lunarbox.Data.Dataflow.Runtime (RuntimeValue) | ||
|
||
newtype TermEnvironment | ||
= TermEnvironment (Map.Map String RuntimeValue) | ||
|
||
derive instance eqTermEnvironment :: Eq TermEnvironment | ||
|
||
derive instance newtypeTermEnvironment :: Newtype TermEnvironment _ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Lunarbox.Data.Dataflow.Runtime.ValueMap | ||
( ValueMap(..) | ||
) where | ||
|
||
import Prelude | ||
import Data.Map as Map | ||
import Data.Newtype (class Newtype) | ||
import Lunarbox.Data.Dataflow.Runtime (RuntimeValue) | ||
|
||
-- A map holding the runtime values of different locations | ||
newtype ValueMap l | ||
= ValueMap (Map.Map l RuntimeValue) | ||
|
||
derive instance eqValueMap :: Eq l => Eq (ValueMap l) | ||
|
||
derive instance newtypeValueMap :: Newtype (ValueMap l) _ | ||
|
||
derive newtype instance semigroupValueMap :: Ord l => Semigroup (ValueMap l) | ||
|
||
derive newtype instance monoidValueMap :: Ord l => Monoid (ValueMap l) |