简体   繁体   English

类型同义词的部分应用

[英]Partial application of type synonyms

I'm having trouble with unsaturated type synonyms in the following example: 在以下示例中,我遇到了不饱和类型同义词的麻烦:

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE LiberalTypeSynonyms #-}

module TypeFamilyHackery where

data T k v a = T

type family CollectArgTypes arr where
  CollectArgTypes (a -> b) = (a, CollectArgTypes b)
  CollectArgTypes _ = ()

type family MapReturnType f t where
  MapReturnType f (a -> b) = a -> MapReturnType f b
  MapReturnType f r = f r

type MkT k v = T k v v

-- | Goal:
-- @
--   BuryT Int = T () Int Int
--   BuryT (Bool -> Int) = Bool -> T (Bool, ()) Int Int
--   BuryT (a -> b -> c) = a -> b -> T (a,(b,())) c c
-- @
type BuryT t = MapReturnType (MkT (CollectArgTypes t)) t

But this complains with The type synonym 'MkT' should have 2 arguments, but has been given 1 . 但这抱怨The type synonym 'MkT' should have 2 arguments, but has been given 1 I could specialize MapReturnType for MkT (CollectArgTypes t) , but I rather like it how it is. 我可以将MapReturnTypeMkT (CollectArgTypes t) ,但我更喜欢它的状态。

Since -XLiberalTypeSynonyms doesn't seem to deliver (why?), what are my options to get BuryT working? 由于-XLiberalTypeSynonyms似乎未提供(为什么?),我如何选择才能使BuryT正常工作?

LiberalTypeSynonyms works by inlining all “obvious” type definitions. LiberalTypeSynonyms通过内联所有“显而易见的”类型定义来工作。 To make your example work, it would have to 为了使您的示例有效,必须

  1. Treat MapReturnType (MkT (CollectArgTypes t)) t first as some MapReturnType ㄊ t 首先将MapReturnType (MkT (CollectArgTypes t)) t视为某些MapReturnType ㄊ t
  2. Inline this to ㄊ t , using the definition MapReturnType fr = fr 使用定义MapReturnType fr = fr将其内联到ㄊ t
  3. At this point, invoking again would give MkT (CollectArgTypes t) t which is a perfectly fine fully-applied synonym and hence no problem. 在这一点上,调用又会给MkT (CollectArgTypes t) t这是一个完全正常的完全应用代名词,因此没有问题。

But step 2 isn't possible, because MapReturnType is not just a synonym. 但第2步是不可能的,因为MapReturnType 只是一个代名词。 To use MapReturnType fr = fr , the compiler would first have to be sure that r isn't a function type, but it really can't know this – it's after all a completely free parameter. 要使用MapReturnType fr = fr ,编译器首先必须确保r不是函数类型,但它实际上不知道这一点–毕竟这是一个完全自由的参数。

So what the compiler would actually need to do there is, defer the resolution of MapReturnType and thus also of BuryT to concrete use sites. 那么编译器会真正需要做的有,推迟的分辨率MapReturnType ,因此也BuryT到具体使用的网站。 Now, that might be quite useful, but it would open up quite a can of worms. 现在,这可能很有用,但是会打开很多蠕虫。 Namely, it would then be very easy to intertwine Turing-complete programs anywhere in the types of your program. 也就是说,然后很容易在程序类型中的任何地方将图灵完成的程序交织在一起。 I don't think this is worth it. 我认为这不值得。

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

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