From a66cd3cf912c6f506840cfd9bb94fb4f363b22e0 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 16 Mar 2026 13:51:51 -0500 Subject: [PATCH] xo-numeric: attach type to numeric primitives --- .../include/xo/numeric/NumericPrimitives.hpp | 28 +++-- xo-numeric/src/numeric/NumericPrimitives.cpp | 114 +++++++++++++++--- .../numeric/numeric_register_primitives.cpp | 22 ++-- xo-type/include/xo/type/Metatype.hpp | 4 + xo-type/src/type/Metatype.cpp | 3 + 5 files changed, 134 insertions(+), 37 deletions(-) diff --git a/xo-numeric/include/xo/numeric/NumericPrimitives.hpp b/xo-numeric/include/xo/numeric/NumericPrimitives.hpp index 8a1cd33d..aec3ae84 100644 --- a/xo-numeric/include/xo/numeric/NumericPrimitives.hpp +++ b/xo-numeric/include/xo/numeric/NumericPrimitives.hpp @@ -20,26 +20,34 @@ namespace xo { /** polymorphic (in both arguments1) multiply **/ static DPrimitive_gco_2_gco_gco * make_multiply_pm(obj mm, StringTable * stbl); - /** polymorphic (in both arguments) divide **/ - static DPrimitive_gco_2_gco_gco * make_divide_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_divide_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) add **/ - static DPrimitive_gco_2_gco_gco * make_add_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_add_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) subtract **/ - static DPrimitive_gco_2_gco_gco * make_subtract_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_subtract_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) compare (==) **/ - static DPrimitive_gco_2_gco_gco * make_cmpeq_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_cmpeq_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) compare (!=) **/ - static DPrimitive_gco_2_gco_gco * make_cmpne_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_cmpne_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) compare (<) **/ - static DPrimitive_gco_2_gco_gco * make_cmplt_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_cmplt_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) compare (<=) **/ - static DPrimitive_gco_2_gco_gco * make_cmple_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_cmple_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) compare (>) **/ - static DPrimitive_gco_2_gco_gco * make_cmpgt_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_cmpgt_pm(obj mm, + StringTable * stbl); /** polymorphic (in both arguments) compare (>=) **/ - static DPrimitive_gco_2_gco_gco * make_cmpge_pm(obj mm); + static DPrimitive_gco_2_gco_gco * make_cmpge_pm(obj mm, + StringTable * stbl); }; } } diff --git a/xo-numeric/src/numeric/NumericPrimitives.cpp b/xo-numeric/src/numeric/NumericPrimitives.cpp index aeb7d8fb..decc8d60 100644 --- a/xo-numeric/src/numeric/NumericPrimitives.cpp +++ b/xo-numeric/src/numeric/NumericPrimitives.cpp @@ -34,65 +34,143 @@ namespace xo { } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_divide_pm(obj mm) + NumericPrimitives::make_divide_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_div", + (void)stbl; + + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op+: numeric x numeric -> numeric + auto pm_ty = obj + (DFunctionType::_make(mm, numeric_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_div", pm_ty, &NumericDispatch::divide); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_add_pm(obj mm) + NumericPrimitives::make_add_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_add", + (void)stbl; + + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op+: numeric x numeric -> numeric + auto pm_ty = obj + (DFunctionType::_make(mm, numeric_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_add", pm_ty, &NumericDispatch::add); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_subtract_pm(obj mm) + NumericPrimitives::make_subtract_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_sub", + (void)stbl; + + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op+: numeric x numeric -> numeric + auto pm_ty = obj + (DFunctionType::_make(mm, numeric_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_sub", pm_ty, &NumericDispatch::subtract); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_cmpeq_pm(obj mm) + NumericPrimitives::make_cmpeq_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpeq", + (void)stbl; + + auto booleic_ty = DAtomicType::make(mm, Metatype::t_booleic()); + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op==: numeric x numeric -> booleic + auto pm_ty = obj + (DFunctionType::_make(mm, booleic_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpeq", pm_ty, &NumericDispatch::cmp_equal); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_cmpne_pm(obj mm) + NumericPrimitives::make_cmpne_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpne", + (void)stbl; + + auto booleic_ty = DAtomicType::make(mm, Metatype::t_booleic()); + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op!=: numeric x numeric -> booleic + auto pm_ty = obj + (DFunctionType::_make(mm, booleic_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpne", pm_ty, &NumericDispatch::cmp_notequal); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_cmplt_pm(obj mm) + NumericPrimitives::make_cmplt_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_cmplt", + (void)stbl; + + auto booleic_ty = DAtomicType::make(mm, Metatype::t_booleic()); + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op!=: numeric x numeric -> booleic + auto pm_ty = obj + (DFunctionType::_make(mm, booleic_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_cmplt", pm_ty, &NumericDispatch::cmp_less); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_cmple_pm(obj mm) + NumericPrimitives::make_cmple_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_cmple", + (void)stbl; + + auto booleic_ty = DAtomicType::make(mm, Metatype::t_booleic()); + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op!=: numeric x numeric -> booleic + auto pm_ty = obj + (DFunctionType::_make(mm, booleic_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_cmple", pm_ty, &NumericDispatch::cmp_lessequal); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_cmpgt_pm(obj mm) + NumericPrimitives::make_cmpgt_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpgt", + (void)stbl; + + auto booleic_ty = DAtomicType::make(mm, Metatype::t_booleic()); + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op!=: numeric x numeric -> booleic + auto pm_ty = obj + (DFunctionType::_make(mm, booleic_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpgt", pm_ty, &NumericDispatch::cmp_greater); } DPrimitive_gco_2_gco_gco * - NumericPrimitives::make_cmpge_pm(obj mm) + NumericPrimitives::make_cmpge_pm(obj mm, + StringTable * stbl) { - return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpge", + (void)stbl; + + auto booleic_ty = DAtomicType::make(mm, Metatype::t_booleic()); + auto numeric_ty = DAtomicType::make(mm, Metatype::t_numeric()); + // #op!=: numeric x numeric -> booleic + auto pm_ty = obj + (DFunctionType::_make(mm, booleic_ty, numeric_ty, numeric_ty)); + + return DPrimitive_gco_2_gco_gco::_make(mm, "_cmpge", pm_ty, &NumericDispatch::cmp_greatequal); } diff --git a/xo-numeric/src/numeric/numeric_register_primitives.cpp b/xo-numeric/src/numeric/numeric_register_primitives.cpp index 20eb5967..6150df9f 100644 --- a/xo-numeric/src/numeric/numeric_register_primitives.cpp +++ b/xo-numeric/src/numeric/numeric_register_primitives.cpp @@ -29,6 +29,7 @@ namespace xo { } } +#ifdef OBSOLETE bool install_aux(InstallSink sink, obj mm, std::string_view name, @@ -46,6 +47,7 @@ namespace xo { return true; } } +#endif } bool @@ -62,30 +64,32 @@ namespace xo { NumericPrimitives::make_multiply_pm(mm, stbl), flags & InstallFlags::f_essential); ok = ok & install_aux(sink, - NumericPrimitives::make_divide_pm(mm), + NumericPrimitives::make_divide_pm(mm, stbl), flags & InstallFlags::f_essential); ok = ok & install_aux(sink, - NumericPrimitives::make_add_pm(mm), + NumericPrimitives::make_add_pm(mm, stbl), flags & InstallFlags::f_essential); ok = ok & install_aux(sink, - NumericPrimitives::make_subtract_pm(mm), + NumericPrimitives::make_subtract_pm(mm, stbl), flags & InstallFlags::f_essential); ok = ok & install_aux(sink, - NumericPrimitives::make_cmpeq_pm(mm), + NumericPrimitives::make_cmpeq_pm(mm, stbl), flags & InstallFlags::f_essential); ok = ok & install_aux(sink, - NumericPrimitives::make_cmpne_pm(mm), + NumericPrimitives::make_cmpne_pm(mm, stbl), flags & InstallFlags::f_essential); ok = ok & install_aux(sink, - NumericPrimitives::make_cmplt_pm(mm), + NumericPrimitives::make_cmplt_pm(mm, stbl), flags & InstallFlags::f_essential); ok = ok & install_aux(sink, - NumericPrimitives::make_cmple_pm(mm), + NumericPrimitives::make_cmple_pm(mm, stbl), flags & InstallFlags::f_essential); - ok = ok & install_aux(sink, mm, "_cmpgt", &NumericDispatch::cmp_greater, + ok = ok & install_aux(sink, + NumericPrimitives::make_cmpgt_pm(mm, stbl), flags & InstallFlags::f_essential); - ok = ok & install_aux(sink, mm, "_cmpge", &NumericDispatch::cmp_greatequal, + ok = ok & install_aux(sink, + NumericPrimitives::make_cmpge_pm(mm, stbl), flags & InstallFlags::f_essential); return ok; diff --git a/xo-type/include/xo/type/Metatype.hpp b/xo-type/include/xo/type/Metatype.hpp index 8f7fc628..9ff612aa 100644 --- a/xo-type/include/xo/type/Metatype.hpp +++ b/xo-type/include/xo/type/Metatype.hpp @@ -48,6 +48,9 @@ namespace xo { /** any numeric type: i16|i32|i64|f32|f64 **/ t_numeric, + /** generalized boolean type: bool, function with bool codomain **/ + t_booleic, + /** any callable type (e.g. all function types) **/ t_callable, @@ -78,6 +81,7 @@ namespace xo { static Metatype t_dict() { return Metatype(code::t_dict); } static Metatype t_integer() { return Metatype(code::t_integer); } static Metatype t_numeric() { return Metatype(code::t_numeric); } + static Metatype t_booleic() { return Metatype(code::t_booleic); } static Metatype t_callable() { return Metatype(code::t_callable); } static Metatype t_any() { return Metatype(code::t_any); } diff --git a/xo-type/src/type/Metatype.cpp b/xo-type/src/type/Metatype.cpp index aa1a1bb2..1ac26f62 100644 --- a/xo-type/src/type/Metatype.cpp +++ b/xo-type/src/type/Metatype.cpp @@ -31,6 +31,7 @@ namespace xo { case code::t_dict: return "dict"; case code::t_integer: return "integer"; + case code::t_booleic: return "booleic"; case code::t_numeric: return "numeric"; case code::t_callable: return "callable"; case code::t_any: return "any"; @@ -76,6 +77,8 @@ namespace xo { case code::t_integer: return true; + case code::t_booleic: + return true; case code::t_numeric: return true; case code::t_callable: