Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Compiler construction 不安全的强制和更高效的Agda代码(-ftrust me im Agda)_Compiler Construction_Haskell_Agda - Fatal编程技术网

Compiler construction 不安全的强制和更高效的Agda代码(-ftrust me im Agda)

Compiler construction 不安全的强制和更高效的Agda代码(-ftrust me im Agda),compiler-construction,haskell,agda,Compiler Construction,Haskell,Agda,在Agda邮件列表上,Conor McBride询问: 有什么办法能找到我吗 像假定的 trustFromJust :: Maybe x -> x 实际上,这并没有检查公正性,而是出了问题(在米尔纳的文章中) (感觉)如果什么都没有 Agda可能会证明a==1 a,并且可以消除sum类型的中间构造函数 我可以想到使用“不安全”或“解包关闭”的方法,但其他人有想法吗 import GHC.Prim trustFromJust :: Maybe x -> x trustFrom

在Agda邮件列表上,Conor McBride询问:

有什么办法能找到我吗 像假定的

   trustFromJust :: Maybe x -> x
实际上,这并没有检查公正性,而是出了问题(在米尔纳的文章中) (感觉)如果什么都没有

Agda可能会证明a==1 a,并且可以消除sum类型的中间构造函数

我可以想到使用“不安全”或“解包关闭”的方法,但其他人有想法吗

import GHC.Prim

trustFromJust :: Maybe x -> x
trustFromJust x = y
    where Just1 y = unsafeCoerce# x

data Just1 a = Just1 a
虽然这样做会导致错误(单个构造函数类型可以避免一些闭包开销)。但核心看起来还不错:

main2 =
  case (Data.Maybe.Just @ Type.Integer main3)
       `cast`
       (CoUnsafe
         (Data.Maybe.Maybe Type.Integer)
         (Just1 Type.Integer)
               :: Data.Maybe.Maybe Type.Integer
                    ~
                  Just1 Type.Integer)
  of _ { Just1 y_aeb ->
  $wshowsPrec 0 y_aeb ([] @ Char)

由于这是一个研究问题,我们有一些可能的方法,但它们都归结为:

  • 玩些小把戏,把“也许”的标签倒过来

有趣的是,您的代码在GHCi中运行。虽然它在编译时确实会出错。也许它的第二个构造函数是
Just
,第一个构造函数是
Just1
,这难道不意味着它们会得到不同的thunk返回向量偏移量吗?在这两种情况下,必须构造一个
可能
框来解包,无论您是使用unsafeccerce还是fromJust解包,似乎都会有相同的开销,不是吗?如果您在
Just1
构造函数前面添加一个伪构造函数,那么
unsafeccerce#
将不会导致segfault。我不知道这是因为偏移量现在匹配还是因为数据类型现在也是求和类型。问题是指针标记:
just x
的表示是一个低位为2的指针,而
Just1 x
是一个低位为1的指针。单个构造函数数据类型的case continuation假设标记位为1,并在访问字段时考虑到这一点。使用
版本的
可能
并反转构造函数,以便
具有标记1,这是一种合法的解决方法。