-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyperlattice.lisp
74 lines (63 loc) · 2.77 KB
/
hyperlattice.lisp
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
(in-package :cl-user)
(defpackage hyperlattices/hyperlattice
(:nicknames hyperlattice)
(:use c2cl
hyperlattices/hash-table-utils
hyperlattices/generic-interface
hyperlattices/lattice)
(:export #:hyperlattice-sup
#:hyperlattice-inf
#:hyperlattice-add
#:hyperlattice-remove
#:hyperlattice-member-p
#:hyperlattice-sup-set
#:hyperlattice-inf-set
#:hyperlattice-closure
#:hyperlattice
#:elements-of
#:sup-of
#:inf-of)
(:documentation "Implementation of HYPERLATTICE algebraic datatype's type class and method specializations."))
(in-package :hyperlattices/hyperlattice)
;; Define a function to compute the supremum of two lattices
(defun hyperlattice-sup (a b)
(make-instance 'hyperlattice :elements (merge-hash-tables (elements-of a) (elements-of b))
:sup (sup-of a)
:inf (inf-of a)))
;; Define a function to compute the infimum of two lattices
(defun hyperlattice-inf (a b)
(make-instance 'hyperlattice :elements (intersection-hash-tables (elements-of a) (elements-of b))
:sup (sup-of a)
:inf (inf-of a)))
;; Define a class to represent a hyperlattice
(defclass hyperlattice (lattice)
((elements :initarg :elements :accessor elements-of)
(sup :initarg :sup :accessor sup-of)
(inf :initarg :inf :accessor inf-of))
(:default-initargs :elements (make-hash-table)
:sup #'hyperlattice-sup
:inf #'hyperlattice-inf))
;; Define a function to add a lattice to the hyperlattice
(defun hyperlattice-add (hyperlattice lattice)
(setf (gethash lattice (elements-of hyperlattice)) t))
;; Define a function to remove a lattice from the hyperlattice
(defun hyperlattice-remove (hyperlattice lattice)
(remhash lattice (elements-of hyperlattice)))
;; Define a function to check if a lattice is in the hyperlattice
(defun hyperlattice-member-p (hyperlattice lattice)
(gethash lattice (elements-of hyperlattice)))
;; Define a function to compute the supremum of a set of lattices
(defun hyperlattice-sup-set (hyperlattice set)
(reduce (sup-of hyperlattice) set))
;; Define a function to compute the infimum of a set of lattices
(defun hyperlattice-inf-set (hyperlattice set)
(reduce (inf-of hyperlattice) set))
;; Define a function to compute the closure of a set of lattices
(defun hyperlattice-closure (hyperlattice set)
(let ((closure set))
(loop
for element being the hash-keys of (elements-of hyperlattice)
unless (member element closure)
when (every (lambda (x) (hyperlattice-member-p hyperlattice x)) (cons element closure))
do (push element closure))
closure))