package containers

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file CCZipper.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

(* This file is free software, part of containers. See file "license" for more details. *)

(** {1 List Zipper} *)

type 'a t = 'a list * 'a list

let empty = [], []

let is_empty = function
  | [], [] -> true
  | _ -> false

(*$T
  (is_empty empty)
  not ([42] |> make |> right |> is_empty)
*)

let to_list (l,r) = List.rev_append l r

let to_rev_list (l,r) = List.rev_append r l

(*$inject
  let zip_gen = Q.(pair (small_list int)(small_list int))
*)

(*$Q
  zip_gen (fun z -> \
    to_list z = List.rev (to_rev_list z))
*)

let make l = [], l

let left = function
  | x::l, r -> l, x::r
  | [], r -> [], r

let left_exn = function
  | x::l, r -> l, x::r
  | [], _ -> invalid_arg "zipper.left_exn"

let right = function
  | l, x::r -> x::l, r
  | l, [] -> l, []

let right_exn = function
  | l, x::r -> x::l, r
  | _, [] -> invalid_arg "zipper.right_exn"

let modify f z = match z with
  | l, [] ->
    begin match f None with
      | None -> z
      | Some x -> l, [x]
    end
  | l, x::r ->
    begin match f (Some x) with
      | None -> l,r
      | Some _ -> l, x::r
    end

let is_focused = function
  | _, _::_ -> true
  | _, [] -> false

let focused = function
  | _, x::_ -> Some x
  | _, [] -> None

(*$Q
  zip_gen (fun g -> \
    is_focused g = (focused g |> CCOpt.is_some))
*)

let focused_exn = function
  | _, x::_ -> x
  | _, [] -> raise Not_found

let insert x (l,r) = l, x::r

let remove (l,r) = match r with
  | [] -> l, []
  | _ :: r' -> l, r'

(*$Q
  Q.(triple int (list small_int)(list small_int)) (fun (x,l,r) -> \
    insert x (l,r) |> remove = (l,r))
*)

let drop_before (_, r) = [], r

let drop_after (l, r) = match r with
  | [] -> l, []
  | x :: _ -> l, [x]

let drop_after_and_focused (l, _) = l, []

(*$=
  ([1], [2]) (drop_after ([1], [2;3]))
  ([1], []) (drop_after ([1], []))
  ([1], []) (drop_after_and_focused ([1], [2;3]))
*)
OCaml

Innovation. Community. Security.