Skip to content

Instantly share code, notes, and snippets.

@sshine
Last active September 12, 2022 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshine/89367a6886414c56a5b766ba204b5de5 to your computer and use it in GitHub Desktop.
Save sshine/89367a6886414c56a5b766ba204b5de5 to your computer and use it in GitHub Desktop.
-- I'm trying to symbolically evaluate this function,
mapOpcodeM (jumpMapper (lookupLabel M.empty)) ADD :: Either TranslateError PositionalOpcode
~> mapOnOther' ADD ADD
~> do res <- mapOnOther (jumpMapper (lookupLabel M.empty)) ADD
case res of
Just opb -> return opb
Nothing -> return ADD
~> do res <- const (pure Nothing) ADD
case res of
Just opb -> return opb
Nothing -> return ADD
~> do res <- Right Nothing
case res of
Just opb -> return opb
Nothing -> return ADD
~> case Nothing of
Just opb -> return opb
Nothing -> return ADD
~> return ADD
-- Yet, when I run it, it doesn't terminate:
> mapOpcodeM (jumpMapper (lookupLabel M.empty)) ADD :: Either TranslateError PositionalOpcode
Right ...hangs...^CInterrupted.
mapOpcodeM :: forall m a b. Monad m => OpcodeMapper m a b -> Opcode' a -> m (Opcode' b)
mapOpcodeM mapper opcode = case opcode of
JUMP a -> mapOnJump mapper a
JUMPI a -> mapOnJumpi mapper a
JUMPDEST a -> mapOnJumpdest mapper a
-- 0s: Stop and Arithmetic Operations
STOP -> mapOnOther' STOP STOP
ADD -> mapOnOther' ADD ADD
...
where
mapOnOther' :: Opcode' a -> Opcode' b -> m (Opcode' b)
mapOnOther' opa opbDefault = do
res <- mapOnOther mapper opa
case res of
Just opb -> return opb
Nothing -> return opbDefault
jumpMapper :: Applicative f => (a -> f j) -> OpcodeMapper f a j
jumpMapper f = OpcodeMapper
{ mapOnJump = fmap JUMP . f
, mapOnJumpi = fmap JUMPI . f
, mapOnJumpdest = fmap JUMPDEST . f
, mapOnOther = const (pure Nothing)
}
lookupLabel :: Map Text position -> Text -> Either TranslateError position
lookupLabel labelMap label =
case Map.lookup label labelMap of
Just pos -> Right pos
Nothing -> Left (TranslateError [label] [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment