简体   繁体   English

等级2约束编程/约束含义

[英]Rank 2 constraint programming / constraint implication

import Data.Constraint

reify :: (c' :- c) -> (c => a) -> (c' => a)
reify cons f = case cons of Sub d -> case d of Dict -> f

Using reify , I can weaken a constraint c to a constraint c' given proof that c' implies c . 使用reify ,我可以削弱约束c到约束c'给出证明c'意味着c

Now, I want a Rank2 variant of this: 现在,我想要一个Rank2变体:

-- reify2 Rank2's reify
reify2 :: (forall r1. c' r1 :- c r1) -> 
          (forall r2. c r2 => a) -> 
          (forall r3. c' r3 => a)
reify2 cons f = ???

But I'm unable to implement such a function, even though it must "clearly" be possible. 但是,即使必须“显然”可行,我也无法实现该功能。

The ambiguity can be lifted using ScopedTypeVariables+TypeApplications , although you need to reorder the arguments of reify2 , putting the type arguments first to put them in scope. 可以使用ScopedTypeVariables+TypeApplications歧义,尽管您需要重新排列reify2的参数, reify2将类型参数放入范围。

{-# LANGUAGE AllowAmbiguousTypes, RankNTypes, ConstraintKinds, GADTs, ScopedTypeVariables, TypeApplications, TypeOperators #-}

data Dict c where
  Dict :: c => Dict c

data c :- d where
  Sub :: (c => Dict d) -> c :- d

reify2 :: forall r3 c c' a. c' r3 =>
          (forall r1. c' r1 :- c r1) ->
          (forall r2. c r2 => a) ->
          a
reify2 cons f =
  case cons @r3 of
    Sub Dict -> f @r3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM