-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEval.elm
More file actions
37 lines (31 loc) · 1.15 KB
/
Eval.elm
File metadata and controls
37 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Eval exposing (Step(..), parseEval, unstep)
import Dict exposing (Dict)
import Lambda exposing (DeBruijn(..), Expr(..), deBruijnBeta, equivalent, toDeBruijn)
import Parser exposing (Parser, run)
type Step a = Initial a | Intermediate a | Finished String a
unstep : Step DeBruijn -> DeBruijn
unstep ex = case ex of
Intermediate e -> e
Initial e -> e
Finished reason e -> e
evalDB : Int -> Int -> DeBruijn -> List (Step DeBruijn)
evalDB n maxDepth ex =
let newEx = deBruijnBeta ex in
if equivalent ex newEx then
[Finished "Reached normal form." ex]
else if n >= maxDepth then
[Finished "Ran out of time." ex]
else if n == 0 then
Initial ex :: evalDB (n+1) maxDepth newEx
else
Intermediate ex :: evalDB (n+1) maxDepth newEx
parseAppend : Parser Expr -> String -> List Expr -> List Expr
parseAppend p str hist = case run p str of
Ok ex -> ex :: hist
Err _ -> hist
parseEval : Parser Expr -> String -> Int -> List (Step DeBruijn)
parseEval p str maxDepth = case run p str of
Ok ex -> (case toDeBruijn 0 Dict.empty ex of
Just e -> evalDB 0 maxDepth e
Nothing -> [Initial (DVar 666)])
Err _ -> []