ref: 79340a0fa7c9cfbe44df5c5502486abef826ae5e
parent: 12bac3e8b05c9fe9baa14e4d19bb5711ba9b2dc6
author: Lennart Augustsson <lennart.augustsson@epicgames.com>
date: Fri Sep 22 09:39:20 EDT 2023
Small fixes.
--- a/README.md
+++ b/README.md
@@ -130,10 +130,15 @@
Available commands:
* `:quit` Quit the interactive system
-* `:clear` Clear all definitions
+* `:clear` Get back to start state
* `:del STR` Delete all definitions that begin with `STR`
* `expr` Evaluate expression. ***NOTE*** Currently only expressions of type `Int` are allowed.
* `defn` Add definition (can also be an `import`)
+
+***NOTE*** When you `import` a module it is cached.
+If the file changes and you import it again it will not reload.
+You can use `:clear` no get back to an empty cache.
+This is a bug.
## Runtime
The runtime system is written in C and is in `eval.c`.
--- a/comb/mhs.comb
+++ b/comb/mhs.comb
@@ -1,3 +1,3 @@
v3.5
-882
-(($A :0 _769) (($A :1 (($B _815) _0)) (($A :2 ((($S' _815) _0) $I)) (($A :3 _739) (($A :4 (_3 "undefined")) (($A :5 $I) (($A :6 ((($C' $B) _768) (($C _48) _5))) (($A :7 ((($C' _6) (_786 _45)) ((_48 _784) _44))) (($A :8 (($B (($S _815) _784)) _3)) (($A :9 $T) (($A :10 ($T $I)) (($A :11 (($B (_48 _159)) _10)) (($A :12 (($B ($B (_47 _9))) ((($C' $B) (($B $C) _10)) ($B _10)))) (($A :13 (($B ($B (_47 _9))) ((($C' $B) (($B $C) _10)) ($BK _10)))) (($A :14 (($B (_47 _9)) $P)) (($A :15 (($B ($B (_47 _9))) (($B (($C' $C) _10)) ($B $P)))) (($A :16 _15) (($A :17 (($B (_47 _9)) ($B ($P _709)))) (($A :18 (($B (_47 _9)) ($BK ($P _709)))) (($A :19 ((_47 _9) (($S $P) $I))) (($A :20 (($B (_47 _9)) (($C ($S' $P)) $I))) (($A :21 (($B $Y) (($B ($B ($P (_14 _88)))) ((($C' $B) (($B ($C' $B)) ($B _12))) ((($C' ($C' $B)) ($B _12)) (($B ($B _14)) _89)))))) (($A :22 (($B $Y) (($B ($B ($P (_14 _709)))) (($B ($C' $B)) ($B _13))))) (($A :23 _3) (($A :24 ($T (_14 _709))) (($A :25 (($C $C) _32)) (($A :26 ($T _31)) (($A :27 (($P _32) _31)) (($A :28 _32) (($A :29 (($C (($C $S') _27)) $I)) (($A :30 (($C $S) _27)) (($A :31 $K) (($A :32 $A) (($A :33 _744) (($A :34 _745) (($A :35 ((($S' _26) (_736 97)) (($C _736) 122))) (($A :36 ((($S' _26) (_736 65)) (($C _736) 90))) (($A :37 ((($S' _25) _35) _36)) (($A :38 ((($S' _26) (_736 48)) (($C _736) 57))) (($A :39 ((($S' _26) (_736 32)) (($C _736) 126))) (($A :40 _733) (($A :41 _734) (($A :42 _736) (($A :43 _735) (($A :44 (($B $BK) $T)) (($A :45 ($BK $T)) (($A :46 $P) (($A :47 $I) (($A :48 $B) (($A :49 $I) (($A :50 $K) (($A :51 $C) (($A :52 _740) (($A :53 (($C (($C $S') _159)) _160)) (($A :54 ((($C' ($S' ($C' $B))) $B) $I)) (($A :55 _710) (($A :56 _711) (($A :57 _712) (($A :58 _713) (($A :59 _714) (($A :60 _715) (($A :61 (_56 0)) (($A :62 _721) (($A :63 _722) (($A :64 _723) (($A :65 _724) (($A :66 _725) (($A :67 _726) (($A :68 _62) (($A :69 ($BK $K)) (($A :70 (($B $BK) (($B ($B $BK)) $P))) (($A :71 (($B ($B ($B $BK))) (($B ($B ($B $BK))) (($B ($B ($B $C))) (($B ($B $C)) $P))))) (($A :72 ((($S' $S) ((($S' ($S' $C)) ((($C' ($C' $S)) ((($C' $B) (($B ($S' $S')) ((($C' $B) (($B _25) (_65 0))) (_62 0)))) (($B ($B (($C' $P) (_60 1)))) _55))) ($C $P))) _58)) _59)) (($A :73 _69) (($A :74 ((($S' $C) (($B ($P _148)) ((($C' ($C' $B)) ((($C' $C) _62) _148)) _149))) (($B (($C' ($C' ($C' $C))) ((($C' ($C' ($C' $C))) ((($C' ($C' ($C' ($C' $S')))) (($B ($B ($B ($B $C)))) (($B (($C' ($C' ($C' $C))) (($B ($B ($B (($S' $S') (_62 0))))) (($B (($C' ($C' $C)) (($B ($B (($S' $S') (_62 1)))) (($B (($C' $C) (($B (($C' $S') (_62 2))) ($C _74)))) ($C _74))))) ($C _74))))) ($C _74)))) ($T $K))) ($T $A)))) (($C _72) 4)))) (($A :75 (_81 _50)) (($A :76 ((_96 (_53 _75)) _73)) (($A :77 (($C ((($C' $B) (($P _88) ((($C' ($C' $O)) $P) $K))) ((($S' ($C' ($C' ($C' $B)))) (($B ($B ($B ($B _78)))) ((($S' ($C' ($C' $B))) (($B ($B ($B _78))) ((($S' ($C' $B)) (($B ($B _78)) ((($C' $B) (($B _94) ($T 0))) _77))) ((($C' $B) (($B _94) ($T 1))) _77)))) ((($C' $B) (($B _94) ($T 2))) _77)))) ((($C' $B) (($B _94) ($T 3))) _77)))) (($B $T) (($B ($B $P)) (($C' _55) (_57 4)))))) (($A :78 (($S $S) (($B $BK) (($B $BK) ((($S' $S) $T) (($B $BK) (($B $BK) (($C ((($S' $C') $S) (($B ($B ($B ($S $B)))) (($B ($B ($B ($B ($B $BK))))) (($B (($S' ($C' $B)) (($B $B') $B'))) (($B ($B ($B ($B ($B ($S $B)))))) (($B ($B ($B ($B ($B ($B ($B $BK))))))) ((($C' $B) ($B' ($B' (($B ($C' ($C' ($C' $C)))) (($B (($C' $B) ($B' (($B $C) _64)))) (($B (($C' $B) _89)) _78)))))) (($B (($C' $B) _89)) ($C _78)))))))))) (((_708 "lib/Data/IntMap.hs") 3) 8))))))))) (($A :79 ((_48 (_94 _159)) _77)) (($A :80 ((($C' $C) ((($C' $C) ($C _74)) (_3 "Data.IntMap.!"))) $I)) (($A :81 (($B (($C' $B) $T)) (($B ($B $Y)) ((($C' ($C' ($S' ($S' $C)))) (($B (($S' $B) (($B ($S' $P)) ($C _70)))) (($B ($B (($C' ($S' $C)) (($B (($S' ($S' $S')) _62)) ((($C' ($C' $B)) (($B $B') ($B _47))) ((($C' ($C' _47)) _75) ((((_71 _69) _69) _69) _69))))))) ($B (($C' $B) _70))))) ((($C' $B) (($B ($C' ($C' ($C' $C)))) ((($C' ($C' ($C' ($C' $C)))) ((($C' ($C' ($C' ($C' ($C' $S'))))) (($B ($B ($B ($B ($B $C))))) ((($S' ($S' ($S' ($S' ($S' $C))
\ No newline at end of file
+883
+(($A :0 _770) (($A :1 (($B _816) _0)) (($A :2 ((($S' _816) _0) $I)) (($A :3 _740) (($A :4 (_3 "undefined")) (($A :5 $I) (($A :6 ((($C' $B) _769) (($C _48) _5))) (($A :7 ((($C' _6) (_787 _45)) ((_48 _785) _44))) (($A :8 (($B (($S _816) _785)) _3)) (($A :9 $T) (($A :10 ($T $I)) (($A :11 (($B (_48 _159)) _10)) (($A :12 (($B ($B (_47 _9))) ((($C' $B) (($B $C) _10)) ($B _10)))) (($A :13 (($B ($B (_47 _9))) ((($C' $B) (($B $C) _10)) ($BK _10)))) (($A :14 (($B (_47 _9)) $P)) (($A :15 (($B ($B (_47 _9))) (($B (($C' $C) _10)) ($B $P)))) (($A :16 _15) (($A :17 (($B (_47 _9)) ($B ($P _710)))) (($A :18 (($B (_47 _9)) ($BK ($P _710)))) (($A :19 ((_47 _9) (($S $P) $I))) (($A :20 (($B (_47 _9)) (($C ($S' $P)) $I))) (($A :21 (($B $Y) (($B ($B ($P (_14 _88)))) ((($C' $B) (($B ($C' $B)) ($B _12))) ((($C' ($C' $B)) ($B _12)) (($B ($B _14)) _89)))))) (($A :22 (($B $Y) (($B ($B ($P (_14 _710)))) (($B ($C' $B)) ($B _13))))) (($A :23 _3) (($A :24 ($T (_14 _710))) (($A :25 (($C $C) _32)) (($A :26 ($T _31)) (($A :27 (($P _32) _31)) (($A :28 _32) (($A :29 (($C (($C $S') _27)) $I)) (($A :30 (($C $S) _27)) (($A :31 $K) (($A :32 $A) (($A :33 _745) (($A :34 _746) (($A :35 ((($S' _26) (_737 97)) (($C _737) 122))) (($A :36 ((($S' _26) (_737 65)) (($C _737) 90))) (($A :37 ((($S' _25) _35) _36)) (($A :38 ((($S' _26) (_737 48)) (($C _737) 57))) (($A :39 ((($S' _26) (_737 32)) (($C _737) 126))) (($A :40 _734) (($A :41 _735) (($A :42 _737) (($A :43 _736) (($A :44 (($B $BK) $T)) (($A :45 ($BK $T)) (($A :46 $P) (($A :47 $I) (($A :48 $B) (($A :49 $I) (($A :50 $K) (($A :51 $C) (($A :52 _741) (($A :53 (($C (($C $S') _159)) _160)) (($A :54 ((($C' ($S' ($C' $B))) $B) $I)) (($A :55 _711) (($A :56 _712) (($A :57 _713) (($A :58 _714) (($A :59 _715) (($A :60 _716) (($A :61 (_56 0)) (($A :62 _722) (($A :63 _723) (($A :64 _724) (($A :65 _725) (($A :66 _726) (($A :67 _727) (($A :68 _62) (($A :69 ($BK $K)) (($A :70 (($B $BK) (($B ($B $BK)) $P))) (($A :71 (($B ($B ($B $BK))) (($B ($B ($B $BK))) (($B ($B ($B $C))) (($B ($B $C)) $P))))) (($A :72 ((($S' $S) ((($S' ($S' $C)) ((($C' ($C' $S)) ((($C' $B) (($B ($S' $S')) ((($C' $B) (($B _25) (_65 0))) (_62 0)))) (($B ($B (($C' $P) (_60 1)))) _55))) ($C $P))) _58)) _59)) (($A :73 _69) (($A :74 ((($S' $C) (($B ($P _148)) ((($C' ($C' $B)) ((($C' $C) _62) _148)) _149))) (($B (($C' ($C' ($C' $C))) ((($C' ($C' ($C' $C))) ((($C' ($C' ($C' ($C' $S')))) (($B ($B ($B ($B $C)))) (($B (($C' ($C' ($C' $C))) (($B ($B ($B (($S' $S') (_62 0))))) (($B (($C' ($C' $C)) (($B ($B (($S' $S') (_62 1)))) (($B (($C' $C) (($B (($C' $S') (_62 2))) ($C _74)))) ($C _74))))) ($C _74))))) ($C _74)))) ($T $K))) ($T $A)))) (($C _72) 4)))) (($A :75 (_81 _50)) (($A :76 ((_96 (_53 _75)) _73)) (($A :77 (($C ((($C' $B) (($P _88) ((($C' ($C' $O)) $P) $K))) ((($S' ($C' ($C' ($C' $B)))) (($B ($B ($B ($B _78)))) ((($S' ($C' ($C' $B))) (($B ($B ($B _78))) ((($S' ($C' $B)) (($B ($B _78)) ((($C' $B) (($B _94) ($T 0))) _77))) ((($C' $B) (($B _94) ($T 1))) _77)))) ((($C' $B) (($B _94) ($T 2))) _77)))) ((($C' $B) (($B _94) ($T 3))) _77)))) (($B $T) (($B ($B $P)) (($C' _55) (_57 4)))))) (($A :78 (($S $S) (($B $BK) (($B $BK) ((($S' $S) $T) (($B $BK) (($B $BK) (($C ((($S' $C') $S) (($B ($B ($B ($S $B)))) (($B ($B ($B ($B ($B $BK))))) (($B (($S' ($C' $B)) (($B $B') $B'))) (($B ($B ($B ($B ($B ($S $B)))))) (($B ($B ($B ($B ($B ($B ($B $BK))))))) ((($C' $B) ($B' ($B' (($B ($C' ($C' ($C' $C)))) (($B (($C' $B) ($B' (($B $C) _64)))) (($B (($C' $B) _89)) _78)))))) (($B (($C' $B) _89)) ($C _78)))))))))) (((_709 "lib/Data/IntMap.hs") 3) 8))))))))) (($A :79 ((_48 (_94 _159)) _77)) (($A :80 ((($C' $C) ((($C' $C) ($C _74)) (_3 "Data.IntMap.!"))) $I)) (($A :81 (($B (($C' $B) $T)) (($B ($B $Y)) ((($C' ($C' ($S' ($S' $C)))) (($B (($S' $B) (($B ($S' $P)) ($C _70)))) (($B ($B (($C' ($S' $C)) (($B (($S' ($S' $S')) _62)) ((($C' ($C' $B)) (($B $B') ($B _47))) ((($C' ($C' _47)) _75) ((((_71 _69) _69) _69) _69))))))) ($B (($C' $B) _70))))) ((($C' $B) (($B ($C' ($C' ($C' $C)))) ((($C' ($C' ($C' ($C' $C)))) ((($C' ($C' ($C' ($C' ($C' $S'))))) (($B ($B ($B ($B ($B $C))))) ((($S' ($S' ($S' ($S' ($S' $C))
\ No newline at end of file
--- a/lib/Data/Integer.hs
+++ b/lib/Data/Integer.hs
@@ -1,69 +1,86 @@
-- Copyright 2023 Lennart Augustsson
-- See LICENSE file for full license.
-- *** WIP, do not use! ***
-module Data.Integer(Integer) where
---Yimport Primitives(Word)
+module Data.Integer(module Data.Integer) where
+import Prelude
+{-import Control.Error
import Data.Bool
import Data.Function
-import qualified Data.Int as I
+import Data.Int
import Data.List
-import qualified Data.Word as W
+-}
data Sign = Plus | Minus
-data Integer = I Sign [Word] -- each word is <2^32, LSW first
+type Digit = Int
-zeroW :: Word
-zeroW = W.intToWord 0
+data Integer = I Sign [Digit] -- each word is <2^32, least significant digit first, no trailing 0s
-maxW :: Word
-maxW = W.intToWord 4294967296
+intToInteger :: Int -> Integer
+intToInteger i | i > 0 = I Plus (f i)
+ | i < 0 = I Minus (f (negate i)) -- XXX minInt
+ | True = I Plus []
+ where f x | x >= maxD = [rem x maxD, quot x maxD]
+ | True = [x]
-(+) :: Integer -> Integer -> Integer
-(+) (I Plus xs) (I Plus ys) = I Plus (add xs ys)
-(+) (I Plus xs) (I Minus ys) | ltW xs ys = I Minus (sub ys xs)
- | True = I Plus (sub xs ys)
-(+) (I Minus xs) (I Plus ys) | ltW ys xs = I Minus (sub xs ys)
- | True = I Plus (sub ys xs)
-(+) (I Minus xs) (I Minus ys) = I Minus (add xs ys)
+zeroD :: Digit
+zeroD = 0
-negate :: Integer -> Integer
-negate (I Plus x) = I Minus x
-negate (I Minus x) = I Plus x
+maxD :: Digit
+maxD = 4294967296 -- - 2^32
-(-) :: Integer -> Integer -> Integer
-(-) x y = x + negate y
+addI :: Integer -> Integer -> Integer
+addI (I Plus xs) (I Plus ys) = I Plus (add xs ys)
+addI (I Plus xs) (I Minus ys) | ltW xs ys = I Minus (sub ys xs)
+ | True = I Plus (sub xs ys)
+addI (I Minus xs) (I Plus ys) | ltW ys xs = I Minus (sub xs ys)
+ | True = I Plus (sub ys xs)
+addI (I Minus xs) (I Minus ys) = I Minus (add xs ys)
-add :: [Word] -> [Word] -> [Word]
-add = add' zeroW
+negateI :: Integer -> Integer
+negateI (I Plus x) = I Minus x
+negateI (I Minus x) = I Plus x
-add' :: Word -> [Word] -> [Word] -> [Word]
-add' ci (x : xs) (y : ys) = s : add' co xs ys where (s, co) = addW ci x y
-add' ci (x : xs) [] = s : add' co xs [] where (s, co) = addW ci x zeroW
-add' ci [] (y : ys) = s : add' co [] ys where (s, co) = addW ci zeroW y
-add' ci [] [] = if (W.==) ci zeroW then [] else [ci]
+subI :: Integer -> Integer -> Integer
+subI x y = addI x (negateI y)
--- Add 3 words with carry
-addW :: Word -> Word -> Word -> (Word, Word)
-addW x y z = (W.quot s maxW, W.rem s maxW) where s = (W.+) ((W.+) x y) z
+add :: [Digit] -> [Digit] -> [Digit]
+add = add' zeroD
--- We always have xs <= ys
-sub :: [Word] -> [Word] -> [Word]
-sub = sub' zeroW
+add' :: Digit -> [Digit] -> [Digit] -> [Digit]
+add' ci (x : xs) (y : ys) = s : add' co xs ys where (s, co) = addD ci x y
+add' ci (x : xs) [] = s : add' co xs [] where (s, co) = addD ci x zeroD
+add' ci [] (y : ys) = s : add' co [] ys where (s, co) = addD ci zeroD y
+add' ci [] [] = if ci == zeroD then [] else [ci]
-sub' :: Word -> [Word] -> [Word] -> [Word]
+-- Add 3 digits with carry
+addD :: Digit -> Digit -> Digit -> (Digit, Digit)
+addD x y z = (quot s maxD, rem s maxD) where s = x + y + z
+
+-- We always have xs >= ys
+sub :: [Digit] -> [Digit] -> [Digit]
+sub xs ys = trim0 (sub' zeroD xs ys)
+
+sub' :: Digit -> [Digit] -> [Digit] -> [Digit]
sub' bi (x : xs) (y : ys) = d : sub' bo xs ys where (d, bo) = subW bi x y
+sub' bi (x : xs) [] = d : sub' bo xs [] where (d, bo) = subW bi x zeroD
+sub' bi [] _ = error "sub'"
-subW :: Word -> Word -> Word -> (Word, Word)
-subW _ _ _ = error "subW"
+subW :: Digit -> Digit -> Digit -> (Digit, Digit)
+subW x y z = (quot d maxD, rem d maxD) where d = y - z + x
-ltW :: [Word] -> [Word] -> Bool
-ltW axs ays = (I.<) lxs lys || (I.==) lxs lys && cmp (reverse axs) (reverse ays)
+-- Remove trailing 0s
+trim0 :: [Digit] -> [Digit]
+trim0 = reverse . dropWhile (== 0) . reverse
+
+-- Is axs < ays?
+ltW :: [Digit] -> [Digit] -> Bool
+ltW axs ays = lxs < lys || lxs == lys && cmp (reverse axs) (reverse ays)
where
lxs = length axs
lys = length ays
- cmp (x:xs) (y:ys) = (W.<) x y || (W.==) x y && cmp xs ys
+ cmp (x:xs) (y:ys) = x < y || x == y && cmp xs ys
cmp [] [] = False
cmp _ _ = error "cmp"
--
⑨