Skip to content

Commit

Permalink
feat typereg: iter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Jul 23, 2024
1 parent 472f163 commit b822c2f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/typereg/ty_def.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type record = { fields: (string * ty) list }
let map_record ~f { fields } : record =
{ fields = List.map (fun (s, ty) -> s, f ty) fields }

let iter_record ~f { fields } : unit = List.iter (fun (_, ty) -> f ty) fields

type cstor = {
c: string; (** Constructor name *)
args: ty list;
Expand All @@ -17,6 +19,7 @@ type cstor = {
[@@deriving show { with_path = false }, eq, yojson]

let map_cstor ~f (c : cstor) : cstor = { c with args = List.map f c.args }
let iter_cstor ~f c : unit = List.iter f c.args

(** Definition *)
type decl =
Expand All @@ -31,6 +34,12 @@ let map_decl ~f (d : decl) : decl =
| Alg cs -> Alg (List.map (map_cstor ~f) cs)
| Record r -> Record (map_record ~f r)

let iter_decl ~f (d : decl) : unit =
match d with
| Alias ty -> f ty
| Alg cs -> List.iter (iter_cstor ~f) cs
| Record r -> iter_record ~f r

type t = {
path: string; (** Path *)
name: string; (** Name of the type *)
Expand All @@ -42,6 +51,7 @@ type t = {
type clique = t list [@@deriving eq, yojson, show]

let map ~f (self : t) : t = { self with decl = map_decl ~f self.decl }
let iter_ty ~f (self : t) = iter_decl ~f self.decl

let compare_by_name (d1 : t) (d2 : t) =
compare (d1.path, d1.name) (d2.path, d2.name)
9 changes: 9 additions & 0 deletions src/typereg/ty_expr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ let map_shallow ~f (self : t) =
| Arrow (lbl, a, b) -> Arrow (lbl, f a, f b)
| Tuple l -> Tuple (List.map f l)

let iter_shallow ~f (self : t) =
match self with
| Var _ -> ()
| Cstor (s, l) -> List.iter f l
| Arrow (lbl, a, b) ->
f a;
f b
| Tuple l -> List.iter f l

let var v : t = Var v
let cstor c l : t = Cstor (c, l)
let arrow ?label a b : t = Arrow (label, a, b)
Expand Down

0 comments on commit b822c2f

Please sign in to comment.