Skip to content

Commit

Permalink
feat(Trie): add type alias "untagged"
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Sep 30, 2022
1 parent 4671c83 commit 031ac7a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Trie.ml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ let of_seq s = of_seq_with_merger ~prefix:Emp (fun _ _ y -> y) s

(** {1 Tags} *)

type 'data untagged = ('data, unit) t

let[@inline] retag t : _ t -> _ t =
function
| None -> None
Expand Down
3 changes: 3 additions & 0 deletions src/Trie.mli
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ val of_seq_with_merger : ?prefix:bwd_path -> (bwd_path -> 'data * 'tag -> 'data

(** {1 Tags} *)

(** Untagged tries (where all tags are [()]). *)
type 'data untagged = ('data, unit) t

(** [retag tag t] changes all tags within [t] to [tag] in O(1) time. The data remain intact. *)
val retag : 'tag -> ('data, _) t -> ('data, 'tag) t

Expand Down
2 changes: 0 additions & 2 deletions src/UntaggedTrie.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
(** A convenience module {!module:UntaggedTrie} derived from {!module:Trie} with the type of tags being {!type:unit}. *)

open Trie

type path = Trie.path
Expand Down
4 changes: 3 additions & 1 deletion src/UntaggedTrie.mli
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(** @canonical Trie.Untagged *)

(** A convenience module {!module:Trie.Untagged} derived from {!module:Trie} for untagged tries {!type:Trie.untagged}. *)

type path = Trie.path

type bwd_path = Trie.bwd_path

type 'data t = ('data, unit) Trie.t
type 'data t = 'data Trie.untagged
(** @canonical Trie.Untagged.t *)

val empty : 'data t
Expand Down
7 changes: 5 additions & 2 deletions test/Example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ let rec interpret_decl : decl -> unit =
| Export p ->
S.export_visible (Language.only p)
| Section (p, sec) ->
S.section p @@ fun () -> List.iter interpret_decl sec
S.section p @@ fun () -> interpret_section sec

and interpret_section section =
List.iter interpret_decl section

let interpret (prog : program) =
let open Handler in
S.run ~shadow ~not_found ~hook @@ fun () ->
List.iter interpret_decl prog
interpret_section prog

(* Some code in action *)
let () = interpret [
Expand Down
1 change: 1 addition & 0 deletions test/ListAsTrie.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ let to_seq_values l = Seq.map snd @@ List.to_seq l
let of_seq s = Seq.fold_left (union_singleton ~prefix:Emp (fun _ _ y -> y)) empty s
let of_seq_with_merger ?(prefix=Emp) m s = Seq.fold_left (union_singleton ~prefix m) empty s

type 'a untagged = (path * ('a * unit)) list
let retag t l = List.map (fun (p, (d, _)) -> p, (d, t)) l
let untag l = retag () l
let retag_subtree pre t l =
Expand Down

0 comments on commit 031ac7a

Please # to comment.