Automatic decoding of supported literals in REPL
Automatic decoding & display of string, number, and list types in REPL. General updates to README, style, and comments.
This commit is contained in:
@ -34,8 +34,11 @@ _S = Fork (Stem (Fork Leaf Leaf)) Leaf
|
||||
_K :: T
|
||||
_K = Stem Leaf
|
||||
|
||||
-- Identity
|
||||
-- We use the "point-free" style which drops a redundant node
|
||||
-- Full _I form (SKK): Fork (Stem (Stem Leaf)) (Stem Leaf)
|
||||
_I :: T
|
||||
_I = apply (apply _S _K) _K -- Fork (Stem (Stem Leaf)) (Stem Leaf)
|
||||
_I = Fork (Stem (Stem Leaf)) Leaf
|
||||
|
||||
-- Booleans
|
||||
_false :: T
|
||||
@ -51,9 +54,6 @@ _not = Fork (Fork _true (Fork Leaf _false)) Leaf
|
||||
toString :: String -> T
|
||||
toString str = toList (map toNumber (map fromEnum str))
|
||||
|
||||
ofString :: T -> String
|
||||
ofString tc = map (toEnum . ofNumber) (ofList tc)
|
||||
|
||||
toNumber :: Int -> T
|
||||
toNumber 0 = Leaf
|
||||
toNumber n =
|
||||
@ -61,20 +61,31 @@ toNumber n =
|
||||
(if odd n then Stem Leaf else Leaf)
|
||||
(toNumber (n `div` 2))
|
||||
|
||||
ofNumber :: T -> Int
|
||||
ofNumber Leaf = 0
|
||||
ofNumber (Fork Leaf rest) = 2 * ofNumber rest
|
||||
ofNumber (Fork (Stem Leaf) rest) = 1 + 2 * ofNumber rest
|
||||
ofNumber _ = error "Invalid Tree Calculus number"
|
||||
|
||||
toList :: [T] -> T
|
||||
toList [] = Leaf
|
||||
toList (x:xs) = Fork x (toList xs)
|
||||
|
||||
ofList :: T -> [T]
|
||||
ofList Leaf = []
|
||||
ofList (Fork x rest) = x : ofList rest
|
||||
ofList _ = error "Invalid Tree Calculus list"
|
||||
ofNumber :: T -> Either String Int
|
||||
ofNumber Leaf = Right 0
|
||||
ofNumber (Fork Leaf rest) = case ofNumber rest of
|
||||
Right n -> Right (2 * n)
|
||||
Left err -> Left err
|
||||
ofNumber (Fork (Stem Leaf) rest) = case ofNumber rest of
|
||||
Right n -> Right (1 + 2 * n)
|
||||
Left err -> Left err
|
||||
ofNumber _ = Left "Invalid Tree Calculus number"
|
||||
|
||||
ofString :: T -> Either String String
|
||||
ofString tc = case ofList tc of
|
||||
Right list -> traverse (fmap toEnum . ofNumber) list
|
||||
Left err -> Left err
|
||||
|
||||
ofList :: T -> Either String [T]
|
||||
ofList Leaf = Right []
|
||||
ofList (Fork x rest) = case ofList rest of
|
||||
Right xs -> Right (x : xs)
|
||||
Left err -> Left err
|
||||
ofList _ = Left "Invalid Tree Calculus list"
|
||||
|
||||
-- Utility
|
||||
toAscii :: T -> String
|
||||
|
Reference in New Issue
Block a user