Skip to content

Commit

Permalink
Fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinborner committed Feb 24, 2024
1 parent 750eb72 commit 5df0a1d
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/Reducer/ION.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Data.Bits ( (.|.) )
import Data.Char ( chr
, ord
)
import Data.List ( intercalate )
import qualified Data.Map as M
import Data.Map ( Map )
import Helper
Expand All @@ -35,6 +34,11 @@ data VM = VM
isComb :: Int -> Bool
isComb n = n < ncomb

-- default chr panics at high n
chr' :: Int -> Char
chr' n | n < 256 = chr n
chr' _ = '?'

new :: VM
new = VM spTop ncomb 0 mempty

Expand Down Expand Up @@ -90,7 +94,7 @@ lazy d f a vm = do
store (sp vm3) f' (store dst f' (store (dst + 1) a' vm3))

rules :: Int -> VM -> VM
rules ch vm = case chr ch of
rules ch vm = case chr' ch of
'M' -> lvm 0 (arg 1) (arg 1)
'Y' -> lvm 0 (arg 1) (app (ord 'Y') 1)
'I' -> if arg' 2 vm == load (sp vm + 1) vm
Expand Down Expand Up @@ -143,7 +147,8 @@ rules ch vm = case chr ch of
let parentVal = load (load (sp vm1 + 1) vm1 + 1) vm1
let (a, vm2) = app (ord 'f') (load (sp vm1) vm1) vm1
lazy 0 (wor a) (wor parentVal) (store (sp vm2) parentVal vm2)
_ -> error "invalid combinator"
'\0' -> store (sp vm) (arg' 1 vm) vm
_ -> error $ "invalid combinator " ++ show ch
where lvm n f g = lazy n f g vm

eval :: VM -> VM
Expand All @@ -167,7 +172,7 @@ hasVar0 :: Int -> Int -> VM -> Bool
hasVar0 db depth vm = do
let f = load db vm
let a = load (db + 1) vm
case chr f of
case chr' f of
'V' -> a == depth
'L' -> hasVar0 a (depth + 1) vm
_ -> hasVar0 f depth vm || hasVar0 a depth vm
Expand All @@ -185,7 +190,7 @@ dbIndex :: Int -> Int -> VM -> (Int, VM)
dbIndex x depth vm = do
let f = load x vm
let a = load (x + 1) vm
case chr f of
case chr' f of
'V' -> app f (depth - 1 - a) vm
'L' -> do
let (idx, vm1) = dbIndex a (depth + 1) vm
Expand All @@ -196,7 +201,7 @@ dbIndex x depth vm = do
app f' a' vm2

clapp :: (Int, Int) -> VM -> (Int, VM)
clapp (f, a) vm = case (chr f, chr a) of
clapp (f, a) vm = case (chr' f, chr' a) of
('K', 'I') -> vord 'F'
('B', 'K') -> vord 'D'
('C', 'I') -> vord 'T'
Expand Down Expand Up @@ -283,7 +288,7 @@ convertK :: Int -> VM -> (Int, Int, VM)
convertK db vm = do
let f = load db vm
let a = load (db + 1) vm
case chr f of
case chr' f of
'V' -> do
let iter n 0 vm' = (n, vm')
iter n i vm' = let (n', vm1) = app n 0 vm' in iter n' (i - 1) vm1
Expand Down Expand Up @@ -319,7 +324,7 @@ resolveExpression n _ | isComb n = error "unexpected combinator"
resolveExpression n vm = do
let f = load n vm
let a = load (n + 1) vm
case chr f of
case chr' f of
'V' -> Bruijn a
'L' -> Abstraction $ resolveExpression a vm
_ -> Application (resolveExpression f vm) (resolveExpression a vm)
Expand All @@ -338,10 +343,11 @@ parseExpression _ _ = error "invalid expression"

reduce :: Expression -> Expression
reduce e = do
let vm = new
let vm = new
let (db, vm1) = parseExpression (Abstraction e) vm
let (cl, vm2) = toCLK db vm1
let res = run cl vm2
let (idx, fin) = dbIndex (load spTop res) 0 res
let (Abstraction t) = resolveExpression idx fin
t
let (cl, vm2) = toCLK db vm1
let res = run cl vm2
let (idx, fin) = dbIndex (load spTop res) 0 res
case resolveExpression idx fin of
Abstraction t -> t
t -> error $ "unexpected result " ++ show t

0 comments on commit 5df0a1d

Please sign in to comment.