Skip to content

Commit

Permalink
Merge pull request #106 from JohanWiltink/main
Browse files Browse the repository at this point in the history
allow identifier names ending in '?'
  • Loading branch information
JohanWiltink authored Feb 1, 2024
2 parents 3c0f6e1 + 99ae4c9 commit b89b755
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
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

0 comments on commit b89b755

Please sign in to comment.