aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/logic/fuzzy.lux
blob: a30c42d8c457b069a1f2dff5ed3a5af35642e9c1 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
... https://en.wikipedia.org/wiki/Fuzzy_logic
(.module:
  [library
   [lux "*"
    [abstract
     [predicate {"+" [Predicate]}]
     [functor
      ["." contravariant]]]
    [data
     [collection
      ["." list]
      ["." set {"+" [Set]}]]]
    [math
     [number
      ["/" rev]]]]]
  ["." // "_"
   ["#" continuous]])

(type: .public (Fuzzy a)
  (-> a Rev))

(implementation: .public functor
  (contravariant.Functor Fuzzy)
  
  (def: (each f fb)
    (|>> f fb)))

(template [<name> <verdict>]
  [(def: .public <name>
     Fuzzy
     (function (_ _)
       <verdict>))]

  [empty //.false]
  [full //.true]
  )

(def: .public (membership set elem)
  (All (_ a) (-> (Fuzzy a) a Rev))
  (set elem))

(template [<set_composition> <membership_composition>]
  [(def: .public (<set_composition> left right)
     (All (_ a) (-> (Fuzzy a) (Fuzzy a) (Fuzzy a)))
     (function (_ elem)
       (<membership_composition> (left elem)
                                 (right elem))))]

  [union //.or]
  [intersection //.and]
  )

(def: .public (complement set)
  (All (_ a) (-> (Fuzzy a) (Fuzzy a)))
  (|>> set //.not))

(def: .public (difference sub base)
  (All (_ a) (-> (Fuzzy a) (Fuzzy a) (Fuzzy a)))
  (..intersection (..complement sub) base))

(def: .public (of_predicate predicate)
  (All (_ a) (-> (Predicate a) (Fuzzy a)))
  (function (_ elem)
    (if (predicate elem)
      //.true
      //.false)))

(def: .public (predicate treshold set)
  (All (_ a) (-> Rev (Fuzzy a) (Predicate a)))
  (function (_ elem)
    (/.> treshold (set elem))))

(def: .public of_set
  (All (_ a) (-> (Set a) (Fuzzy a)))
  (|>> set.member? ..of_predicate))

(def: (ascending from to)
  (-> Rev Rev (Fuzzy Rev))
  (let [measure (/.- from to)]
    (function (_ elem)
      (cond (/.< from elem)
            ... below
            //.false

            (/.< to elem)
            ... in the middle...
            (|> elem
                (/.- from)
                (/./ measure))

            ... above
            //.true))))

(def: (descending from to)
  (-> Rev Rev (Fuzzy Rev))
  (..complement (..ascending from to)))

(def: .public (gradient from to)
  (-> Rev Rev (Fuzzy Rev))
  (if (/.< to from)
    (..ascending from to)
    (..descending from to)))

(template: (!sort_2 <low> <high>)
  [(if (/.> <low> <high>)
     [<low> <high>]
     [<high> <low>])])

(def: .public (triangle bottom middle top)
  (-> Rev Rev Rev (Fuzzy Rev))
  (let [[low_0 high_0] (!sort_2 bottom middle)
        [bottom' high_1] (!sort_2 low_0 top)
        [middle' top'] (!sort_2 high_0 high_1)]
    (..intersection (..ascending bottom' middle')
                    (..descending middle' top'))))

(def: .public (trapezoid bottom middle_bottom middle_top top)
  (-> Rev Rev Rev Rev (Fuzzy Rev))
  (let [[low_0 high_0] (!sort_2 bottom middle_bottom)
        [low_1 high_1] (!sort_2 middle_top top)
        [bottom' middle_0] (!sort_2 low_0 low_1)
        [middle_1 top'] (!sort_2 high_0 high_1)
        [middle_bottom' middle_top'] (!sort_2 middle_0 middle_1)]
    (..intersection (..ascending bottom' middle_bottom')
                    (..descending middle_top' top'))))

(def: .public (cut treshold set)
  (All (_ a) (-> Rev (Fuzzy a) (Fuzzy a)))
  (function (_ elem)
    (let [membership (set elem)]
      (if (/.< treshold membership)
        //.false
        (|> membership (/.- treshold) (/.* //.true))))))