Hoopl 中重写函数内的单子效应示例?

发布于 2024-11-17 23:49:52 字数 771 浏览 3 评论 0原文

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:模块化,用于数据流分析和转换的可重用库” 在第 4.3 节“重写函数和客户端的 monad”中也有同样的说法。

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

The type of (forward) rewriting functions in Hoopl is given by the mkFRewrite function:

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

The m type implies that I can use monadic effects while rewriting. The paper "Hoopl: A Modular, Reusable Library for Dataflow Analysis and Transformation" says the same in Section 4.3, "The rewrite function and the client's monad."

Can anyone give me an example of a rewrite function that has non-Hoopl monadic effects embedded inside it? For example, a rewriter that uses a State monad or does some IO.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-11-24 23:49:52

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

您需要值 FwdRewrite mn 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 的唯一约束code> 你所拥有的是它是一个 CheckpointMonad;然后,当您运行该通行证时,您将获得您可以自己运行的最终单子值。

事实上,GHC 的 Hoopl 将 m 用作 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

This should be pretty simple, just chase the types.

You want a value of FwdRewrite m n f with a custom value of m, so you can pass it to the following function:

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)

So the only constraint on m you have is that it is a CheckpointMonad; then when you run the pass you'll get the final monadic value which you can run yourself.

In fact, GHC's Hoopl passes use with m as a SimplUniqMonad, so we can get fresh labels while we're operating on the graph.

{-# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文