Skip to content

Commit f5b1314

Browse files
committed
fix a bug in under both
1 parent fc11217 commit f5b1314

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

site/primitives.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
"args": 1,
431431
"outputs": 1,
432432
"class": "MonadicArray",
433-
"description": "Encode an array as bits (LSB-first) **Warning** Due to floating point imprecision, conversion (both [bits] and [un][bits]) performed on large numbers (over 53 bits long) may give incorrect results."
433+
"description": "Encode an array as bits (LSB-first)"
434434
},
435435
"both": {
436436
"glyph": "",

src/compile/invert/under.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,29 @@ under!(DipPat, input, g_sig, inverse, asm, Dip, span, [f], {
378378
Ok((&[], before, after))
379379
});
380380

381-
under!(BothPat, input, g_sig, inverse, asm, Both, span, [f], {
381+
under!(BothPat, input, g_sig, inverse, asm, {
382+
let (input, val) = if let Ok((input, val)) = Val.invert_extract(input, asm) {
383+
(input, Some(val))
384+
} else {
385+
(input, None)
386+
};
387+
let [Mod(Both, args, span), input @ ..] = input else {
388+
return generic();
389+
};
390+
let span = *span;
391+
let [f] = args.as_slice() else {
392+
return generic();
393+
};
382394
let inner_g_sig = Signature::new(
383395
g_sig.args.saturating_sub(1),
384396
g_sig.outputs.saturating_sub(1),
385397
);
386398
let (f_before, mut f_after) = f.under_inverse(inner_g_sig, inverse, asm)?;
387399

388-
let balanced = g_sig.args <= g_sig.outputs;
400+
let balanced = g_sig.args <= g_sig.outputs && !(val.is_some() && f.sig == (1, 1));
389401
// Make before
390-
let before = if !inverse || balanced {
402+
let mut before = val.unwrap_or_default();
403+
before.push(if !inverse || balanced {
391404
Mod(Both, eco_vec![f_before], span)
392405
} else {
393406
let node = f_before.node;
@@ -404,7 +417,7 @@ under!(BothPat, input, g_sig, inverse, asm, Both, span, [f], {
404417
node.prepend(val);
405418
}
406419
node
407-
};
420+
});
408421
// Make after
409422
let after = if inverse || balanced {
410423
ImplMod(UnBoth, eco_vec![f_after], span)

tests/under.ua

+2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ G ← ⍜(⊙⊙⊢) ⋅⊙∘
378378
⍤⤙≍ map "abc" [1 2 3] ⍜insert∘ @d 4 map "abc" [1 2 3]
379379
⍤⤙≍ map "abc" [1 2 3] ⍜remove∘ @a map "abc" [1 2 3]
380380
⍤⤙≍ map "abc" [1 2 3] ⍜remove∘ @d map "abc" [1 2 3]
381+
⍤⤙≍ ⟜⍜(get"abc")∘ map {"abc"} [1]
382+
⍤⤙≍ □⟜⍜(◇get"abc")∘ map {"abc"} [1]
381383

382384
# Pattern matching
383385
⍤⤙≍ "2 - 4" ⍜°$"_ - _"∩(×2⋕) "1 - 2"

0 commit comments

Comments
 (0)