如果我知道运行时是真的,该如何告诉GHC满足类型级别<=约束?

问题描述

我有一个由自然数n参数化的类型:

data MyType (n :: Nat) = MyType

这种类型的操作仅在n > 0时才有意义,因此我将其指定为约束:

myFunction :: (KnownNat n,1 <= n) => MyType n -> MyType n
myFunction = id

(请注意,这些功能的实际版本 do 通过将n转换为natVal来使用SomeMyType。)

我想创建一个存在性类型(n),让我们在运行时选择一个data SomeMyType where SomeMyType :: forall n. (KnownNat n,1 <= n) => MyType n -> SomeMyType

SomeMyType

要产生someMyTypeVal的值,我正在编写一个someNatVal的函数,其作用类似于someMyTypeVal :: Integer -> Maybe SomeMyType someMyTypeVal n | n > 0 = case someNatVal n of Just (SomeNat (_ :: p n)) -> Just (SomeMyType (MyType @n)) Nothing -> Nothing | otherwise = Nothing

1 <= n

这在没有• Couldn't match type ‘1 <=? n’ with ‘'True’ arising from a use of ‘SomeMyType’ 约束的情况下可以很好地工作,但是在约束条件下,我收到以下类型错误:

n > 0

如何解决此限制?由于我在运行时检查过unsafeCoerce,因此我不介意在此处使用类似1 <= n的操作来说服GHC unsafeCoerce是正确的,但我不能 just 使用n,因为那样会丢失<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.drawerlayout.widget.DrawerLayout android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <fragment android:id="@+id/myNavHostFragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/navigation" /> </LinearLayout> <com.google.android.material.navigation.NavigationView android:id="@+id/navView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/navdrawer_menu" app:headerLayout="@layout/nav_header"/> </androidx.drawerlayout.widget.DrawerLayout> 的类型级别的值。

处理此问题的最佳方法是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)