From 3063608df6ff1668f683d9d22c57fea29d728d0d Mon Sep 17 00:00:00 2001 From: Marcos Magueta Date: Tue, 11 Nov 2025 00:00:43 -0300 Subject: migrating to erlang to attach to mnesia --- src/main.app.src | 15 +++++++++++++++ src/main.erl | 7 +++++++ src/operations.erl | 15 +++++++++++++++ src/relation.erl | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 src/main.app.src create mode 100644 src/main.erl create mode 100644 src/operations.erl create mode 100644 src/relation.erl (limited to 'src') diff --git a/src/main.app.src b/src/main.app.src new file mode 100644 index 0000000..0b7700a --- /dev/null +++ b/src/main.app.src @@ -0,0 +1,15 @@ +{application, main, [ + {description, "A relational algebra engine"}, + {vsn, "0.1.0"}, + {registered, []}, + {mod, {main_app, []}}, + {applications, [ + kernel, + stdlib, + mnesia + ]}, + {env, []}, + {modules, []}, + {licenses, ["Apache-2.0"]}, + {links, []} + ]}. diff --git a/src/main.erl b/src/main.erl new file mode 100644 index 0000000..3c34a31 --- /dev/null +++ b/src/main.erl @@ -0,0 +1,7 @@ +-module(main). +-export([fibonacci/1]). + +fibonacci(0) -> 0; +fibonacci(1) -> 1; +fibonacci(N) -> + fibonacci(N-2) + fibonacci(N-1). diff --git a/src/operations.erl b/src/operations.erl new file mode 100644 index 0000000..7846caf --- /dev/null +++ b/src/operations.erl @@ -0,0 +1,15 @@ +-module(operations). +-export([select/2, project/2, cartesian_product/2]). + +%% @doc Selection - filter tuples based on a predicate +%% `Predicate` is a function that takes a tuple and returns true/false +select(Relation, Predicate) -> + sets:filter(Predicate, Relation). + +%% @doc Projection - select specific attributes from a relation +%% Note that `AttributeNames` can have duplicate keys, which are ignored +project(Relation, AttributeNames) when is_list(AttributeNames) -> + sets:map(fun (Tuple) -> maps:with(AttributeNames, Tuple) end, Relation). + +cartesian_product(Relation1, Relation2) -> + sets:from_list([maps:merge(Tuple1, Tuple2) || Tuple1 <- sets:to_list(Relation1), Tuple2 <- sets:to_list(Relation2)], [{version, 2}]). diff --git a/src/relation.erl b/src/relation.erl new file mode 100644 index 0000000..66930d2 --- /dev/null +++ b/src/relation.erl @@ -0,0 +1,42 @@ +-module(relation). +-export([new_attribute/2, new_tuple/1, new_relation/1]). + +%% Attribute: A pair of (Name, Value), eg. ("Name", "Augusto") +%% Tuple: A set of attributes, eg. (("Name", "Augusto"), ("Age", 20), ("Surname", "Rengel")) +%% Relation: A set of tuples organized through some selector +%% Example Relation: f(x: Nat) = x + 1 +%% |{(0, 1), (1, 2), (2, 3), ...}| = |N| +%% RelationCustomer = [(Username, Text), (Password, Text)] +%% RelationProduct = [(Name, Text), (Price, Real)] +%% RelationOrder = [(OrderId, Natural), ((Username, Text), +%% (Password, Text), RelationCustomer), ((Name, Text), +%% (Price, Real), RelationProduct)] + +new_attribute(Name, Value) -> + #{name => Name, value => Value}. + +new_tuple(Attributes) when is_list(Attributes) -> + Fun = fun (#{name := Name, value := Value}, Acc) -> + maps:put(Name, Value, Acc) + end, + lists:foldl(Fun, #{}, Attributes). + +new_relation(Tuples) when is_list(Tuples) -> + sets:from_list(Tuples, [{version, 2}]). + +%% SoapName1 = relation:new_attribute(name, "ABC"). +%% SoapPrice1 = relation:new_attribute(price, 1.0). +%% SoapName2 = relation:new_attribute(name, "XYZ"). +%% SoapPrice2 = relation:new_attribute(price, 1.25). +%% BazookaName = relation:new_attribute(name, "Bazzum"). +%% BazookaPrice = relation:new_attribute(price, 413.0). +%% SwordName = relation:new_attribute(name, "Sword"). +%% SwordPrice = relation:new_attribute(price, 100.0). +%% relation:new_relation([relation:new_tuple([SoapName, SoapPrice]), relation:new_tuple([BazookaName, BazookaPrice]), relation:new_tuple([SwordName, SwordPrice])]). + +%% Jules = relation:new_attribute(customer_name, "Jules"). +%% Emily = relation:new_attribute(customer_name, "Emily"). +%% Customers = relation:new_relation([relation:new_tuple([Jules]), +%% relation:new_tuple([Emily])]). + +%% operations:project(operations:select(operations:cartesian_product(relation:new_relation([relation:new_tuple([SoapName1, SoapPrice1]), relation:new_tuple([SoapName2, SoapPrice2]), relation:new_tuple([BazookaName, BazookaPrice]), relation:new_tuple([SwordName, SwordPrice])]), Customers), fun (Tuple) -> maps:get(price, Tuple) =< 1.0 end), [name, customer_name]). -- cgit v1.2.3