-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlattice.lisp
82 lines (71 loc) · 2.46 KB
/
lattice.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
75
76
77
78
79
80
81
82
(in-package :cl-user)
(defpackage hyperlattices/lattice
(:nicknames lattice)
(:use c2cl
hyperlattices/hash-table-utils
hyperlattices/generic-interface
hyperlattices/generalized-lattice)
(:export #:lattice-sup
#:lattice-inf
#:lattice
#:lattice-add
#:lattice-remove
#:lattice-member-p
#:lattice-sup-set
#:lattice-inf-set
#:lattice-closure
#:elements-of
#:sup-of
#:inf-of)
(:documentation "Implementation of LATTICE algebraic datatype's type class and method specializations."))
(in-package :hyperlattices/lattice)
;; Define a function to compute the supremum of two elements
(defun lattice-sup (a b)
(if (eq a b)
a
(if (eq a 'bottom)
b
(if (eq b 'bottom)
a
'top))))
;; Define a function to compute the infimum of two elements
(defun lattice-inf (a b)
(if (eq a b)
a
(if (eq a 'top)
b
(if (eq b 'top)
a
'bottom))))
;; Define a class to represent a lattice
(defclass lattice (generalized-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 #'lattice-sup
:inf #'lattice-inf))
;; Define a function to add an element to the lattice
(defun lattice-add (lattice element)
(setf (gethash element (elements-of lattice)) t))
;; Define a function to remove an element from the lattice
(defun lattice-remove (lattice element)
(remhash element (elements-of lattice)))
;; Define a function to check if an element is in the lattice
(defun lattice-member-p (lattice element)
(gethash element (elements-of lattice)))
;; Define a function to compute the supremum of a set of elements
(defun lattice-sup-set (lattice set)
(reduce (sup-of lattice) set))
;; Define a function to compute the infimum of a set of elements
(defun lattice-inf-set (lattice set)
(reduce (inf-of lattice) set))
;; Define a function to compute the closure of a set of elements
(defun lattice-closure (lattice set)
(let ((closure set))
(loop
for element being the hash-keys of (elements-of lattice)
unless (member element closure)
when (every (lambda (x) (lattice-member-p lattice x)) (cons element closure))
do (push element closure))
closure))