Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow identifier names ending in '?' #106

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lambda-calculus.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,12 @@ function parse(code) {
function name(i) {
if ( code[i]==='_' ) {
while ( identifierChars.test( code[i] || "" ) ) i++;
if ( code[i]==='?' ) i++;
return [sp(i),"_"];
} else if ( letters.test( code[i] || "" ) ) {
let j = i+1;
while ( identifierChars.test( code[j] || "" ) ) j++;
if ( code[j]==='?' ) j++;
return [sp(j),code.slice(i,j)];
} else
return null;
Expand Down
16 changes: 8 additions & 8 deletions tests/basics-binary-scott/solution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ shiftL = \ n . n 0 I I # mind that a shiftL in LE is a divisi

dbl = \ n . n 0 (K (shiftR0 n)) (K (shiftR0 n))

isStrictZero = \ n . n True (K False) (K False) # disallow padding zeroes # O(1)
isZero = \ n . n True isZero (K False) # allow padding zeroes # amortised O(2), so don't worry too much
isStrictZero? = \ n . n True (K False) (K False) # disallow padding zeroes # O(1)
isZero? = \ n . n True isZero? (K False) # allow padding zeroes # amortised O(2), so don't worry too much

pad = \ n . n (shiftR0 0) (B shiftR0 pad) (B shiftR1 pad)
unpad = \ n . n 0 ( \ z . ( \ unpadZ . isStrictZero unpadZ (shiftR0 unpadZ) 0 ) (unpad z) ) (B shiftR1 unpad)
isPadded = \ n . n False ( \ z . z True (K (isPadded z)) (K (isPadded z)) ) isPadded
unpad = \ n . n 0 ( \ z . ( \ unpadZ . isStrictZero? unpadZ (shiftR0 unpadZ) 0 ) (unpad z) ) (B shiftR1 unpad)
isPadded? = \ n . n False ( \ z . z True (K (isPadded? z)) (K (isPadded? z)) ) isPadded?

# instance Ord

Expand Down Expand Up @@ -99,11 +99,11 @@ bitXor = \ m n . m n
( \ zm . n ( dbl zm) (B dbl (bitXor zm)) (B shiftR1 (bitXor zm)) )
( \ zm . n (shiftR1 zm) (B shiftR1 (bitXor zm)) (B dbl (bitXor zm)) )

testBit = \ i n . isZero i
testBit = \ i n . isZero? i
(n False (testBit (pred i)) (testBit (pred i)))
(n False (K False) (K True))

bit = \ i . isZero i (shiftR0 (bit (pred i))) (succ i)
bit = \ i . isZero? i (shiftR0 (bit (pred i))) (succ i)

popCount = \ n . n 0 popCount (B succ popCount)

Expand Down Expand Up @@ -139,7 +139,7 @@ minus = \ m n . gt m n 0 (go m n)

until = \ p fn x . p x (until p fn (fn x)) x
divMod = \ m n . until (B (lt m) snd) (bimap succ shiftR0) (Pair 0 n)
\ steps nn . isZero steps
\ steps nn . isZero? steps
(divMod (minus m (shiftL nn)) n (B Pair (plus (bit (pred steps)))))
(Pair 0 m)
div = \ m n . fst (divMod m n)
Expand All @@ -160,7 +160,7 @@ gcd = \ m n . m n
(gcd (minus m n) n)
)
)
lcm = \ m n . T (gcd m n) \ g . isZero g (times (div m g) n) g
lcm = \ m n . T (gcd m n) \ g . isZero? g (times (div m g) n) g

min = \ m n . le m n n m
max = \ m n . le m n m n
2 changes: 1 addition & 1 deletion tests/basics-binary-scott/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {fromInt,toInt} = LC;
const {False,True,not,and,or,xor,implies} = solution;
const {LT,EQ,GT,compare,lt,le,eq,ge,gt} = solution;
const {Pair,fst,snd,first,second,both,bimap,curry} = solution;
const {shiftR0,shiftR1,shiftL,dbl,isStrictZero,isZero,pad,unpad,isPadded} = solution;
const {shiftR0,shiftR1,shiftL,dbl,isStrictZero,isZero,pad,unpad,"isPadded?":isPadded} = solution;
const {succ,pred} = solution;
const {bitAnd,bitOr,bitXor,testBit,bit,popCount,even,odd} = solution;
const {plus,times,minus,divMod,div,mod,pow,gcd,lcm,min,max} = solution;
Expand Down
Loading