This commit is contained in:
parent
aa2ff7b2f0
commit
2f3113f55d
11742 changed files with 1223940 additions and 0 deletions
15
unikernel/duniverse/seq/src/dune
Normal file
15
unikernel/duniverse/seq/src/dune
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(executable
|
||||
(modules select_version)
|
||||
(name select_version))
|
||||
|
||||
(rule
|
||||
(targets seq.ml seq.mli)
|
||||
(deps seq_redef.ml seq_redef.mli seq_alias.ml seq_alias.mli)
|
||||
(action
|
||||
(progn
|
||||
(with-stdout-to seq.mli (run ./select_version.exe intf))
|
||||
(with-stdout-to seq.ml (run ./select_version.exe impl)))))
|
||||
|
||||
(library
|
||||
(modules seq)
|
||||
(public_name seq))
|
||||
16
unikernel/duniverse/seq/src/select_version.ml
Normal file
16
unikernel/duniverse/seq/src/select_version.ml
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
let () =
|
||||
let version =
|
||||
Scanf.sscanf Sys.ocaml_version "%d.%d" (fun major minor -> (major, minor))
|
||||
in
|
||||
let basename = if version < (4, 07) then "seq_redef" else "seq_alias" in
|
||||
let file =
|
||||
match Sys.argv.(1) with
|
||||
| "intf" -> basename ^ ".mli"
|
||||
| "impl" -> basename ^ ".ml"
|
||||
| _ -> assert false
|
||||
in
|
||||
let ic = open_in_bin file in
|
||||
let length = in_channel_length ic in
|
||||
let content = really_input_string ic length in
|
||||
close_in ic;
|
||||
print_string content
|
||||
1
unikernel/duniverse/seq/src/seq_alias.ml
Normal file
1
unikernel/duniverse/seq/src/seq_alias.ml
Normal file
|
|
@ -0,0 +1 @@
|
|||
include Stdlib.Seq
|
||||
2
unikernel/duniverse/seq/src/seq_alias.mli
Normal file
2
unikernel/duniverse/seq/src/seq_alias.mli
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
include module type of Stdlib.Seq
|
||||
73
unikernel/duniverse/seq/src/seq_redef.ml
Normal file
73
unikernel/duniverse/seq/src/seq_redef.ml
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
(**************************************************************************)
|
||||
(* *)
|
||||
(* OCaml *)
|
||||
(* *)
|
||||
(* Simon Cruanes *)
|
||||
(* *)
|
||||
(* Copyright 2017 Institut National de Recherche en Informatique et *)
|
||||
(* en Automatique. *)
|
||||
(* *)
|
||||
(* All rights reserved. This file is distributed under the terms of *)
|
||||
(* the GNU Lesser General Public License version 2.1, with the *)
|
||||
(* special exception on linking described in the file LICENSE. *)
|
||||
(* *)
|
||||
(**************************************************************************)
|
||||
|
||||
(* Module [Seq]: functional iterators *)
|
||||
|
||||
type +'a node =
|
||||
| Nil
|
||||
| Cons of 'a * 'a t
|
||||
|
||||
and 'a t = unit -> 'a node
|
||||
|
||||
let empty () = Nil
|
||||
|
||||
let return x () = Cons (x, empty)
|
||||
|
||||
let rec map f seq () = match seq() with
|
||||
| Nil -> Nil
|
||||
| Cons (x, next) -> Cons (f x, map f next)
|
||||
|
||||
let rec filter_map f seq () = match seq() with
|
||||
| Nil -> Nil
|
||||
| Cons (x, next) ->
|
||||
match f x with
|
||||
| None -> filter_map f next ()
|
||||
| Some y -> Cons (y, filter_map f next)
|
||||
|
||||
let rec filter f seq () = match seq() with
|
||||
| Nil -> Nil
|
||||
| Cons (x, next) ->
|
||||
if f x
|
||||
then Cons (x, filter f next)
|
||||
else filter f next ()
|
||||
|
||||
let rec flat_map f seq () = match seq () with
|
||||
| Nil -> Nil
|
||||
| Cons (x, next) ->
|
||||
flat_map_app f (f x) next ()
|
||||
|
||||
(* this is [append seq (flat_map f tail)] *)
|
||||
and flat_map_app f seq tail () = match seq () with
|
||||
| Nil -> flat_map f tail ()
|
||||
| Cons (x, next) ->
|
||||
Cons (x, flat_map_app f next tail)
|
||||
|
||||
let fold_left f acc seq =
|
||||
let rec aux f acc seq = match seq () with
|
||||
| Nil -> acc
|
||||
| Cons (x, next) ->
|
||||
let acc = f acc x in
|
||||
aux f acc next
|
||||
in
|
||||
aux f acc seq
|
||||
|
||||
let iter f seq =
|
||||
let rec aux seq = match seq () with
|
||||
| Nil -> ()
|
||||
| Cons (x, next) ->
|
||||
f x;
|
||||
aux next
|
||||
in
|
||||
aux seq
|
||||
75
unikernel/duniverse/seq/src/seq_redef.mli
Normal file
75
unikernel/duniverse/seq/src/seq_redef.mli
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
(**************************************************************************)
|
||||
(* *)
|
||||
(* OCaml *)
|
||||
(* *)
|
||||
(* Simon Cruanes *)
|
||||
(* *)
|
||||
(* Copyright 2017 Institut National de Recherche en Informatique et *)
|
||||
(* en Automatique. *)
|
||||
(* *)
|
||||
(* All rights reserved. This file is distributed under the terms of *)
|
||||
(* the GNU Lesser General Public License version 2.1, with the *)
|
||||
(* special exception on linking described in the file LICENSE. *)
|
||||
(* *)
|
||||
(**************************************************************************)
|
||||
|
||||
(* Module [Seq]: functional iterators *)
|
||||
|
||||
(** {1 Functional Iterators} *)
|
||||
|
||||
(** The type ['a t] is a {b delayed list}, i.e. a list where some evaluation
|
||||
is needed to access the next element. This makes it possible to build
|
||||
infinite sequences, to build sequences as we traverse them, and to transform
|
||||
them in a lazy fashion rather than upfront.
|
||||
*)
|
||||
|
||||
type 'a t = unit -> 'a node
|
||||
(** The type of delayed lists containing elements of type ['a].
|
||||
Note that the concrete list node ['a node] is delayed under a closure,
|
||||
not a [lazy] block, which means it might be recomputed every time
|
||||
we access it. *)
|
||||
|
||||
and +'a node =
|
||||
| Nil
|
||||
| Cons of 'a * 'a t
|
||||
(** A fully-evaluated list node, either empty or containing an element
|
||||
and a delayed tail. *)
|
||||
|
||||
val empty : 'a t
|
||||
(** The empty sequence, containing no elements. *)
|
||||
|
||||
val return : 'a -> 'a t
|
||||
(** The singleton sequence containing only the given element. *)
|
||||
|
||||
val map : ('a -> 'b) -> 'a t -> 'b t
|
||||
(** [map f seq] returns a new sequence whose elements are the elements of
|
||||
[seq], transformed by [f].
|
||||
This transformation is lazy, it only applies when the result is traversed.
|
||||
|
||||
If [seq = [1;2;3]], then [map f seq = [f 1; f 2; f 3]]. *)
|
||||
|
||||
val filter : ('a -> bool) -> 'a t -> 'a t
|
||||
(** Remove from the sequence the elements that do not satisfy the
|
||||
given predicate.
|
||||
This transformation is lazy, it only applies when the result is traversed. *)
|
||||
|
||||
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
||||
(** Apply the function to every element; if [f x = None] then [x] is dropped;
|
||||
if [f x = Some y] then [y] is returned.
|
||||
This transformation is lazy, it only applies when the result is traversed. *)
|
||||
|
||||
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
|
||||
(** Map each element to a subsequence, then return each element of this
|
||||
sub-sequence in turn.
|
||||
This transformation is lazy, it only applies when the result is traversed. *)
|
||||
|
||||
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
|
||||
(** Traverse the sequence from left to right, combining each element with the
|
||||
accumulator using the given function.
|
||||
The traversal happens immediately and will not terminate on infinite sequences.
|
||||
|
||||
Also see {!List.fold_left} *)
|
||||
|
||||
val iter : ('a -> unit) -> 'a t -> unit
|
||||
(** Iterate on the sequence, calling the (imperative) function on every element.
|
||||
The traversal happens immediately and will not terminate on infinite sequences. *)
|
||||
Loading…
Add table
Add a link
Reference in a new issue