summaryrefslogtreecommitdiff
path: root/lc
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-02-03 16:13:43 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-02-03 16:13:43 +0100
commita164b9de2bf9bd7e89a36e7bc390391bd7bd70b1 (patch)
treef8bfaac3c466a91be6aff2a59c2c1c3749f05397 /lc
parent96c593562a50822f246284b1212e260a60bbbc9f (diff)
more uniform handling of literals
Diffstat (limited to 'lc')
-rw-r--r--lc/Builtins.lc2
-rw-r--r--lc/Internals.lc29
-rw-r--r--lc/Prelude.lc23
3 files changed, 34 insertions, 20 deletions
diff --git a/lc/Builtins.lc b/lc/Builtins.lc
index b34f5fae..c0af99c2 100644
--- a/lc/Builtins.lc
+++ b/lc/Builtins.lc
@@ -10,8 +10,6 @@ id x = x
10 10
11--------------------------------------- 11---------------------------------------
12 12
13data Nat = Zero | Succ Nat
14
15data List a = Nil | Cons a (List a) 13data List a = Nil | Cons a (List a)
16 14
17infixr 5 : 15infixr 5 :
diff --git a/lc/Internals.lc b/lc/Internals.lc
index 0ea96032..b7d55b51 100644
--- a/lc/Internals.lc
+++ b/lc/Internals.lc
@@ -58,9 +58,12 @@ data Bool = False | True
58 58
59data Ordering = LT | EQ | GT 59data Ordering = LT | EQ | GT
60 60
61data Nat = Zero | Succ Nat
62
61-- builtin primitives 63-- builtin primitives
62primIntToWord :: Int -> Word 64primIntToWord :: Int -> Word
63primIntToFloat :: Int -> Float 65primIntToFloat :: Int -> Float
66primIntToNat :: Int -> Nat
64primCompareInt :: Int -> Int -> Ordering 67primCompareInt :: Int -> Int -> Ordering
65primCompareWord :: Word -> Word -> Ordering 68primCompareWord :: Word -> Word -> Ordering
66primCompareFloat :: Float -> Float -> Ordering 69primCompareFloat :: Float -> Float -> Ordering
@@ -80,6 +83,9 @@ primIfThenElse :: Bool -> a -> a -> a
80primIfThenElse True a b = a 83primIfThenElse True a b = a
81primIfThenElse False a b = b 84primIfThenElse False a b = b
82 85
86isEQ EQ = True
87isEQ _ = False
88
83-- fromInt is needed for integer literal 89-- fromInt is needed for integer literal
84class Num a where 90class Num a where
85 fromInt :: Int -> a 91 fromInt :: Int -> a
@@ -98,5 +104,26 @@ instance Num Float where
98 fromInt = primIntToFloat 104 fromInt = primIntToFloat
99 compare = primCompareFloat 105 compare = primCompareFloat
100 negate = primNegateFloat 106 negate = primNegateFloat
101 107instance Num Nat where
108 fromInt = primIntToNat --if isEQ (primCompareInt n zero') then Zero else Succ (fromInt (primSubInt n one'))
109 compare = undefined
110 negate = undefined
111
112class Eq a where
113 (==) :: a -> a -> Bool -- todo: use (==) sign
114
115infix 4 ==
116
117instance Eq String where a == b = isEQ (primCompareString a b)
118instance Eq Char where a == b = isEQ (primCompareChar a b)
119instance Eq Int where a == b = isEQ (primCompareInt a b)
120instance Eq Float where a == b = isEQ (primCompareFloat a b)
121instance Eq Bool where
122 True == True = True
123 False == False = True
124 _ == _ = False
125instance Eq Nat where
126 Zero == Zero = True
127 Succ a == Succ b = a == b
128 _ == _ = False
102 129
diff --git a/lc/Prelude.lc b/lc/Prelude.lc
index e8334192..13911a83 100644
--- a/lc/Prelude.lc
+++ b/lc/Prelude.lc
@@ -105,9 +105,6 @@ data RecordC (xs :: [(String, Type)])
105foldr1 f [x] = x 105foldr1 f [x] = x
106foldr1 f (x: xs) = f x (foldr1 f xs) 106foldr1 f (x: xs) = f x (foldr1 f xs)
107 107
108isEQ EQ = True
109isEQ _ = False
110
111False ||| x = x 108False ||| x = x
112True ||| x = True 109True ||| x = True
113 110
@@ -118,20 +115,12 @@ False &&& x = False
118 115
119infixr 3 &&& 116infixr 3 &&&
120 117
121class Eq a where
122 (===) :: a -> a -> Bool -- todo: use (==) sign
123
124infix 4 ===
125
126instance Eq String where
127 a === b = isEQ (primCompareString a b)
128
129------------------------------------ Row polymorphism 118------------------------------------ Row polymorphism
130-- todo: sorted field names (more efficient & easier to use) 119-- todo: sorted field names (more efficient & easier to use)
131 120
132{- 121{-
133isKey _ [] = False 122isKey _ [] = False
134isKey s ((s', _): ss) = s === s' ||| isKey s ss 123isKey s ((s', _): ss) = s == s' ||| isKey s ss
135 124
136subList [] _ = [] 125subList [] _ = []
137subList ((s, t): xs) ys = if isKey s ys then subList xs ys else (s, t): subList xs ys 126subList ((s, t): xs) ys = if isKey s ys then subList xs ys else (s, t): subList xs ys
@@ -140,7 +129,7 @@ addList [] ys = ys
140addList ((s, t): xs) ys = if isKey s ys then addList xs ys else (s, t): addList xs ys 129addList ((s, t): xs) ys = if isKey s ys then addList xs ys else (s, t): addList xs ys
141 130
142findEq x [] = 'Unit 131findEq x [] = 'Unit
143findEq (s, t) ((s', t'):xs) = if s === s' then 'T2 (t ~ t') (findEq (s, t) xs) else findEq (s, t) xs 132findEq (s, t) ((s', t'):xs) = if s == s' then 'T2 (t ~ t') (findEq (s, t) xs) else findEq (s, t) xs
144 133
145sameEq [] _ = 'Unit 134sameEq [] _ = 'Unit
146sameEq (x: xs) ys = 'T2 (findEq x ys) (sameEq xs ys) 135sameEq (x: xs) ys = 'T2 (findEq x ys) (sameEq xs ys)
@@ -162,11 +151,11 @@ record :: [(String, Type)] -> Type
162unsafeCoerce :: forall a b . a -> b 151unsafeCoerce :: forall a b . a -> b
163 152
164isKeyC _ _ [] = 'Empty "" 153isKeyC _ _ [] = 'Empty ""
165isKeyC s t ((s', t'): ss) = if s === s' then t ~ t' else isKeyC s t ss 154isKeyC s t ((s', t'): ss) = if s == s' then t ~ t' else isKeyC s t ss
166 155
167-- todo: don't use unsafeCoerce 156-- todo: don't use unsafeCoerce
168project :: forall a (xs :: [(String, Type)]) . forall (s :: String) -> 'isKeyC s a xs => RecordC xs -> a 157project :: forall a (xs :: [(String, Type)]) . forall (s :: String) -> 'isKeyC s a xs => RecordC xs -> a
169project @a @((s', a'): xs) s @_ (RecordCons ts) | s === s' = fst (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts) 158project @a @((s', a'): xs) s @_ (RecordCons ts) | s == s' = fst (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts)
170project @a @((s', a'): xs) s @_ (RecordCons ts) = project @a @xs s @(undefined @(isKeyC s a xs)) (RecordCons (snd (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts))) 159project @a @((s', a'): xs) s @_ (RecordCons ts) = project @a @xs s @(undefined @(isKeyC s a xs)) (RecordCons (snd (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts)))
171 160
172--------------------------------------- HTML colors 161--------------------------------------- HTML colors
@@ -250,7 +239,7 @@ refract = PrimRefract
250-- operators 239-- operators
251infixl 7 *, /, % 240infixl 7 *, /, %
252infixl 6 +, - 241infixl 6 +, -
253infix 4 ==, /=, <, <=, >=, > 242infix 4 /=, <, <=, >=, >
254 243
255infixr 3 && 244infixr 3 &&
256infixr 2 || 245infixr 2 ||
@@ -272,7 +261,7 @@ a % b = PrimMod a b
272neg a = PrimNeg a 261neg a = PrimNeg a
273 262
274-- comparison 263-- comparison
275a == b = PrimEqual a b 264--a == b = PrimEqual a b
276a /= b = PrimNotEqual a b 265a /= b = PrimNotEqual a b
277a < b = PrimLessThan a b 266a < b = PrimLessThan a b
278a <= b = PrimLessThanEqual a b 267a <= b = PrimLessThanEqual a b