From 10c0e43649ac393c7cb4f680cb40fb0c401081ed Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 26 May 2025 05:36:14 +0300 Subject: [PATCH] =?UTF-8?q?=E2=80=A2internal.Indistinguishable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/system.md | 49 +++++++++++++++++++++-------------------- src/builtins.h | 2 +- src/builtins/compare.c | 23 +++++++++++++++++++ src/builtins/internal.c | 12 ++++++++-- src/builtins/sysfn.c | 9 ++++++-- src/core/stuff.h | 1 + 6 files changed, 67 insertions(+), 29 deletions(-) diff --git a/docs/system.md b/docs/system.md index eed9005d..8147fda4 100644 --- a/docs/system.md +++ b/docs/system.md @@ -88,30 +88,31 @@ Argument must not be a namespace, as that is reserved for future changes. Namespace of various internal functions. May change at any time. -| name | description | -|----------------------------|-------------| -| `•internal.GC` | Run a garbage collection cycle | -| `•internal.Type` | Name of the internal type for an object | -| `•internal.ObjFlags` | Monadically, get the flags of `𝕩`. Dyadically, set the flags of `𝕩` to `𝕨`. | -| `•internal.ElType` | Element size type identifier; see `enum ElType` | -| `•internal.Keep` | Require the argument to stay alive up to this point in the program. Returns the argument, but without signaling to possible optimizations that the input and output will be equal | -| `•internal.PureKeep` | `•internal.Keep` but marked as a pure function | -| `•internal.Refc` | Reference count of the argument, if it's heap-allocated | -| `•internal.IsPure` | Whether the vm considers the argument pure (i.e. it can execute it safely for computing fills) | -| `•internal.Info` | General internal info about the object; a left argument of `1` gives more details | -| `•internal.HeapDump` | Create a heap dump file; saves to `•wdpath`-relative path `𝕩` or `CBQNHeapDump` if `𝕩` isn't an array | -| `•internal.HeapStats` | If argument is `@`, returns `⟨total heap size ⋄ used heap size⟩`. If argument is a string, prints the equivalent of `)mem the-string` | -| `•internal.HasFill` | Returns whether the argument has a fill element (may give `0` even if `1↑0⥊𝕩` doesn't error in some CBQN configurations) | -| `•internal.Squeeze` | Try to convert the argument to its most compact representation; result may have fill updated to `0` or `' '`. (input is left unchanged) | -| `•internal.DeepSqueeze` | Try to convert the argument and all its subarrays to its most compact representation; won't squeeze namespace fields | -| `•internal.ListVariations` | List the possible type variations of the argument array | -| `•internal.Variation` | Convert `𝕩` to the variation specified in `𝕨` | -| `•internal.ClearRefs` | Clear references `•internal.Variation` made for `*Inc` variations | -| `•internal.Unshare` | Get a unique, reference count 1 version of the argument; recursively unshares array items, doesn't touch namespaces | -| `•internal.EEqual` | Exactly equal (NaN equals NaN, 0 equals ¯0) | -| `•internal.Temp` | Place to test new features or temporarily expose some internal function | -| `•internal.Properties` | Various build properties | -| `•internal.Validate` | Validate that `𝕩` has correct flags set | +| name | description | +|-------------------------------|-------------| +| `•internal.GC` | Run a garbage collection cycle | +| `•internal.Type` | Name of the internal type for an object | +| `•internal.ObjFlags` | Monadically, get the flags of `𝕩`. Dyadically, set the flags of `𝕩` to `𝕨`. | +| `•internal.ElType` | Element size type identifier; see `enum ElType` | +| `•internal.Keep` | Require the argument to stay alive up to this point in the program. Returns the argument, but without signaling to possible optimizations that the input and output will be equal | +| `•internal.PureKeep` | `•internal.Keep` but marked as a pure function | +| `•internal.Refc` | Reference count of the argument, if it's heap-allocated | +| `•internal.IsPure` | Whether the vm considers the argument pure (i.e. it can execute it safely for computing fills) | +| `•internal.Info` | General internal info about the object; a left argument of `1` gives more details | +| `•internal.HeapDump` | Create a heap dump file; saves to `•wdpath`-relative path `𝕩` or `CBQNHeapDump` if `𝕩` isn't an array | +| `•internal.HeapStats` | If argument is `@`, returns `⟨total heap size ⋄ used heap size⟩`. If argument is a string, prints the equivalent of `)mem the-string` | +| `•internal.HasFill` | Returns whether the argument has a fill element (may give `0` even if `1↑0⥊𝕩` doesn't error in some CBQN configurations) | +| `•internal.Squeeze` | Try to convert the argument to its most compact representation; result may have fill updated to `0` or `' '`. (input is left unchanged) | +| `•internal.DeepSqueeze` | Try to convert the argument and all its subarrays to its most compact representation; won't squeeze namespace fields | +| `•internal.ListVariations` | List the possible type variations of the argument array | +| `•internal.Variation` | Convert `𝕩` to the variation specified in `𝕨` | +| `•internal.ClearRefs` | Clear references `•internal.Variation` made for `*Inc` variations | +| `•internal.Unshare` | Get a unique, reference count 1 version of the argument; recursively unshares array items, doesn't touch namespaces | +| `•internal.EEqual` | Exactly equal (`𝕨≡𝕩` but NaN equals NaN) | +| `•internal.Indistinguishable` | Semantically indistinguishable (`•internal.EEqual`, plus checking fills) | +| `•internal.Temp` | Place to test new features or temporarily expose some internal function | +| `•internal.Properties` | Various build properties | +| `•internal.Validate` | Validate that `𝕩` has correct flags set | # FFI diff --git a/src/builtins.h b/src/builtins.h index 7d301637..de2c6aa5 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -20,7 +20,7 @@ /* inverse.c*/M(setInvReg,"(SetInvReg)") M(setInvSwap,"(SetInvSwap)") M(nativeInvReg,"(NativeInvReg)") M(nativeInvSwap,"(NativeInvSwap)") \ /*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") \ /*internal.c*/M(heapDump,"•internal.HeapDump") M(internalGC,"•internal.GC") M(heapStats,"•internal.HeapStats") A(iObjFlags,"•internal.ObjFlags") \ -/*internal.c*/D(eequal,"•internal.EEqual") M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") M(iValidate,"•internal.Validate") \ +/*internal.c*/D(eequal,"•internal.EEqual") D(indistinguishable,"•internal.Indistinguishable") M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") M(iValidate,"•internal.Validate") \ /*internal.c*/A(internalTemp,"•internal.Temp") M(iHasFill,"•internal.HasFill") M(iKeep,"•internal.Keep") D(iProperties,"•internal.Properties") \ /*internal.c*/D(variation,"•internal.Variation") A(listVariations,"•internal.ListVariations") M(clearRefs,"•internal.ClearRefs") M(unshare,"•internal.Unshare") \ /* arithd.c*/D(hypot,"•math.Hypot") D(comb,"•math.Comb") D(gcd,"•math.GCD") D(lcm,"•math.LCM") D(atan2,"•math.Atan2") D(atan2ix,"•math.Atan2⁼") D(atan2iw,"•math.Atan2˜⁼") \ diff --git a/src/builtins/compare.c b/src/builtins/compare.c index 5bebe767..7701f505 100644 --- a/src/builtins/compare.c +++ b/src/builtins/compare.c @@ -85,6 +85,29 @@ static bool atomEEqual(B w, B x) { // doesn't consume return atomEqualF(w, x); } +bool indistinguishable(B w, B x) { + if (w.u == x.u) return true; + if (isAtm(w)) { + if (!isAtm(x)) return false; + if (isF64(w)) return isF64(x) && floatIndistinguishable(o2fG(w), o2fG(x)); + if (!isVal(w) || !isVal(x)) return false; // incl. non-equal c32 + return decomposeEqual(w, x, indistinguishable); + } + if (isAtm(x)) return false; + u8 we = TI(w,elType); + u8 xe = TI(x,elType); + if (we!=el_B && xe!=el_B) { + if (elNum(we) != elNum(xe)) return false; + return eequal(w, x); // fast path + } + if (!eqShape(w, x)) return false; + usz wia = IA(w); + SGetU(w) SGetU(x) + for (ux i = 0; i < wia; i++) if (!indistinguishable(GetU(w,i), GetU(x,i))) return false; + if (!indistinguishable(getFillN(w), getFillN(x))) return false; + return true; +} + static const u8 n = 99; u8 const matchFnData[] = { // for the main diagonal, amount to shift length by; otherwise, whether to swap arguments 0,0,0,0,0,n,n,n, diff --git a/src/builtins/internal.c b/src/builtins/internal.c index 7e02cb41..edf39e3e 100644 --- a/src/builtins/internal.c +++ b/src/builtins/internal.c @@ -285,6 +285,14 @@ B eequal_c2(B t, B w, B x) { return m_i32(r); } +bool indistinguishable(B w, B x); + +B indistinguishable_c2(B t, B w, B x) { + bool r = indistinguishable(w, x); + dec(w); dec(x); + return m_i32(r); +} + #ifdef TEST_BITCPY #include "../utils/mut.h" #endif @@ -487,8 +495,8 @@ B getInternalNS(void) { #undef F #define F(X) incG(bi_##X), - Body* d = m_nnsDesc("type","eltype","refc","squeeze","ispure","info", "keep", "purekeep","listvariations","variation","clearrefs", "hasfill","unshare","deepsqueeze","heapdump","eequal", "gc", "temp","heapstats", "objflags", "properties", "validate"); - internalNS = m_nns(d,F(itype)F(elType)F(refc)F(squeeze)F(isPure)F(info)F(iKeep)F(iPureKeep)F(listVariations)F(variation)F(clearRefs)F(iHasFill)F(unshare)F(deepSqueeze)F(heapDump)F(eequal)F(internalGC)F(internalTemp)F(heapStats)F(iObjFlags)F(iProperties)F(iValidate)); + Body* d = m_nnsDesc("type","eltype","refc","squeeze","ispure","info", "keep", "purekeep","listvariations","variation","clearrefs", "hasfill","unshare","deepsqueeze","heapdump","eequal", "gc", "temp","heapstats", "objflags", "properties", "validate", "indistinguishable"); + internalNS = m_nns(d,F(itype)F(elType)F(refc)F(squeeze)F(isPure)F(info)F(iKeep)F(iPureKeep)F(listVariations)F(variation)F(clearRefs)F(iHasFill)F(unshare)F(deepSqueeze)F(heapDump)F(eequal)F(internalGC)F(internalTemp)F(heapStats)F(iObjFlags)F(iProperties)F(iValidate)F( indistinguishable)); #undef F gc_add(internalNS); } diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index beb669ec..bc6fe15e 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -1990,8 +1990,13 @@ u32* const dsv_text[] = { U"•file.Accessed",U"•file.At",U"•file.Bytes",U"•file.Chars",U"•file.Created",U"•file.CreateDir",U"•file.Exists",U"•file.Lines",U"•file.List", U"•file.MapBytes",U"•file.Modified",U"•file.Name",U"•file.Parent",U"•file.path",U"•file.RealPath",U"•file.Remove",U"•file.Rename",U"•file.Size",U"•file.Type", - U"•internal.ClearRefs",U"•internal.DeepSqueeze",U"•internal.EEqual",U"•internal.ElType",U"•internal.GC",U"•internal.HasFill",U"•internal.HeapDump",U"•internal.HeapStats",U"•internal.Info",U"•internal.IsPure",U"•internal.Keep",U"•internal.ListVariations",U"•internal.ObjFlags",U"•internal.PureKeep",U"•internal.Refc",U"•internal.Squeeze",U"•internal.Temp",U"•internal.Type",U"•internal.Unshare",U"•internal.Validate",U"•internal.Variation", - U"•math.Acos",U"•math.Acosh",U"•math.Asin",U"•math.Asinh",U"•math.Atan",U"•math.Atan2",U"•math.Atanh",U"•math.Cbrt",U"•math.Comb",U"•math.Cos",U"•math.Cosh",U"•math.Erf",U"•math.ErfC",U"•math.Expm1",U"•math.Fact",U"•math.GCD",U"•math.Hypot",U"•math.LCM",U"•math.Log10",U"•math.Log1p",U"•math.Log2",U"•math.LogFact",U"•math.Sin",U"•math.Sinh",U"•math.Sum",U"•math.Tan",U"•math.Tanh", + U"•internal.ClearRefs",U"•internal.DeepSqueeze",U"•internal.EEqual",U"•internal.ElType",U"•internal.GC",U"•internal.HasFill",U"•internal.HeapDump", + U"•internal.HeapStats",U"•internal.Indistinguishable",U"•internal.Info",U"•internal.IsPure",U"•internal.Keep",U"•internal.ListVariations",U"•internal.ObjFlags", + U"•internal.PureKeep",U"•internal.Refc",U"•internal.Squeeze",U"•internal.Temp",U"•internal.Type",U"•internal.Unshare",U"•internal.Validate",U"•internal.Variation", + + U"•math.Acos",U"•math.Acosh",U"•math.Asin",U"•math.Asinh",U"•math.Atan",U"•math.Atan2",U"•math.Atanh",U"•math.Cbrt",U"•math.Comb",U"•math.Cos",U"•math.Cosh", + U"•math.Erf",U"•math.ErfC",U"•math.Expm1",U"•math.Fact",U"•math.GCD",U"•math.Hypot",U"•math.LCM",U"•math.Log10",U"•math.Log1p",U"•math.Log2",U"•math.LogFact", + U"•math.Sin",U"•math.Sinh",U"•math.Sum",U"•math.Tan",U"•math.Tanh", U"•ns.Get",U"•ns.Has",U"•ns.Keys", U"•platform.bqn.impl",U"•platform.bqn.implVersion",U"•platform.cpu.arch",U"•platform.environment",U"•platform.os", diff --git a/src/core/stuff.h b/src/core/stuff.h index 0dcaff5a..dbab512e 100644 --- a/src/core/stuff.h +++ b/src/core/stuff.h @@ -343,6 +343,7 @@ SHOULD_INLINE i32 floatCompare(f64 w, f64 x) { // w •Cmp x SHOULD_INLINE bool floatIndistinguishable(f64 a, f64 b) { // whether the floats are indistinguishable by BQN semantics return a==b || (a!=a && b!=b); } +NOINLINE bool indistinguishable(B w, B x); // whether w and x are semantically indistinguishable (i.e. NaN==NaN, fill equality is checked; different internal type/metadata is tolerated) NOINLINE i32 compareF(B w, B x); static i32 compare(B w, B x) { // doesn't consume; -1 if wx, 0 if w≡x