package acgtk

  1. Overview
  2. Docs
Abstract Categorial Grammar development toolkit

Install

Dune Dependency

Authors

Maintainers

Sources

acg-2.1.0-20240219.tar.gz
sha512=5d380a947658fb1201895cb4cb449b1f60f54914c563e85181d628a89f045c1dd7b5b2226bb7865dd090f87caa9187e0ea6c7a4ee3dc3dda340d404c4e76c7c2

doc/src/acgtk.containers/weight.ml.html

Source file weight.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module type Weight =
sig
  type w
  val pp : Format.formatter -> w -> unit
  val init : w

  val is_better : w -> w -> bool
  (** [is better w1 w2] returns [true] if [w1] is strictly better
      than [w2] *)

  val is_equal : w -> w -> bool
  (** [is better w1 w2] returns [true] if [w1=w2] *)
    
  val up : w -> 'a -> w 
  val down : w -> 'a -> w
  val right : w -> 'a -> w
end


module type Weight_sig =
sig
  include Weight
  module WMap : 
  sig
    type 'a t
    val empty : 'a t
    val optimum : 'a t -> w option
    val pp : Format.formatter -> 'a t -> unit
    val add : w -> 'a -> 'a t -> 'a t
    val pop_optimum : 'a t -> ('a * w * 'a t) option
  end
end

module MapMake(W:sig include Weight val compare : w -> w -> int end)=
struct
  type w = W.w
  let pp = W.pp
  let init = W.init
  let is_better = W.is_better
  let is_equal = W.is_equal
  let up = W.up
  let down = W.down
  let right = W.right
                
  module LocalMap = Map.Make (struct type t = W.w let compare = W.compare end)

  module WMap =
    struct
      type 'a t = (W.w * ('a list LocalMap.t)) option
      let empty = None
        
      let optimum = function
        | None -> None
        | Some (w, _) -> Some w
                           
      let pp fmt = function
        | None -> Format.fprintf fmt "None"
        | Some (w, map) ->
          let pp_map fmt m =
            LocalMap.iter
              (fun k v ->
                 Format.fprintf
                   fmt
                   "@[<hov>Bindings:@[ %a -> list of length %d@]@]@ "
                   W.pp
                   k
                   (List.length v))
              m in
          Format.fprintf
            fmt
            "@[Optimum set to: %a@ @[<v> @[%a@]@]@]"
            W.pp
            w
            pp_map
            map
            
      let rec remove_empty_bindings map = 
        match LocalMap.min_binding_opt map with
        | None -> None
        | Some (w', []) -> remove_empty_bindings (LocalMap.remove w' map)
        | Some (w', _) -> Some (w', map)
                            
      let add weight state map =
        match map with
        | None -> Some (weight, LocalMap.add weight [state] LocalMap.empty)
        | Some (opt, map) when W.is_better weight opt ->
          (* weight is strictly better than opt, hence no binding for
             weight is present *)
          Some (weight, LocalMap.add weight [state] map)
        | Some (opt, map) when W.is_equal weight opt ->
          (* weight is opt *)
          let states = LocalMap.find opt map in
          (* Shouldn't raise a Not_found exception *)
          Some (opt, LocalMap.add opt (state :: states) map)
        | Some (opt, map) ->
          (* opt is trictly better than weight *)
          let states =
            match LocalMap.find_opt weight map with
            | None -> [state]
            | Some previous_states -> state :: previous_states in
          Some (opt, LocalMap.add weight states map)
            
      let pop_optimum m =
        match m with
        | None -> None
        | Some (w_opt, map) ->
          (match LocalMap.min_binding_opt map with
           | None -> failwith "Bug: optimum is set for an empty map"
           | Some (w', _) when w' <> w_opt -> failwith "Bug: optimum is not correctly set"
           | Some (_ , [] ) -> failwith "Bug: Should not occurr"
           | Some (w', [s]) ->
             Some (s, w', remove_empty_bindings (LocalMap.remove w' map))
           | Some (w', s :: states) ->
             Some (s, w', Some (w', LocalMap.add w' states map)))
          
    end
end

module Weight_as_Depth_init =
struct
  type w = int
  let pp fmt w = Format.fprintf fmt "depth = %d" w
  let init = 1
  let is_better a b = a < b
  let is_equal a b = a=b
  let up w _ = w - 1 
  let down w _ = w + 1
  let right w _ = w
  (*  let left w _ = w *)
  let compare a b = a - b
end

module Weight_as_Depth = MapMake (Weight_as_Depth_init)

module Weight_as_Depth_and_Size_init =
struct
  type w = { current : int; max : int; size : int }
  let pp fmt w = Format.fprintf fmt "depth = %d, size = %d" w.max w.size
  let init = { current = 1; max = 1; size = 1 }
  let is_better w w' =
    match w.max - w'.max with
    | 0 ->
      (match w.size - w'.size with
       | 0 -> w.current < w'.current
       | i when i < 0 -> true
       | _ -> false)
    | i when i < 0 -> true
    | _ -> false
  let is_equal w w' = w = w'
  let up w _ = { w with current = w.current - 1 }
  let down w  _ =
    let current = w.current + 1 in
    { current  ; size = w.size + 1; max = max current w.max }
  let right w _ = { w with size = w.size + 1 }
  let compare w w' =
    match w.max - w'.max with
    | 0 ->
      (match w.size - w'.size with
       | 0 -> w.current -w'.current
       | r -> r)
    | r -> r
end

module Weight_as_Depth_and_Size = MapMake (Weight_as_Depth_and_Size_init)
OCaml

Innovation. Community. Security.