... https://en.wikipedia.org/wiki/Fuzzy_logic (.require [library [lux (.except) [abstract [functor ["[0]" contravariant]]] [control [function [predicate (.only Predicate)]]] [data [collection ["[0]" list] ["[0]" set (.only Set)]]] [math [number ["/" rev]]]]] ["[0]" // ["[1]" continuous]]) (type .public (Fuzzy a) (-> a Rev)) (def .public functor (contravariant.Functor Fuzzy) (implementation (def (each f fb) (|>> f fb)))) (with_template [ ] [(def .public Fuzzy (function (_ _) ))] [empty //.false] [full //.true] ) (def .public (membership set elem) (All (_ a) (-> (Fuzzy a) a Rev)) (set elem)) (with_template [ ] [(def .public ( left right) (All (_ a) (-> (Fuzzy a) (Fuzzy a) (Fuzzy a))) (function (_ elem) ( (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))) (def !sort_2 (template (_ ) [(if (/.> ) [ ] [ ])])) (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))))))