我在使用 ocaml 多态函数时遇到了一些麻烦

问题描述

我需要你的帮助,我的代码错误在哪里?

<IfModule mod_rewrite.c>
## Turn on rewrite engine
RewriteEngine on
## Coupons CMS v7
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((.*?)(\-(\d+))?)([.]\w+)?$ index.PHP?lcp=$1&lcp_id=$4&ext=$5 [QSA,L]
</IfModule>

错误

let create = Array.make_matrix 10 10;;

let assoc int = create int,create (char_of_int int);;

解决方法

当你在 Ocaml 上隐式定义一个多态函数时,它有一个“弱类型”,这意味着一个类型在你调用一次后肯定会分配给该函数,所以因为你在 int 上调用了 create,它现在具有 int -> int 类型的数组,并且不接受 char 作为参数。

,

这是“价值限制”。您可以通过像这样定义 create 来使其工作:

let create v = Array.make_matrix 10 10 v

它是这样工作的:

# let create v = Array.make_matrix 10 10 v;;
val create : 'a -> 'a array array = <fun>

# let assoc int = create int,create (char_of_int int);;
val assoc : int -> int array array * char array array = <fun>

值限制规定只有“值”(在某种意义上)可以是多态的;特别是,仅应用函数的表达式不能是完全多态的。您可以通过将函数应用于某些值来定义 create,因此值限制可防止它成为多态。上面的定义将 create 定义为 lambda(OCaml 中的 fun),它是每个值限制的“值”。所以它可以是完全多态的。

您可以在 Chapter 5 of the OCaml manual 中了解值限制。