在纯函数 ML 中进行 N 维行走?

发布于 2024-07-25 17:56:12 字数 1438 浏览 4 评论 0原文

这个想法是遍历多个维度,每个维度都定义为一个范围

(* lower_bound, upper_bound, number_of_steps *)
type range = real * real * int

,因此功能类似于 fun foo y xfun foo zy x 可以应用于整个正方形 XY 或立方体 XY*Z。

SML/NJ 不喜欢我下面的实现:

test2.sml:7.5-22.6 Error: right-hand-side of clause doesn't agree with function result type [circularity]
  expression:  (real -> 'Z) -> unit
  result type:  'Z -> 'Y
  in declaration:
    walk = (fn arg => (fn <pat> => <exp>))

这是代码:

fun walk []      _ = ()
  | walk (r::rs) f =
  let
    val (k0, k1, n) = r
    val delta = k1 - k0
    val step = delta / real n

    fun loop 0 _ = ()
      | loop i k = 
        let in
          walk rs (f k) ;          (* Note (f k) "eats" the first argument.
                                      I guess SML doesn't like having the
                                      type of walk change in the middle of its
                                      definition *)
          loop (i - 1) (k + step)
        end
  in
    loop n k0
  end

fun do2D y x = (* ... *) ()
fun do3D z y x = (* ... *) ()

val x_axis = (0.0, 1.0, 10)
val y_axis = (0.0, 1.0, 10)
val z_axis = (0.0, 1.0, 10)

val _ = walk [y_axis, x_axis] do2D
val _ = walk [z_axis, y_axis, x_axis] do3D

这种构造是否可能?

欢迎任何指针。

The idea is to walk over multiple dimensions, each one defined as a range

(* lower_bound, upper_bound, number_of_steps *)
type range = real * real * int

so functions like fun foo y x or fun foo z y x could be applied to the whole square XY or cube XY*Z.

SML/NJ doesn't like my implementation below :

test2.sml:7.5-22.6 Error: right-hand-side of clause doesn't agree with function result type [circularity]
  expression:  (real -> 'Z) -> unit
  result type:  'Z -> 'Y
  in declaration:
    walk = (fn arg => (fn <pat> => <exp>))

Here's the code :

fun walk []      _ = ()
  | walk (r::rs) f =
  let
    val (k0, k1, n) = r
    val delta = k1 - k0
    val step = delta / real n

    fun loop 0 _ = ()
      | loop i k = 
        let in
          walk rs (f k) ;          (* Note (f k) "eats" the first argument.
                                      I guess SML doesn't like having the
                                      type of walk change in the middle of its
                                      definition *)
          loop (i - 1) (k + step)
        end
  in
    loop n k0
  end

fun do2D y x = (* ... *) ()
fun do3D z y x = (* ... *) ()

val x_axis = (0.0, 1.0, 10)
val y_axis = (0.0, 1.0, 10)
val z_axis = (0.0, 1.0, 10)

val _ = walk [y_axis, x_axis] do2D
val _ = walk [z_axis, y_axis, x_axis] do3D

Is this kind of construct even possible ?

Any pointer welcomed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔书几行 2024-08-01 17:56:12

walk 可以在机器学习的类型系统中表达吗?

val walk : range list -> (real -> real -> unit) -> unit
val walk : range list -> (real -> real -> real -> unit) -> unit

ML 中的这两种类型不可能存在相同的值。


不过,我们可以轻松地为每种所需类型生成值。

type range = real * real * int

signature WALK =
  sig
    type apply
    val walk : range list -> apply -> unit
  end

structure Walk0 : WALK =
  struct
    type apply = unit
    fun walk _ _ = ()
  end

functor WALKF (Walk : WALK) : WALK =
  struct
    type apply = real -> Walk.apply
    fun walk ((low, high, steps)::rs) f =
          let fun loop i =
                if i > steps then () else
                  let val x = low + (high - low) * real i / real steps
                  in (Walk.walk rs (f x); loop (i + 1)) end
          in loop 0 end
  end

struture Walk1 = WALKF(Walk0)
struture Walk2 = WALKF(Walk1)
struture Walk3 = WALKF(Walk2)

这样,以下值就存在于所需的类型中。

val Walk0.walk : range list -> unit -> unit
val Walk1.walk : range list -> (real -> unit) -> unit
val Walk2.walk : range list -> (real -> real -> unit) -> unit
val Walk3.walk : range list -> (real -> real -> real -> unit) -> unit

那么你只需要编写

val _ = Walk2.walk [y_axis, x_axis] do2D
val _ = Walk3.walk [z_axis, y_axis, x_axis] do3D

To use the same walk for every Dimensionality,你需要它为每个维度使用相同的类型。

fun walk nil f = f nil
  | walk ((low, high, steps)::rs) f =
      let fun loop i =
            if i > steps then () else
              let val x = low + (high - low) * real i / real steps
              in (walk rs (fn xs -> f (x::xs)); loop (i + 1)) end
      in loop 0 end

因为类型更改为

val walk : range list -> (real list -> unit) -> unit

您的用法也必须更改为

fun do2D [y,x] = (* ... *) ()
fun do3D [z,y,x] = (* ... *) ()

Is walk expressible in ML's type system?

val walk : range list -> (real -> real -> unit) -> unit
val walk : range list -> (real -> real -> real -> unit) -> unit

The same one value cannot possibly exist with both those types in ML.


We can easily generate values for each of the desired types, though.

type range = real * real * int

signature WALK =
  sig
    type apply
    val walk : range list -> apply -> unit
  end

structure Walk0 : WALK =
  struct
    type apply = unit
    fun walk _ _ = ()
  end

functor WALKF (Walk : WALK) : WALK =
  struct
    type apply = real -> Walk.apply
    fun walk ((low, high, steps)::rs) f =
          let fun loop i =
                if i > steps then () else
                  let val x = low + (high - low) * real i / real steps
                  in (Walk.walk rs (f x); loop (i + 1)) end
          in loop 0 end
  end

struture Walk1 = WALKF(Walk0)
struture Walk2 = WALKF(Walk1)
struture Walk3 = WALKF(Walk2)

With this, the following values exist with the desired types.

val Walk0.walk : range list -> unit -> unit
val Walk1.walk : range list -> (real -> unit) -> unit
val Walk2.walk : range list -> (real -> real -> unit) -> unit
val Walk3.walk : range list -> (real -> real -> real -> unit) -> unit

Then you only need to write

val _ = Walk2.walk [y_axis, x_axis] do2D
val _ = Walk3.walk [z_axis, y_axis, x_axis] do3D

To use the same walk for every dimensionality, you need it to use the same type for every dimensionality.

fun walk nil f = f nil
  | walk ((low, high, steps)::rs) f =
      let fun loop i =
            if i > steps then () else
              let val x = low + (high - low) * real i / real steps
              in (walk rs (fn xs -> f (x::xs)); loop (i + 1)) end
      in loop 0 end

Because the type is changed to

val walk : range list -> (real list -> unit) -> unit

your usage also has to change to

fun do2D [y,x] = (* ... *) ()
fun do3D [z,y,x] = (* ... *) ()
好菇凉咱不稀罕他 2024-08-01 17:56:12
fun walk lst f = let
  fun aux rev_prefix [] = f (rev rev_prefix)
    | aux rev_prefix (r::rs) = let
        val (k0, k1, n) = r
        val delta = k1 - k0
        val step = delta / real n

        fun loop 0 _ = ()
          | loop i k = (
              aux (k+step :: rev_prefix) rs;
              loop (i - 1) (k + step)
            )
      in
        loop n k0
      end
in
  aux [] lst
end

fun do2D [x,y] = print (Real.toString x ^ "\t" ^
                        Real.toString y ^ "\n")
fun do3D [x,y,z] = print (Real.toString x ^ "\t" ^
                          Real.toString y ^ "\t" ^
                          Real.toString z ^ "\n")

val x_axis = (0.0, 1.0, 10)
val y_axis = (0.0, 1.0, 10)
val z_axis = (0.0, 1.0, 10)

val () = walk [y_axis, x_axis] do2D
val () = walk [z_axis, y_axis, x_axis] do3D
fun walk lst f = let
  fun aux rev_prefix [] = f (rev rev_prefix)
    | aux rev_prefix (r::rs) = let
        val (k0, k1, n) = r
        val delta = k1 - k0
        val step = delta / real n

        fun loop 0 _ = ()
          | loop i k = (
              aux (k+step :: rev_prefix) rs;
              loop (i - 1) (k + step)
            )
      in
        loop n k0
      end
in
  aux [] lst
end

fun do2D [x,y] = print (Real.toString x ^ "\t" ^
                        Real.toString y ^ "\n")
fun do3D [x,y,z] = print (Real.toString x ^ "\t" ^
                          Real.toString y ^ "\t" ^
                          Real.toString z ^ "\n")

val x_axis = (0.0, 1.0, 10)
val y_axis = (0.0, 1.0, 10)
val z_axis = (0.0, 1.0, 10)

val () = walk [y_axis, x_axis] do2D
val () = walk [z_axis, y_axis, x_axis] do3D
何处潇湘 2024-08-01 17:56:12

找到了这个可变数量参数的实现。 不确定它是否适用,但它看起来很丑陋。

Found this implementation for variable number of arguments. Not sure it applies but it looks quite ugly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文