fix: no stringify in hot loop
This commit is contained in:
3
Makefile
3
Makefile
@@ -13,6 +13,9 @@ thunk: it
|
|||||||
saccharine: it
|
saccharine: it
|
||||||
@ ./lambda.exe - < ./samples/saccharine.txt
|
@ ./lambda.exe - < ./samples/saccharine.txt
|
||||||
|
|
||||||
|
church: it
|
||||||
|
@ ./lambda.exe - < ./samples/church.txt
|
||||||
|
|
||||||
prof:
|
prof:
|
||||||
@ go tool pprof -top profile/cpu.prof
|
@ go tool pprof -top profile/cpu.prof
|
||||||
|
|
||||||
|
|||||||
@@ -41,10 +41,14 @@ func main() {
|
|||||||
// Turn tokens into syntax tree.
|
// Turn tokens into syntax tree.
|
||||||
expression, err := saccharine.Parse(tokens)
|
expression, err := saccharine.Parse(tokens)
|
||||||
cli.HandleError(err)
|
cli.HandleError(err)
|
||||||
logger.Info("parsed syntax tree", "tree", saccharine.Stringify(expression))
|
if options.Verbose {
|
||||||
|
logger.Info("parsed syntax tree", "tree", saccharine.Stringify(expression))
|
||||||
|
}
|
||||||
|
|
||||||
compiled := convert.SaccharineToLambda(expression)
|
compiled := convert.SaccharineToLambda(expression)
|
||||||
logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled))
|
if options.Verbose {
|
||||||
|
logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled))
|
||||||
|
}
|
||||||
|
|
||||||
// Reduce expression.
|
// Reduce expression.
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
@@ -56,7 +60,9 @@ func main() {
|
|||||||
steps := 0
|
steps := 0
|
||||||
|
|
||||||
for lambda.ReduceOnce(&compiled) {
|
for lambda.ReduceOnce(&compiled) {
|
||||||
logger.Info("reduction", "tree", lambda.Stringify(compiled))
|
if options.Verbose {
|
||||||
|
logger.Info("reduction", "tree", lambda.Stringify(compiled))
|
||||||
|
}
|
||||||
if options.Explanation {
|
if options.Explanation {
|
||||||
fmt.Println(" =", lambda.Stringify(compiled))
|
fmt.Println(" =", lambda.Stringify(compiled))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,16 +11,14 @@ func Substitute(e *Expression, target string, replacement Expression) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !IsFreeVariable(typed.Parameter, replacement) {
|
if IsFreeVariable(typed.Parameter, replacement) {
|
||||||
Substitute(&typed.Body, target, replacement)
|
replacementFreeVars := GetFreeVariables(replacement)
|
||||||
return
|
used := GetFreeVariables(typed.Body)
|
||||||
|
used.Merge(replacementFreeVars)
|
||||||
|
freshVar := GenerateFreshName(used)
|
||||||
|
Rename(typed, typed.Parameter, freshVar)
|
||||||
}
|
}
|
||||||
|
|
||||||
replacementFreeVars := GetFreeVariables(replacement)
|
|
||||||
used := GetFreeVariables(typed.Body)
|
|
||||||
used.Merge(replacementFreeVars)
|
|
||||||
freshVar := GenerateFreshName(used)
|
|
||||||
Rename(typed, typed.Parameter, freshVar)
|
|
||||||
Substitute(&typed.Body, target, replacement)
|
Substitute(&typed.Body, target, replacement)
|
||||||
case *Application:
|
case *Application:
|
||||||
Substitute(&typed.Abstraction, target, replacement)
|
Substitute(&typed.Abstraction, target, replacement)
|
||||||
|
|||||||
7
samples/church.txt
Normal file
7
samples/church.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
0 := \f.\x.x
|
||||||
|
inc n := \f x.(f (n f x))
|
||||||
|
exp n m := (m n)
|
||||||
|
|
||||||
|
N := (inc (inc (inc (inc (inc 0)))))
|
||||||
|
|
||||||
|
(exp N N)
|
||||||
Reference in New Issue
Block a user