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
open Lang

module Make (M : sig
  val get_old_name : string -> string

  module Error : sig
    exception Scope_too_large of string
  end
end) =
struct
  let is_used id e =
    let rec is_used = function
      | Const (Var x) when x = id ->
          true
      | Const _ ->
          false
      | Apply (e1, e2) ->
          is_used e1 || is_used e2
      | Bind (_, e1, e2) ->
          is_used e1 || is_used e2
      | Abstract (_, _, e) ->
          is_used e
      | If_then_else (cond, e1, e2) ->
          is_used cond || is_used e1 || is_used e2
      | Match _ ->
          failwith "TODO"
    in
    is_used e

  let is_used_directly id e =
    let is_used_directly = function
      | Const _ as e ->
          is_used id e
      | Bind (_, _, e2) ->
          let ue2 = is_used id e2 in
          ue2
      | Abstract (_, _, _) ->
          false
      | Apply (e1, e2) ->
          let ue1 = is_used id e1 in
          let ue2 = is_used id e2 in
          ue1 && ue2
      | If_then_else (cond, e1, e2) ->
          let ucond = is_used id cond in
          let ue1 = is_used id e1 in
          let ue2 = is_used id e2 in
          (ucond && ue1) || (ue1 && ue2) || (ue2 && ucond)
      | Match _ ->
          true
      (* TODO *)
    in
    is_used_directly e

  let rec expr = function
    | Const _ ->
        ()
    | Bind (id, e1, e2) ->
        if not (is_used_directly id e2) then
          raise @@ M.Error.Scope_too_large (M.get_old_name id) ;
        expr_list [e1; e2]
    | Abstract (_, _, e) ->
        expr e
    | Apply (e1, e2) ->
        expr_list [e1; e2]
    | If_then_else (cond, e1, e2) ->
        expr_list [cond; e1; e2]
    | Match (_origin, e, cases) ->
        expr_list (e :: List.map snd cases)

  and expr_list l = List.iter expr l
end