From df3ea217368d127ff02f525cdfbb64d6c23c854e Mon Sep 17 00:00:00 2001 From: Sergey Rodionov Date: Sat, 27 Jan 2024 16:35:35 +0300 Subject: [PATCH 1/6] minimal binding for PyBHV hypervector library --- python/sandbox/bhv_binding/bhv_binding.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 python/sandbox/bhv_binding/bhv_binding.py diff --git a/python/sandbox/bhv_binding/bhv_binding.py b/python/sandbox/bhv_binding/bhv_binding.py new file mode 100644 index 000000000..0edbb3b9f --- /dev/null +++ b/python/sandbox/bhv_binding/bhv_binding.py @@ -0,0 +1,16 @@ +from hyperon.atoms import OperationAtom +from hyperon.ext import register_atoms +from bhv.np import NumPyBoolBHV as BHV, NumPyBoolPermutation as Perm + + +@register_atoms +def my_atoms(): + return { + 'bhv-new': OperationAtom('bhv-new', BHV.rand), + 'bhv-majority': OperationAtom('bhv-majority', lambda *args: BHV.majority(list(args))), + 'bhv-bind': OperationAtom('bhv-bind', lambda a,b: a^b), + 'bhv-std-apart-relative': OperationAtom('bhv-std-apart-relative', lambda a,b: a.std_apart(b, relative = True)), + 'bhv-is-related': OperationAtom('bhv-is-related', lambda a,b: a.related(b)), + 'bhv-new-perm': OperationAtom('bhv-perm', Perm.random), + 'bhv-apply-perm': OperationAtom('bhv-apply-perm', lambda a, b: a(b)) + } From 0e9873d6bc3a54ac2e72758604e2f39ff993c057 Mon Sep 17 00:00:00 2001 From: Sergey Rodionov Date: Sat, 27 Jan 2024 16:36:12 +0300 Subject: [PATCH 2/6] examples for PyBHV binding --- .../bhv_binding/01_example_majority.metta | 12 ++++++ python/sandbox/bhv_binding/02_perm.metta | 21 ++++++++++ .../sandbox/bhv_binding/03_example_dict.metta | 17 ++++++++ .../04_example_dollar_of_mexico.metta | 39 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 python/sandbox/bhv_binding/01_example_majority.metta create mode 100644 python/sandbox/bhv_binding/02_perm.metta create mode 100644 python/sandbox/bhv_binding/03_example_dict.metta create mode 100644 python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta diff --git a/python/sandbox/bhv_binding/01_example_majority.metta b/python/sandbox/bhv_binding/01_example_majority.metta new file mode 100644 index 000000000..3e000a614 --- /dev/null +++ b/python/sandbox/bhv_binding/01_example_majority.metta @@ -0,0 +1,12 @@ +!(extend-py! bhv_binding) + +!(bind! &a (bhv-new)) +!(bind! &b (bhv-new)) +!(bind! &c (bhv-new)) + +(= (abc) (bhv-majority &a &b &c )) + +!(bhv-std-apart-relative &a (abc)) +!(bhv-is-related &a (abc)) + + diff --git a/python/sandbox/bhv_binding/02_perm.metta b/python/sandbox/bhv_binding/02_perm.metta new file mode 100644 index 000000000..69ac58e32 --- /dev/null +++ b/python/sandbox/bhv_binding/02_perm.metta @@ -0,0 +1,21 @@ +!(extend-py! bhv_binding) + +!(bind! &a (bhv-new)) +!(bind! &b (bhv-new)) + +!(bind! &perm1 (bhv-new-perm)) +(= (perm1 $x) (bhv-apply-perm &perm1 $x)) + +(= (ab) (bhv-majority &a &b )) + +; Should return False since a is permuted but ab is not permuted +!(bhv-is-related (perm1 &a) (ab)) + +;Should return True +!(bhv-is-related (perm1 &a) (perm1 (ab))) + + + + + + diff --git a/python/sandbox/bhv_binding/03_example_dict.metta b/python/sandbox/bhv_binding/03_example_dict.metta new file mode 100644 index 000000000..7a027b12f --- /dev/null +++ b/python/sandbox/bhv_binding/03_example_dict.metta @@ -0,0 +1,17 @@ +!(extend-py! bhv_binding) + +!(bind! &v1 (bhv-new)) +!(bind! &v2 (bhv-new)) +!(bind! &v3 (bhv-new)) +!(bind! &k1 (bhv-new)) +!(bind! &k2 (bhv-new)) +!(bind! &k3 (bhv-new)) + + +(= (dict) (bhv-majority (bhv-bind &v1 &k1) (bhv-bind &v2 &k2) (bhv-bind &v3 &k3) )) +(= (v1_retrived) (bhv-bind &k1 (dict))) + +!(bhv-std-apart-relative (v1_retrived) &v1) +!(bhv-is-related (v1_retrived) &v1) + + diff --git a/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta new file mode 100644 index 000000000..07e1cd7cd --- /dev/null +++ b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta @@ -0,0 +1,39 @@ +!(extend-py! bhv_binding) + +name, capital_city, money = BHV.nrand(3) +united_states, washington_dc, dollar = BHV.nrand(3) +mexico, mexico_city, peso = BHV.nrand(3) + + +!(bind! &name (bhv-new)) +!(bind! &capital_city (bhv-new)) +!(bind! &money (bhv-new)) +!(bind! &united_states (bhv-new)) +!(bind! &washington_dc (bhv-new)) +!(bind! &dollar (bhv-new)) +!(bind! &mexico (bhv-new)) +!(bind! &mexico_city (bhv-new)) +!(bind! &peso (bhv-new)) + +USA = BHV.majority([name ^ united_states, capital_city ^ washington_dc, money ^ dollar]) +MEX = BHV.majority([name ^ mexico, capital_city ^ mexico_city, money ^ peso]) + +(= (USA) (bhv-majority (bhv-bind &name &united_states) (bhv-bind &capital_city &washington_dc) (bhv-bind &money &dollar) )) +(= (MEX) (bhv-majority (bhv-bind &name &mexico) (bhv-bind &capital_city &mexico_city) (bhv-bind &money &peso) )) + +(= (Pair) (bhv-bind (USA) (MEX))) + +; should return True here +!(bhv-is-related (bhv-bind (USA) &money) &dollar) + + +(= (dollar_of_mexico) (bhv-bind &dollar (Pair))) + +; should return True here. "Dollar of mexico" is peso +!(bhv-is-related (dollar_of_mexico) &peso) + + + + + + From 15331cfbe566e5f801346337df1f4dcd1d0d3eea Mon Sep 17 00:00:00 2001 From: Sergey Rodionov Date: Sat, 27 Jan 2024 16:45:12 +0300 Subject: [PATCH 3/6] rename 02_example_perm.metta --- .../sandbox/bhv_binding/{02_perm.metta => 02_example_perm.metta} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename python/sandbox/bhv_binding/{02_perm.metta => 02_example_perm.metta} (100%) diff --git a/python/sandbox/bhv_binding/02_perm.metta b/python/sandbox/bhv_binding/02_example_perm.metta similarity index 100% rename from python/sandbox/bhv_binding/02_perm.metta rename to python/sandbox/bhv_binding/02_example_perm.metta From 6725c82020898d42e146b23a38e6b15f47dc50c8 Mon Sep 17 00:00:00 2001 From: Sergey Rodionov Date: Sat, 27 Jan 2024 16:49:59 +0300 Subject: [PATCH 4/6] remove trush from 04_example_dollar_of_mexico.metta --- python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta | 5 ----- 1 file changed, 5 deletions(-) diff --git a/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta index 07e1cd7cd..745ed7930 100644 --- a/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta +++ b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta @@ -1,10 +1,5 @@ !(extend-py! bhv_binding) -name, capital_city, money = BHV.nrand(3) -united_states, washington_dc, dollar = BHV.nrand(3) -mexico, mexico_city, peso = BHV.nrand(3) - - !(bind! &name (bhv-new)) !(bind! &capital_city (bhv-new)) !(bind! &money (bhv-new)) From a4b057169e0d3b70b4093c10133df0085e72cc69 Mon Sep 17 00:00:00 2001 From: Sergey Rodionov Date: Sat, 27 Jan 2024 16:51:09 +0300 Subject: [PATCH 5/6] remove trush from 04_example_dollar_of_mexico.metta --- python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta index 745ed7930..05a635e95 100644 --- a/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta +++ b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta @@ -10,8 +10,6 @@ !(bind! &mexico_city (bhv-new)) !(bind! &peso (bhv-new)) -USA = BHV.majority([name ^ united_states, capital_city ^ washington_dc, money ^ dollar]) -MEX = BHV.majority([name ^ mexico, capital_city ^ mexico_city, money ^ peso]) (= (USA) (bhv-majority (bhv-bind &name &united_states) (bhv-bind &capital_city &washington_dc) (bhv-bind &money &dollar) )) (= (MEX) (bhv-majority (bhv-bind &name &mexico) (bhv-bind &capital_city &mexico_city) (bhv-bind &money &peso) )) From f580941a19caffae487ebd30a924c07ec3d44469 Mon Sep 17 00:00:00 2001 From: Sergey Rodionov Date: Mon, 29 Jan 2024 16:56:32 +0300 Subject: [PATCH 6/6] fix examples --- python/sandbox/bhv_binding/01_example_majority.metta | 6 +++--- python/sandbox/bhv_binding/02_example_perm.metta | 6 +++--- python/sandbox/bhv_binding/03_example_dict.metta | 8 ++++---- .../bhv_binding/04_example_dollar_of_mexico.metta | 12 ++++++------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/python/sandbox/bhv_binding/01_example_majority.metta b/python/sandbox/bhv_binding/01_example_majority.metta index 3e000a614..1949ac883 100644 --- a/python/sandbox/bhv_binding/01_example_majority.metta +++ b/python/sandbox/bhv_binding/01_example_majority.metta @@ -4,9 +4,9 @@ !(bind! &b (bhv-new)) !(bind! &c (bhv-new)) -(= (abc) (bhv-majority &a &b &c )) +!(bind! &abc (bhv-majority &a &b &c )) -!(bhv-std-apart-relative &a (abc)) -!(bhv-is-related &a (abc)) +!(bhv-std-apart-relative &a &abc) +!(bhv-is-related &a &abc) diff --git a/python/sandbox/bhv_binding/02_example_perm.metta b/python/sandbox/bhv_binding/02_example_perm.metta index 69ac58e32..4e1a172b6 100644 --- a/python/sandbox/bhv_binding/02_example_perm.metta +++ b/python/sandbox/bhv_binding/02_example_perm.metta @@ -6,13 +6,13 @@ !(bind! &perm1 (bhv-new-perm)) (= (perm1 $x) (bhv-apply-perm &perm1 $x)) -(= (ab) (bhv-majority &a &b )) +!(bind! ab (bhv-majority &a &b )) ; Should return False since a is permuted but ab is not permuted -!(bhv-is-related (perm1 &a) (ab)) +!(bhv-is-related (perm1 &a) ab) ;Should return True -!(bhv-is-related (perm1 &a) (perm1 (ab))) +!(bhv-is-related (perm1 &a) (perm1 ab)) diff --git a/python/sandbox/bhv_binding/03_example_dict.metta b/python/sandbox/bhv_binding/03_example_dict.metta index 7a027b12f..4d9f85367 100644 --- a/python/sandbox/bhv_binding/03_example_dict.metta +++ b/python/sandbox/bhv_binding/03_example_dict.metta @@ -8,10 +8,10 @@ !(bind! &k3 (bhv-new)) -(= (dict) (bhv-majority (bhv-bind &v1 &k1) (bhv-bind &v2 &k2) (bhv-bind &v3 &k3) )) -(= (v1_retrived) (bhv-bind &k1 (dict))) +!(bind! dict (bhv-majority (bhv-bind &v1 &k1) (bhv-bind &v2 &k2) (bhv-bind &v3 &k3) )) +!(bind! v1_retrived (bhv-bind &k1 dict)) -!(bhv-std-apart-relative (v1_retrived) &v1) -!(bhv-is-related (v1_retrived) &v1) +!(bhv-std-apart-relative v1_retrived &v1) +!(bhv-is-related v1_retrived &v1) diff --git a/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta index 05a635e95..2c2474300 100644 --- a/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta +++ b/python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta @@ -11,19 +11,19 @@ !(bind! &peso (bhv-new)) -(= (USA) (bhv-majority (bhv-bind &name &united_states) (bhv-bind &capital_city &washington_dc) (bhv-bind &money &dollar) )) -(= (MEX) (bhv-majority (bhv-bind &name &mexico) (bhv-bind &capital_city &mexico_city) (bhv-bind &money &peso) )) +!(bind! USA (bhv-majority (bhv-bind &name &united_states) (bhv-bind &capital_city &washington_dc) (bhv-bind &money &dollar) )) +!(bind! MEX (bhv-majority (bhv-bind &name &mexico) (bhv-bind &capital_city &mexico_city) (bhv-bind &money &peso) )) -(= (Pair) (bhv-bind (USA) (MEX))) +!(bind! Pair (bhv-bind USA MEX)) ; should return True here -!(bhv-is-related (bhv-bind (USA) &money) &dollar) +!(bhv-is-related (bhv-bind USA &money) &dollar) -(= (dollar_of_mexico) (bhv-bind &dollar (Pair))) +!(bind! dollar_of_mexico (bhv-bind &dollar Pair)) ; should return True here. "Dollar of mexico" is peso -!(bhv-is-related (dollar_of_mexico) &peso) +!(bhv-is-related dollar_of_mexico &peso)