Skip to content

Commit 0b866c9

Browse files
author
euonymos
committed
chore: refactor Cardano.CEM.Compile module
1 parent a352f62 commit 0b866c9

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

example/CEM/Example/Compiled.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import CEM.Example.Voting (SimpleVoting)
1010
import Cardano.CEM
1111
import Prelude
1212

13-
$(compileCEM True ''SimpleAuction)
14-
$(compileCEM False ''SimpleVoting)
13+
$(compileCEMOnchain True ''SimpleAuction)
14+
$(compileCEMOnchain False ''SimpleVoting)

src/Cardano/CEM.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module Cardano.CEM (
44

55
-- TODO: review
66

7-
import Cardano.CEM.Address as X (cemScriptPlutusCredential)
7+
import Cardano.CEM.Address as X (
8+
cemScriptAddress,
9+
cemScriptPlutusAddress,
10+
cemScriptPlutusCredential,
11+
)
812
import Cardano.CEM.Compile as X
913
import Cardano.CEM.DSL as X (
1014
CEMScript (..),
@@ -19,5 +23,8 @@ import Cardano.CEM.Monads as X
1923
import Cardano.CEM.Monads.CLB as X
2024
import Cardano.CEM.OffChain as X
2125
import Cardano.CEM.OnChain as X
22-
import Cardano.CEM.TH as X (compileCEM, deriveCEMAssociatedTypes)
26+
import Cardano.CEM.TH as X (
27+
compileCEMOnchain,
28+
deriveCEMAssociatedTypes,
29+
)
2330
import Data.Spine as X (derivePlutusSpine)

src/Cardano/CEM/Compile.hs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,54 @@ transitionStateSpines kind spec = concat $ map ownUtxoState spec
7272
-- and smart constructors.
7373
parseSpine _ = error "SameScript is too complex to statically know its spine"
7474

75-
-- | Checking for errors and normalising
75+
-- -----------------------------------------------------------------------------
76+
-- Some preliminary checks
77+
-- -----------------------------------------------------------------------------
78+
79+
-- Checks are based on this pseudo-lattice ordering.
80+
data CheckResult = Yes | No | Maybe
81+
deriving stock (Eq, Show)
82+
83+
opposite :: Ordering -> Ordering
84+
opposite EQ = EQ
85+
opposite LT = GT
86+
opposite GT = LT
87+
88+
instance Ord CheckResult where
89+
compare Yes No = EQ
90+
compare Yes Maybe = GT
91+
compare No Maybe = GT
92+
compare Yes Yes = EQ
93+
compare No No = EQ
94+
compare Maybe Maybe = EQ
95+
compare x y = opposite $ compare y x
96+
97+
{- | Performs some preliminary checks over the CEM script specification:
98+
* there is only one initial transition
99+
* every transition has zero or one `In` state
100+
-}
76101
preProcessForOnChainCompilation ::
77102
(CEMScript script, Show a) =>
78103
Map.Map a [TxConstraint False script] ->
79104
Map.Map a [TxConstraint False script]
80105
preProcessForOnChainCompilation spec =
81-
if length possibleCreators == 1
106+
if length initialTransitions == 1
82107
then
83108
let
84-
-- FIXME: relies on `error` inside...
109+
-- PM relies on `error` inside transitionInStateSpine
85110
!_ = map transitionInStateSpine $ Map.elems spec
86111
in
87112
spec
88113
else
89114
error $
90-
"CEMScript should have exactly 1 creating transition, "
91-
<> "while possible creators are "
92-
<> ppShowList possibleCreators
115+
"CEMScript must have exactly one initial transition, "
116+
<> "while there are many ones: "
117+
<> ppShowList initialTransitions
93118
where
94-
possibleCreators = filter (maybeIsCreator . snd) (Map.toList spec)
119+
initialTransitions = filter (isInitial . snd) (Map.toList spec)
95120

96-
maybeIsCreator :: [TxConstraint resolved script] -> Bool
97-
maybeIsCreator constrs =
121+
isInitial :: [TxConstraint resolved script] -> Bool
122+
isInitial constrs =
98123
not (maybeHasSameScriptFanOfKind In)
99124
&& maybeHasSameScriptFanOfKind Out
100125
where
@@ -111,21 +136,3 @@ preProcessForOnChainCompilation spec =
111136
_ -> No
112137
where
113138
recur = isSameScriptOfKind xKind
114-
115-
-- | We have abstract interpretator at home
116-
data CheckResult = Yes | No | Maybe
117-
deriving stock (Eq, Show)
118-
119-
opposite :: Ordering -> Ordering
120-
opposite EQ = EQ
121-
opposite LT = GT
122-
opposite GT = LT
123-
124-
instance Ord CheckResult where
125-
compare Yes No = EQ
126-
compare Yes Maybe = GT
127-
compare No Maybe = GT
128-
compare Yes Yes = EQ
129-
compare No No = EQ
130-
compare Maybe Maybe = EQ
131-
compare x y = opposite $ compare y x

src/Cardano/CEM/Indexing.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module Cardano.CEM.Indexing
2-
( module X
3-
) where
1+
module Cardano.CEM.Indexing (
2+
module X,
3+
) where
44

55
import Cardano.CEM.Indexing.Event as X
66
import Cardano.CEM.Indexing.Oura as X
7-
import Cardano.CEM.Indexing.Tx as X
7+
import Cardano.CEM.Indexing.Tx as X

src/Cardano/CEM/TH.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Cardano.CEM.TH (
22
deriveCEMAssociatedTypes,
3-
compileCEM,
3+
compileCEMOnchain,
44
) where
55

66
import Cardano.CEM.Compile (preProcessForOnChainCompilation)
@@ -49,8 +49,8 @@ deriveCEMAssociatedTypes _deriveBlueprint scriptName = do
4949
reifyInstances familyName [argType]
5050
return name
5151

52-
compileCEM :: Bool -> Name -> Q [Dec]
53-
compileCEM debugBuild name = do
52+
compileCEMOnchain :: Bool -> Name -> Q [Dec]
53+
compileCEMOnchain debugBuild name = do
5454
-- TODO: two duplicating cases on `transitionComp`
5555
let plutusScript =
5656
[|

0 commit comments

Comments
 (0)