feat: fun little program

This commit is contained in:
2025-12-28 00:53:43 -05:00
parent f4897d53a9
commit 4d81aca0b2
4 changed files with 56 additions and 15 deletions

View File

@@ -11,4 +11,7 @@ thunk: it
@ ./lambda.exe - < ./samples/thunk.txt @ ./lambda.exe - < ./samples/thunk.txt
saccharine: it saccharine: it
@ ./lambda.exe -x - < ./samples/saccharine.txt @ ./lambda.exe - < ./samples/saccharine.txt
saccharineX: it
@ ./lambda.exe -x - < ./samples/saccharine.txt > saccharine.out

View File

@@ -44,15 +44,20 @@ func main() {
fmt.Println(lambda.Stringify(compiled)) fmt.Println(lambda.Stringify(compiled))
} }
steps := 0
for lambda.ReduceOnce(&compiled) { for lambda.ReduceOnce(&compiled) {
logger.Info("reduction", "tree", lambda.Stringify(compiled)) logger.Info("reduction", "tree", lambda.Stringify(compiled))
if options.Explanation { if options.Explanation {
fmt.Println(" =", lambda.Stringify(compiled)) fmt.Println(" =", lambda.Stringify(compiled))
} }
steps++
} }
elapsed := time.Since(start).Milliseconds() elapsed := time.Since(start).Milliseconds()
fmt.Println(lambda.Stringify(compiled)) fmt.Println(lambda.Stringify(compiled))
fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms") fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms")
fmt.Fprintln(os.Stderr, "Steps:", steps)
fmt.Fprintln(os.Stderr, "Speed:", float32(steps)/(float32(elapsed)/1000), "ops")
} }

View File

@@ -149,7 +149,7 @@ func parseClause(i *TokenIterator, braces bool) (*ast.Clause, error) {
} else if len(stmts) == 0 { } else if len(stmts) == 0 {
return nil, fmt.Errorf("no statements in clause") return nil, fmt.Errorf("no statements in clause")
} else if last, ok = stmts[len(stmts)-1].(*ast.DeclareStatement); !ok { } else if last, ok = stmts[len(stmts)-1].(*ast.DeclareStatement); !ok {
return nil, fmt.Errorf("this clause contains no final return value") return nil, fmt.Errorf("this clause contains no final return value (col %d)", i.MustGet().Column)
} }
if braces { if braces {

View File

@@ -1,19 +1,52 @@
true x y := x T x y := x
false x y := y F x y := y
if b t e := (b t e)
pair a b := \c.(c a b) pair a b := \c.(c a b)
left p := (p true) left p := (p T)
false p := (p false) right p := (p F)
zero 0 1 x := x
print n := (n 0 1 end) print n := (n 0 1 end)
inc n := \0 1 x.{ fix f := (\x.(f (x x)) \x.(f (x x)))
initial := (pair true x)
onZero p := (pair false ((left p 1 0) (right p)))
onOne p := (pair (left p) (1 (right p)))
(n onZero onOne initial)
}
(print zero) some x := (pair T x)
none := \.F
isfull := left
unwrap := right
nil := none
push i l := (some (pair i l))
peek l := (left (unwrap l))
pop l := (right (unwrap l))
inc := (fix \self l.{
(if (isfull l)
(if (peek l)
(push F (self (pop l)))
(push T (pop l))
)
(push T nil)
)
})
print := (fix \self l.{
(if (isfull l)
((if (peek l) 1 0) (self (pop l)))
END
)
})
one := (push T nil)
double N := (push F N)
N :=
(double (double (double (double (double
(double (double (double (double (double
(double (double (double (double (double
(double (double (double (double (double
(double (double (double (double (double
(double (double (double (double (double
one))))))))))))))))))))))))))))))
(print N)