package dolmen

  1. Overview
  2. Docs

Source file view.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

(* This file is free software, part of dolmen. See file "LICENSE" for more information *)

(* Extended views *)
(* ************************************************************************* *)

module Assoc(V : Dolmen_intf.View.TFF.S) = struct

  let rec ty_left_assoc top_head = function
    | [] -> []
    | (t :: r) as args ->
      begin match V.Ty.view ~expand:false t with
        | App(h, h_args) when top_head h ->
          ty_left_assoc top_head (h_args @ r)
        | _ -> args
      end

  let ty_right_assoc top_head args =
    let rec aux top_head = function
      | [] -> []
      | (t :: r) as rev_args ->
        begin match V.Ty.view ~expand:false t with
          | App(h, h_args) when top_head h ->
            aux top_head (List.rev_append h_args r)
          | _ -> List.rev rev_args
        end
    in
    aux top_head (List.rev args)

  let rec term_left_assoc top_head = function
    | [] -> []
    | (t :: r) as args ->
      begin match V.Term.view t with
        | App(h, _, h_args) when top_head h ->
          term_left_assoc top_head (h_args @ r)
        | _ -> args
      end

  let term_right_assoc top_head args =
    let rec aux top_head = function
      | [] -> []
      | (t :: r) as rev_args ->
        begin match V.Term.view t with
          | App(h, _, h_args) when top_head h ->
            aux top_head (List.rev_append h_args r)
          | _ -> List.rev rev_args
        end
    in
    aux top_head (List.rev args)

  let term_chainable top_head args =
    let rec aux top_head acc = function
      | [] -> Some (List.rev acc)
      | t :: r ->
        begin match V.Term.view t with
          | App(h, _, [a; b]) when top_head h ->
            begin match acc with
              | [] -> aux top_head [b; a] r
              | x :: _ ->
                if V.Term.equal x a
                then aux top_head (b :: acc) r
                else None
            end
          | _ -> None
        end
    in
    aux top_head [] args

end