| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package flow |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "maps" |
| 10 | ) |
| 11 | |
| 12 | // A Semilattice describes a bounded semilattice over Elem. |
| 13 | // That is, a partial order over values of type Elem, with a binary |
| 14 | // Merge operator and an identity element. |
| 15 | // |
| 16 | // This is typically implemented by a stateless type, and acts as a factory for |
| 17 | // lattice elements. |
| 18 | type Semilattice[Elem any] interface { |
| 19 | // Ident returns the identity element of this lattice, that is the unit of |
| 20 | // the Merge operation. |
| 21 | Ident() Elem |
| 22 | |
| 23 | // Equals returns whether a and b are the same element. |
| 24 | Equals(a, b Elem) bool |
| 25 | |
| 26 | // Merge combines two lattice values, such as the two possible values of a |
| 27 | // variable at the end of an if/else statement. |
| 28 | // |
| 29 | // Merge must satisfy the following identities, where we use ∧ for Merge, = |
| 30 | // for Equals, and 𝟏 for Ident: |
| 31 | // |
| 32 | // - Associativity: x ∧ (y ∧ z) = (x ∧ y) ∧ z |
| 33 | // - Commutativity: x ∧ y = y ∧ x |
| 34 | // - Idempotency: x ∧ x = x |
| 35 | // - Identity: x ∧ 𝟏 = x |
| 36 | Merge(a, b Elem) Elem |
| 37 | } |
| 38 | |
| 39 | // A MapLattice implements [Semilattice][map[Key]Elem]. The values in the map |
| 40 | // are themselves defined by [Semilattice] L. |
| 41 | // |
| 42 | // Any elements missing from the map are implicitly L's identity element, and |
| 43 | // L's identity element never appears as a value in the map. |
| 44 | type MapLattice[Key comparable, Elem any, L Semilattice[Elem]] struct { |
| 45 | l L |
| 46 | } |
| 47 | |
| 48 | func (m MapLattice[Key, Elem, L]) Ident() map[Key]Elem { |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func (m MapLattice[Key, Elem, L]) Equals(a, b map[Key]Elem) bool { |
| 53 | return maps.EqualFunc(a, b, m.l.Equals) |
| 54 | } |
| 55 | |
| 56 | func (m MapLattice[Key, Elem, L]) Merge(a, b map[Key]Elem) map[Key]Elem { |
| 57 | if len(a) == 0 { |
| 58 | return b |
| 59 | } else if len(b) == 0 { |
| 60 | return a |
| 61 | } |
| 62 | |
| 63 | // We need to consider the union of keys in a and b. |
| 64 | out := make(map[Key]Elem) |
| 65 | id := m.l.Ident() |
| 66 | for k, av := range a { |
| 67 | bv, ok := b[k] |
| 68 | if !ok { |
| 69 | // Because Merge(x, Ident()) == x, we can skip calling L.Merge. |
| 70 | out[k] = av |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | w := m.l.Merge(av, bv) |
| 75 | if m.l.Equals(w, id) { |
| 76 | // In a semilattice, Merge(x, y) = Ident is only possible when x == |
| 77 | // Ident and y == Ident. |
| 78 | panic(fmt.Sprintf("%T is not a semilattice: Merge returned Ident for non-Ident arguments", m.l)) |
| 79 | } |
| 80 | out[k] = w |
| 81 | } |
| 82 | // We considered keys that are only in a, and in both a and b. Now we just |
| 83 | // need to handle keys that are only in b. |
| 84 | for k, v2 := range b { |
| 85 | if _, ok := a[k]; !ok { |
| 86 | out[k] = v2 |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return out |
| 91 | } |