From a942d79869f1fcad66571614d66d536a3d7f8589 Mon Sep 17 00:00:00 2001 From: Peter Harpending Date: Sat, 11 Oct 2025 15:04:00 -0600 Subject: [PATCH] snowflakes work except define --- src/wfc.erl | 3 ++- src/wfc_bm.erl | 2 +- src/wfc_eval_context.erl | 57 +++++++++++++++++++++++++++++++++++++++- src/wfc_sftt.erl | 17 ++++++++---- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/wfc.erl b/src/wfc.erl index 79331d2..616a303 100644 --- a/src/wfc.erl +++ b/src/wfc.erl @@ -13,7 +13,8 @@ reps/1, reps/2, add/1, - mul/1 + mul/1, + zero/0, one/0 ]). diff --git a/src/wfc_bm.erl b/src/wfc_bm.erl index 52b390d..878ad5c 100644 --- a/src/wfc_bm.erl +++ b/src/wfc_bm.erl @@ -219,7 +219,7 @@ dot({bm, {rc, 1, Same}, Bits1}, {bm, {rc, Same, 1}, Bits2}) -> parity(SummandBits). --spec mand(bitstring(), bitstring()) -> bitstring(). +-spec mand(bm(), bm()) -> bm(). % @doc bitwise-and two matrices of the same shape mand({bm, Shape, Bits1}, {bm, Shape, Bits2}) -> diff --git a/src/wfc_eval_context.erl b/src/wfc_eval_context.erl index d8cfda2..d686282 100644 --- a/src/wfc_eval_context.erl +++ b/src/wfc_eval_context.erl @@ -32,7 +32,62 @@ default() -> default_snowflakes() -> #{<<"and">> => fun wfc:mul/1, - <<"xor">> => fun wfc:add/1}. + <<"xor">> => fun wfc:add/1, + <<"ior">> => + fun IOR([S1 | Rest]) -> + case IOR(Rest) of + {ok, S2} -> {ok, sf_ior(S1, S2)}; + Error -> Error + end; + IOR([]) -> + {ok, wfc:one()} + end, + <<"not">> => + fun ([S]) -> {ok, sf_not(S)}; + (Bad) -> {error, wfc_utils:str("not/1: wrong number of arguments: ~p", [Bad])} + end, + <<"implies">> => + fun ([A, B]) -> {ok, sf_implies(A, B)}; + (Bad) -> {error, wfc_utils:str("implies/2: wrong number of arguments: ~p", [Bad])} + end, + <<"impliedby">> => + fun ([A, B]) -> {ok, sf_impliedby(A, B)}; + (Bad) -> {error, wfc_utils:str("impliedby/2: wrong number of arguments: ~p", [Bad])} + end, + <<"iff">> => + fun ([A, B]) -> {ok, sf_iff(A, B)}; + (Bad) -> {error, wfc_utils:str("iff/2: wrong number of arguments: ~p", [Bad])} + end + }. + +sf_ior(A, B) -> + wfc_sftt:appl_ttf(fun ttf_ior/2, [A, B]). + +sf_not(A) -> + {ok, Result} = wfc:add([wfc_sentence:one(), A]), + Result. + +sf_implies(A, B) -> + wfc_sftt:appl_ttf(fun ttf_implies/2, [A, B]). + +sf_impliedby(A, B) -> + sf_implies(B, A). + +sf_iff(A, B) -> + {ok, Result} = wfc:mul([sf_implies(A, B), sf_impliedby(A, B)]), + Result. + +ttf_ior(0, 0) -> 0; +ttf_ior(1, 0) -> 1; +ttf_ior(0, 1) -> 1; +ttf_ior(1, 1) -> 1. + +ttf_implies(0, 0) -> 1; +ttf_implies(1, 0) -> 0; +ttf_implies(0, 1) -> 1; +ttf_implies(1, 1) -> 1. + + define(Pat, Sentence, Ctx = #ctx{patterns = OldPatterns}) -> NewPatterns = maps:put(Pat, Sentence, OldPatterns), diff --git a/src/wfc_sftt.erl b/src/wfc_sftt.erl index 9f8fddf..435db20 100644 --- a/src/wfc_sftt.erl +++ b/src/wfc_sftt.erl @@ -12,7 +12,7 @@ arity/1, tt/1, sf/1, sf_to_tt/1, tt_to_sf/1, - eval/2, + appl_ttf/2, appl/2, bfls/1, bfl/2 ]). @@ -73,11 +73,18 @@ sf(List) -> {sf, wfc_bm:col(List)}. --spec eval(sf() | tt(), [wfc:sentence()]) -> wfc:sentence(). +-spec appl_ttf(fun(), [wfc:sentence()]) -> wfc:sentence(). -eval(TT = {tt, _}, Sentences) -> - eval(tt_to_sf(TT), Sentences); -eval(SF = {sf, SFBM}, Sentences) -> +appl_ttf(Fun, Sentences) -> + SF = ttfun_to_sf(Fun), + appl(SF, Sentences). + + +-spec appl(sf() | tt(), [wfc:sentence()]) -> wfc:sentence(). + +appl(TT = {tt, _}, Sentences) -> + appl(tt_to_sf(TT), Sentences); +appl(SF = {sf, SFBM}, Sentences) -> Arity = arity(SF), Arity = length(Sentences), % [0, 0, 0], [1, 0, 0], ...