From bb7b28a9e8a1a05d6d9af0f943a158a03a148190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Thu, 9 Jul 2026 19:44:18 +0200 Subject: Expr data type --- minici.cabal | 1 + src/Expr.hs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/Expr.hs diff --git a/minici.cabal b/minici.cabal index 0a35b53..0bb59a2 100644 --- a/minici.cabal +++ b/minici.cabal @@ -47,6 +47,7 @@ executable minici Config Destination Eval + Expr Expression FileUtils Job diff --git a/src/Expr.hs b/src/Expr.hs new file mode 100644 index 0000000..fa17158 --- /dev/null +++ b/src/Expr.hs @@ -0,0 +1,51 @@ +module Expr ( + Expr(..), + ExprContext(..), + getContext, + + addDependency, + collectDependencies, + + exprIO, +) where + +import Data.Kind + + +data Expr c a where + Pure :: a -> Expr c a + App :: Expr c (a -> b) -> Expr c a -> Expr c b + GetContext :: Expr c c + AddDependency :: ExprContext c => ExprDependency c -> Expr c a -> Expr c a + ExprIO :: IO a -> Expr c a + +instance Functor (Expr c) where + fmap f x = Pure f <*> x + +instance Applicative (Expr c) where + pure = Pure + (<*>) = App + + +class Monoid (ExprDependency c) => ExprContext c where + type ExprDependency c :: Type + + +getContext :: Expr c c +getContext = GetContext + + +addDependency :: ExprContext c => ExprDependency c -> Expr c a -> Expr c a +addDependency = AddDependency + +collectDependencies :: ExprContext c => Expr c a -> ExprDependency c +collectDependencies = \case + Pure {} -> mempty + App f x -> collectDependencies f <> collectDependencies x + GetContext {} -> mempty + AddDependency d x -> d <> collectDependencies x + ExprIO {} -> mempty + + +exprIO :: IO a -> Expr c a +exprIO = ExprIO -- cgit v1.2.3