4

Hoopl中(前向)重写函数的类型由mkFRewrite函数给出:

mkFRewrite :: (FuelMonad m) => 
   (forall e x.
      n e x
      -> f
      -> m (Maybe (hoopl-3.8.6.1:Compiler.Hoopl.Dataflow.Graph n e x)))
   -> FwdRewrite m n f

m类型意味着我可以在重写时使用单子效果。论文“Hoopl: A Modular, Reusable Library for Dataflow Analysis and Transformation”在第 4.3 节“重写函数和客户端的 monad”中说了同样的话。

谁能给我一个重写函数的例子,其中嵌入了非 Hoopl monadic效果?例如,使用 State monad 或执行一些 IO 的重写器。

4

1 回答 1

2

这应该很简单,只需追逐类型。

您想要一个FwdRewrite m n f具有自定义值的 值m,因此您可以将其传递给以下函数:

analyzeAndRewriteFwd ::
  forall m n f e x entries.
    (CheckpointMonad m,
     NonLocal n,
     LabelsPtr entries) =>
  FwdPass m n f ->
  MaybeC e entries ->
  Graph n e x ->
  Fact e f ->
  m (Graph n e x, FactBase f, MaybeO x f)

所以对m你的唯一限制是它是一个CheckpointMonad; 然后,当您运行通行证时,您将获得可以自己运行的最终一元值。

事实上,GHC 的 Hoopl 通过使用 with mas a SimplUniqMonad,因此我们可以在对图进行操作时获得新的标签。

{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeFamilies #-}

import Compiler.Hoopl
import Control.Monad.State

type StateFuel s a = CheckingFuelMonad (State s) a

instance CheckpointMonad (State s) where
    type Checkpoint (State s) = s
    checkpoint = get
    restart = put
于 2011-07-04T13:55:52.343 回答