Day3 working

- Fix string and character encoding in output
- Fix autos not solving if another extends context
This commit is contained in:
2024-11-29 22:10:43 -08:00
parent 18e44cb7d3
commit baeaf4295d
13 changed files with 759 additions and 39 deletions

View File

@@ -1,5 +1,7 @@
module Prelude
id : a. a a
id x = x
data Bool : U where
True False : Bool
@@ -15,6 +17,11 @@ _||_ : Bool → Bool → Bool
True || _ = True
False || b = b
infixr 5 _&&_
_&&_ : Bool Bool Bool
False && b = False
True && b = b
infixl 6 _==_
class Eq a where
_==_ : a a Bool
@@ -23,6 +30,10 @@ data Nat : U where
Z : Nat
S : Nat -> Nat
pred : Nat Nat
pred Z = Z
pred (S k) = k
instance Eq Nat where
Z == Z = True
S n == S m = n == m
@@ -73,7 +84,13 @@ infixr 2 _,_
data _×_ : U U U where
_,_ : A B. A B A × B
infixl 6 _<_
fst : a b. a × b a
fst (a,b) = a
snd : a b. a × b b
snd (a,b) = b
infixl 6 _<_ _<=_
class Ord a where
_<_ : a a Bool
@@ -82,6 +99,8 @@ instance Ord Nat where
Z < S _ = True
S n < S m = n < m
_<=_ : a. {{Eq a}} {{Ord a}} a a Bool
a <= b = a == b || a < b
-- Monad
class Monad (m : U U) where
@@ -95,6 +114,9 @@ ma >>= amb = bind ma amb
_>>_ : {0 m} {{Monad m}} {0 a b} -> m a -> m b -> m b
ma >> mb = mb
join : m. {{Monad m}} {0 a} m (m a) m a
join mma = mma >>= id
-- Equality
infixl 1 _≡_
@@ -167,7 +189,6 @@ instance Mul Nat where
Z * _ = Z
S n * m = m + n * m
infixl 7 _-_
class Sub a where
_-_ : a a a
@@ -189,6 +210,17 @@ pfunc sconcat : String → String → String := `(x,y) => x + y`
instance Concat String where
_++_ = sconcat
pfunc jsEq uses (True False) : a. a a Bool := `(_, a, b) => a == b ? True : False`
instance Eq Int where
a == b = jsEq a b
instance Eq String where
a == b = jsEq a b
instance Eq Char where
a == b = jsEq a b
data Unit : U where
MkUnit : Unit
@@ -306,9 +338,6 @@ instance Show Int where
pfunc ord : Char -> Int := `(c) => c.charCodeAt(0)`
infix 6 _<=_
pfunc _<=_ uses (True False) : Int -> Int -> Bool := `(x,y) => (x <= y) ? True : False`
pfunc unpack : String -> List Char
:= `(s) => {
let acc = Nil(Char)
@@ -327,9 +356,15 @@ pfunc pack : List Char → String := `(cs) => {
}
`
pfunc debugStr : a. a String := `(_, obj) => {
pfunc debugStr uses (natToInt listToArray) : a. a String := `(_, obj) => {
const go = (obj) => {
if (obj?.tag) {
if (obj?.tag === '_::_') {
let stuff = listToArray(undefined,obj)
return '['+(stuff.map(go).join(', '))+']'
}
if (obj?.tag === 'S') {
return ''+natToInt(obj)
} else if (obj?.tag) {
let rval = '('+obj.tag
for(let i=0;;i++) {
let key = 'h'+i
@@ -398,3 +433,53 @@ span {a} f xs = go xs Nil
go (x :: xs) left = if f x
then go xs (x :: left)
else (reverse left, x :: xs)
instance Show Nat where
show n = show (natToInt n)
enumerate : a. List a List (Nat × a)
enumerate {a} xs = go Z xs
where
go : Nat List a List (Nat × a)
go k Nil = Nil
go k (x :: xs) = (k,x) :: go (S k) xs
filter : a. (a Bool) List a List a
filter pred Nil = Nil
filter pred (x :: xs) = if pred x then x :: filter pred xs else filter pred xs
drop : a. Nat -> List a -> List a
drop _ Nil = Nil
drop Z xs = xs
drop (S k) (x :: xs) = drop k xs
take : a. Nat -> List a -> List a
take Z xs = Nil
take _ Nil = Nil
take (S k) (x :: xs) = x :: take k xs
getAt : a. Nat List a Maybe a
getAt _ Nil = Nothing
getAt Z (x :: xs) = Just x
getAt (S k) (x :: xs) = getAt k xs
splitOn : a. {{Eq a}} a List a List (List a)
splitOn {a} v xs = go Nil xs
where
go : List a List a List (List a)
go acc Nil = reverse acc :: Nil
go acc (x :: xs) = if x == v
then reverse acc :: go Nil xs
else go (x :: acc) xs
class Inhabited a where
default : a
instance a. Inhabited (List a) where
default = Nil
getAt! : a. {{Inhabited a}} Nat List a a
getAt! _ Nil = default
getAt! Z (x :: xs) = x
getAt! (S k) (x :: xs) = getAt! k xs