'惯性'的Haskell型不等式 [英] 'idiomatic' Haskell type inequality

查看:112
本文介绍了'惯性'的Haskell型不等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(从前面的问题编辑,我认为下面的代码不起作用)

我希望实现一个haskell函数f,它有一个限制,使得它的2个参数不能有相同的类型。我已经使用了下面的代码:

$ $ p $ {code> { - #LANGUAGE MultiParamTypeClasses,FunctionalDependencies,UndecidableInstances,FlexibleInstances,FlexibleContexts,TypeFamilies,IncoherentInstances# - }
data HTrue = HTrue
data HFalse = HFalse

class HEq xyb | x y - > b
实例(b〜HTrue)=> HEq x x b
instance(b〜HFalse)=> HEq xy b


g ::(HEq a b HFalse)=> a - > b - > ()
gxy =()

现在函数g只接受a和b不同种类。这是在haskell中编码类型不等式的'idiomiatic'方法吗?如果没有,那么它有什么问题? new

  { - #语言DataKinds,PolyKinds,TypeFamilies# - } 

类型家庭Equal(a :: k)(b :: k):: Bool
类型实例其中
等于aa = True
Equal ab = False


(edited from previous question where I thought code below doesn't work)

I wish to implement a haskell function f that has a restriction such that its 2 parameters must not have the same type. I have used the following code:

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, UndecidableInstances, FlexibleInstances, FlexibleContexts, TypeFamilies, IncoherentInstances #-}
data HTrue = HTrue
data HFalse = HFalse

class HEq x y b | x y -> b
instance (b ~ HTrue) => HEq x x b
instance (b ~ HFalse) => HEq x y b


g :: (HEq a b HFalse) => a -> b -> ()
g x y = ()

Now the function g only accepts a and b if they have different types. Is this the 'idiomiatic' way to code type inequality in haskell? If not, what are the problems with it?

解决方案

With new features being added to GHC, you'll be able to write:

{-# LANGUAGE DataKinds, PolyKinds, TypeFamilies #-}

type family Equal (a :: k) (b :: k) :: Bool
type instance where
   Equal a a = True
   Equal a b = False

这篇关于'惯性'的Haskell型不等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆