Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nothunks instances (#76) #97

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crem.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ library
ghc-options: -Weverything -Wno-safe -Wno-unsafe -Wno-missing-safe-haskell-mode -Wno-implicit-prelude -Wno-missing-export-lists -Wno-missing-home-modules -Wno-missing-import-lists -Wno-all-missed-specialisations -Wno-prepositive-qualified-module
build-depends:
base >=4.15 && <4.19
, nothunks >=0.1 && <0.4
, profunctors >=3.2 && <5.7
, singletons-base >=3.0 && <3.3
, text >=1.2 && <2.1
Expand Down
2 changes: 2 additions & 0 deletions src/Crem/Graph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module Crem.Graph where

import Crem.Render.RenderableVertices (RenderableVertices (..))
import "base" Data.List (nub)
import "nothunks" NoThunks.Class (NoThunks (..))

-- * Graph

-- | A graph is just a list of edges between vertices of type @a@
newtype Graph a = Graph [(a, a)]
deriving stock (Eq, Show)
deriving newtype (NoThunks)

-- | The product graph.
-- It has as vertices the product of the set of vertices of the initial graph.
Expand Down
8 changes: 8 additions & 0 deletions src/Crem/Render/RenderFlow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Crem.Render.RenderFlow where

import Crem.Render.Render
import Crem.StateMachine
import "nothunks" NoThunks.Class (NoThunks (..), allNoThunks)

-- | A tree-like structure which could be used to attach metadata to any
-- similar tree-like structure with only leaves and nodes with exactly two
Expand All @@ -20,6 +21,13 @@ data TreeMetadata a
| BinaryLabel (TreeMetadata a) (TreeMetadata a)
deriving stock (Show)

instance NoThunks a => NoThunks (TreeMetadata a) where
showTypeOf _ = "TreeMetadata"
wNoThunks ctxt tm =
case tm of
LeafLabel x -> noThunks ctxt x
BinaryLabel x y -> allNoThunks [noThunks ctxt x, noThunks ctxt y]

-- | Given a `StateMachineT` and a `TreeMetadata` of @MachineLabel@s, we can
-- create a flow representation of our machine.
--
Expand Down
12 changes: 12 additions & 0 deletions src/Crem/StateMachine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "base" Control.Category (Category (..))
import "base" Data.Bifunctor (Bifunctor (second), bimap)
import "base" Data.Foldable (foldlM)
import "base" Data.Kind (Type)
import "nothunks" NoThunks.Class (NoThunks (..), allNoThunks)
import "profunctors" Data.Profunctor (Choice (..), Profunctor (..), Strong (..))
import "singletons-base" Data.Singletons (Demote, SingI, SingKind)
import Prelude hiding ((.))
Expand Down Expand Up @@ -74,6 +75,17 @@ data StateMachineT m input output where
-> StateMachineT m b (n c)
-> StateMachineT m a (n c)

instance NoThunks (StateMachineT m input output) where
showTypeOf _ = "StateMachineT"
wNoThunks ctxt sm =
case sm of
Basic _ -> return Nothing
Sequential x y -> allNoThunks [noThunks ctxt x, noThunks ctxt y]
Parallel x y -> allNoThunks [noThunks ctxt x, noThunks ctxt y]
Alternative x y -> allNoThunks [noThunks ctxt x, noThunks ctxt y]
Feedback x y -> allNoThunks [noThunks ctxt x, noThunks ctxt y]
Kleisli x y -> allNoThunks [noThunks ctxt x, noThunks ctxt y]

-- | A `StateMachine` is an effectful machine for every possible monad @m@.
-- Needing to work for every monad, in fact it can not perform any kind of
-- effect and needs to be pure in nature.
Expand Down
10 changes: 10 additions & 0 deletions src/Crem/Topology.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Crem.Topology
)
where

import "nothunks" NoThunks.Class (NoThunks (..))
import "singletons-base" Data.Singletons.Base.TH
import "singletons-base" Prelude.Singletons

Expand Down Expand Up @@ -64,6 +65,15 @@ data AllowTransition (topology :: Topology vertex) (initial :: vertex) (final ::
:: AllowTransition ('Topology map) a b
-> AllowTransition ('Topology (x ': map)) a b

instance NoThunks (AllowTransition topology initial final) where
showTypeOf _ = "AllowTransition"
wNoThunks ctxt at =
case at of
AllowIdentityEdge -> return Nothing
AllowFirstEdge -> return Nothing
AllowAddingEdge x -> noThunks ctxt x
AllowAddingVertex x -> noThunks ctxt x

-- | The `AllowedTransition` type class enables to automatically perform proof search
-- for a `AllowTransition` term.
-- It has an instance for every constructor of `AllowTransition`
Expand Down