Skip to content

Commit 83ff82d

Browse files
committed
WIP AST refactoring
1 parent 29b0740 commit 83ff82d

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

src/Act/Syntax/Timing.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Data.Char (toLower)
2121
data Timing = Timed | Untimed
2222

2323
-- | Encodes choice between explicitly referring to the pre-/post-state, or not.
24-
data Time where
24+
data Time t where
2525
Pre :: Time Timed
2626
Post :: Time Timed
2727
Neither :: Time Untimed

src/Act/Syntax/Typed.hs

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ import Act.Syntax.Timing as Act.Syntax.Typed
5757
import Act.Syntax.Untyped as Act.Syntax.Typed (Pn, Interface(..), EthEnv(..), Decl(..), SlotType(..), ArgType(..), Pointer(..), makeIface, argToAbiType)
5858

5959
-- AST post typechecking
60-
data Act t = Act Store [Contract t]
60+
data Act t = Act StorageTyping [Contract t]
6161
deriving (Show, Eq)
6262

6363
data Contract t = Contract (Constructor t) [Behaviour t]
6464
deriving (Show, Eq)
6565

66-
-- For each contract, it stores the type of a storage variables and
67-
-- the order in which they are declared
68-
type Store = Map Id (Map Id (SlotType, Integer))
66+
-- For each contract, we store the type of a storage variables and the order in
67+
-- which they are declared
68+
type StorageTyping = Map Id (Map Id (ValueType, Integer))
6969

7070

7171
-- | Represents a contract level invariant. The invariant is defined in the
@@ -93,49 +93,47 @@ data InvariantPred (t :: Timing) where
9393
deriving instance Show (InvariantPred t)
9494
deriving instance Eq (InvariantPred t)
9595

96+
type Cases a = [(Exp ABoolean Untimed, a)]
97+
9698
data Constructor t = Constructor
9799
{ _cname :: Id
98100
, _cinterface :: Interface
99101
, _cpreconditions :: [Exp ABoolean t]
102+
, _ccases :: Cases [StorageUpdate t]
100103
, _cpostconditions :: [Exp ABoolean t]
101104
, _invariants :: [Invariant t]
102-
, _initialStorage :: [StorageUpdate t]
103105
} deriving (Show, Eq)
104-
105-
106+
106107
-- After typing each behavior may be split to multiple behaviors, one for each case branch.
107108
-- In this case, only the `_caseconditions`, `_stateUpdates`, and `_returns` fields are different.
108109
data Behaviour t = Behaviour
109110
{ _name :: Id
110111
, _contract :: Id
111112
, _interface :: Interface
112113
, _preconditions :: [Exp ABoolean t] -- if preconditions are not satisfied execution is reverted
113-
, _caseconditions :: [Exp ABoolean t] -- if preconditions are satisfied and the case conditions are not, some other instance of the behavior should apply
114+
, _cases :: Cases ([StorageUpdate t], Maybe (TypedExp Timed))
114115
, _postconditions :: [Exp ABoolean Timed]
115-
, _stateUpdates :: [StorageUpdate t]
116-
, _returns :: Maybe (TypedExp Timed)
117116
} deriving (Show, Eq)
118117

119118
data StorageUpdate (t :: Timing) where
120-
Update :: TValueType a -> TItem a Storage t -> Exp a t -> StorageUpdate t
119+
Update :: TValueType a -> Ref Storage t -> Exp a t -> StorageUpdate t
121120
deriving instance Show (StorageUpdate t)
122121

123122
instance Eq (StorageUpdate t) where
124123
(==) :: StorageUpdate t -> StorageUpdate t -> Bool
125124
Update vt1@VType i1 e1 == Update vt2@VType i2 e2 = eqS'' vt1 vt2 && eqS' i1 i2 && eqS e1 e2
126125

127-
_Update :: TItem a Storage t -> Exp a t -> StorageUpdate t
128-
_Update item@(Item t@VType _) expr = Update t item expr
129-
130-
data Location (t :: Timing) where
131-
Loc :: TValueType a -> SRefKind k -> TItem a k t -> Location t
132-
deriving instance Show (Location t)
126+
_Update :: Ref Storage t -> Exp a t -> StorageUpdate t
127+
_Update ref expr = Update (getValueType ref) ref expr
133128

134-
instance Eq (Location t) where
135-
Loc vt1@VType SRefKind i1 == Loc vt2@VType SRefKind i2 = eqS'' vt1 vt2 && eqTypeKind i1 i2
129+
data TypedRef (t :: Timing) where
130+
TRef :: TValueType a -> SRefKind k -> Ref k t -> TypedRef t
131+
deriving instance Show (TypedRef t)
136132

137-
_Loc :: SRefKind k -> TItem a k t -> Location t
138-
_Loc k item@(Item t@VType _) = Loc t k item
133+
instance Eq (TypedRef t) where
134+
TRef vt1@VType SRefKind i1 == TRef vt2@VType SRefKind i2 = eqS'' vt1 vt2 && eqTypeKind i1 i2
135+
_TRef :: SRefKind k -> Ref k t -> TypedRef t
136+
_TRef k item@(Item t@VType _) = TRef t k item
139137

140138
-- | Distinguish the type of Refs to calldata variables and storage
141139
data RefKind = Storage | Calldata
@@ -197,21 +195,6 @@ instance Eq (Ref k t) where
197195
SField _ r c x == SField _ r' c' x' = r == r' && c == c' && x == x'
198196
_ == _ = False
199197

200-
-- | Item is a reference together with its Act type. The type is
201-
-- parametrized on a timing `t`, a type `a`, and the reference kind
202-
-- `k`. `t` can be either `Timed` or `Untimed` and indicates whether
203-
-- any indices that reference items in storage explicitly refer to the
204-
-- pre-/post-state, or not. `a` is the type of the item that is
205-
-- referenced. Items are also annotated with the original ValueType
206-
-- that carries more precise type information (e.g., the exact
207-
-- contract type).
208-
data TItem (a :: ActType) (k :: RefKind) (t :: Timing) where
209-
Item :: TValueType a -> Ref k t -> TItem a k t
210-
deriving instance Show (TItem a k t)
211-
deriving instance Eq (TItem a k t)
212-
213-
--_Item :: SingI a => ValueType -> Ref k t -> TItem a k t
214-
--_Item = Item sing
215198

216199
-- | Expressions for which the return type is known.
217200
data TypedExp t

0 commit comments

Comments
 (0)