too fucking tired... going to bed

This commit is contained in:
Peter Harpending
2025-10-02 08:44:17 -07:00
parent 437988acee
commit 69a89cf78e
8 changed files with 527 additions and 3 deletions
+66 -2
View File
@@ -13,11 +13,14 @@
-export([
%% constructors
zero/0,
zero/0, one/0,
validate/1,
from_list/1, to_list/1,
%% ops
add/2, add/1
add/2, add/1,
mul/2, mul_wxs/2, mul/1,
%%% from_
from_ltr/1, from_word/1
]).
-opaque sentence() :: {s, ordsets:ordset(wfc_word:word())}.
@@ -27,11 +30,21 @@
%%--------------------------
-spec zero() -> sentence().
% @doc the 0-sentence is the empty sentence
zero() ->
{s, []}.
-spec one() -> sentence().
% @doc the 1-sentence is the sentence that contains only the 1-word
one() ->
{s, [wfc_word:one()]}.
-spec from_list(Words) -> Result
when Words :: [wfc_word:word()],
Result :: {ok, sentence()}
@@ -91,3 +104,54 @@ symdiff(X, Y) ->
add([S | Rest]) -> add(S, add(Rest));
add([]) -> zero().
%%---------------------------
%% spec ops
%%---------------------------
-spec mul(sentence(), sentence()) -> sentence().
mul({s, [W | Rest]}, S) ->
%% (W + Rest) * S = W*S + Rest*S
add(mul_wxs(W, S),
mul({s, Rest}, S));
mul({s, []}, _) ->
%% first arg 0 = result 0
zero().
-spec mul([sentence()]) -> sentence().
% @doc
% multiply a list of sentences together; mul([]) = {s, {w, []}}
mul([S | Rest]) -> mul(S, mul(Rest));
mul([]) -> one().
-spec mul_wxs(wfc_word:word(), sentence()) -> sentence().
mul_wxs(W, {s, [X | Rest]}) ->
% W * (X + Rest) = W*X + W*Rest
add({s, [wfc_word:mul(W, X)]},
mul_wxs(W, {s, Rest}));
mul_wxs(_, {s, []}) ->
zero().
%%---------------------------
%% from_
%%---------------------------
-spec from_ltr(term()) -> {ok, sentence()} | {error, string()}.
from_ltr(Ltr) ->
case wfc_word:from_ltr(Ltr) of
{ok, Word} -> from_word(Word);
Error -> Error
end.
-spec from_word(term()) -> {ok, sentence()} | {error, string()}.
from_word(Word) ->
from_list([Word]).