鉴于 OCaml 中流类型的这些类型声明,如何编写展平函数?
Given these type declarations in OCaml for a stream type, how to I write a flatten function?
exception Empty_list
type 'a stream = Stream of 'a * (unit -> 'a stream) | Nil
let shead s = match s with
| Stream (a, _) -> a
| Nil -> raise Empty_list
let stail s = match s with
| Stream (_, s) -> s ()
| Nil -> raise Empty_list
let stream_to_list s =
let rec stream_to_list s accumulator = match s with
| Nil -> accumulator
| Stream (_, _) -> stream_to_list (stail s) (shead s :: accumulator)
in List.rev (stream_to_list s [])
flatten 函数将采用一组任意嵌套的流,并生成一个完全扁平化的版本,这是嵌套结构中序遍历的结果,仅包含叶子。这意味着 shead
对这个新事物的每个成员的结果将 return 不是流的东西。
想法是 stream_to_list (flatten s)
会返回一个列表,其中没有元素是流。
您想要的函数类型错误,不能存在于像 OCaml 这样的参数多态性语言中:'a stream -> 'b stream where 'b is not a stream
不是有效类型。
参数多态性要求多态函数不会改变其参数类型的函数行为。这在语义方面很有用,一旦完成类型检查就可以擦除类型,并且出于类型理论原因:证明(又名程序)无法检查他们试图证明的定理(又名类型)。
如果您想多次展平嵌套流,有两种选择:
- 如果嵌套级别静态已知,您可以多次使用
flatten
。注意,用代码的大小 来平展一些时间指数是很直接的
let flatten16 x =
let flatten2 x = flatten (flatten x) in
let flatten4 x = flatten2 (flatten2 x) in
let flatten8 x = flatten4 (flatten4 x) in
flatten8 (flatten8 x)
这在实践中完全足够了,因为类型不会在人类编写的代码中呈指数增长。
- 如果嵌套级别是任意的,则嵌套级别需要在值级别上可见(构造流已经需要)。这可以通过以下变体类型实现:
type 'a nested_seq = Seq of 'a Seq.t | Nested of 'a Seq.t nested_seq
(您的类型等同于标准库中的 Seq.t
)
然后将 'a nested_seq
扁平化为 'a Seq.t
是一个明确定义的概念,可以通过一些多态递归来完成:
let rec flatten: 'a. 'a nested_seq -> 'a Seq.t = fun x ->
match x with
| Seq x -> x
| Nested s -> Seq.flat_map Fun.id (flatten s)
(* or `Seq.concat (flatten s)` in OCaml 4.13 or with a sequence libray *)
旁白:shead
和 stail
是反模式:为了安全地使用这些函数,您需要模式匹配它们未来的参数,并且它们在每个分支中丢弃匹配的值。
换句话说,
let head s =
match s with
| Nil -> None
| Stream _ -> Some (shead s)
比
更不清晰也更不安全
let head s =
match s with
| Nil -> None
| Stream (s, _) -> Some s
(选项函数对于与通用选项函数组合很有用(例如来自选项 monad))。
exception Empty_list
type 'a stream = Stream of 'a * (unit -> 'a stream) | Nil
let shead s = match s with
| Stream (a, _) -> a
| Nil -> raise Empty_list
let stail s = match s with
| Stream (_, s) -> s ()
| Nil -> raise Empty_list
let stream_to_list s =
let rec stream_to_list s accumulator = match s with
| Nil -> accumulator
| Stream (_, _) -> stream_to_list (stail s) (shead s :: accumulator)
in List.rev (stream_to_list s [])
flatten 函数将采用一组任意嵌套的流,并生成一个完全扁平化的版本,这是嵌套结构中序遍历的结果,仅包含叶子。这意味着 shead
对这个新事物的每个成员的结果将 return 不是流的东西。
想法是 stream_to_list (flatten s)
会返回一个列表,其中没有元素是流。
您想要的函数类型错误,不能存在于像 OCaml 这样的参数多态性语言中:'a stream -> 'b stream where 'b is not a stream
不是有效类型。
参数多态性要求多态函数不会改变其参数类型的函数行为。这在语义方面很有用,一旦完成类型检查就可以擦除类型,并且出于类型理论原因:证明(又名程序)无法检查他们试图证明的定理(又名类型)。
如果您想多次展平嵌套流,有两种选择:
- 如果嵌套级别静态已知,您可以多次使用
flatten
。注意,用代码的大小 来平展一些时间指数是很直接的
let flatten16 x =
let flatten2 x = flatten (flatten x) in
let flatten4 x = flatten2 (flatten2 x) in
let flatten8 x = flatten4 (flatten4 x) in
flatten8 (flatten8 x)
这在实践中完全足够了,因为类型不会在人类编写的代码中呈指数增长。
- 如果嵌套级别是任意的,则嵌套级别需要在值级别上可见(构造流已经需要)。这可以通过以下变体类型实现:
type 'a nested_seq = Seq of 'a Seq.t | Nested of 'a Seq.t nested_seq
(您的类型等同于标准库中的 Seq.t
)
然后将 'a nested_seq
扁平化为 'a Seq.t
是一个明确定义的概念,可以通过一些多态递归来完成:
let rec flatten: 'a. 'a nested_seq -> 'a Seq.t = fun x ->
match x with
| Seq x -> x
| Nested s -> Seq.flat_map Fun.id (flatten s)
(* or `Seq.concat (flatten s)` in OCaml 4.13 or with a sequence libray *)
旁白:shead
和 stail
是反模式:为了安全地使用这些函数,您需要模式匹配它们未来的参数,并且它们在每个分支中丢弃匹配的值。
换句话说,
let head s =
match s with
| Nil -> None
| Stream _ -> Some (shead s)
比
更不清晰也更不安全let head s =
match s with
| Nil -> None
| Stream (s, _) -> Some s
(选项函数对于与通用选项函数组合很有用(例如来自选项 monad))。