diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-02-03 16:13:43 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-02-03 16:13:43 +0100 |
commit | a164b9de2bf9bd7e89a36e7bc390391bd7bd70b1 (patch) | |
tree | f8bfaac3c466a91be6aff2a59c2c1c3749f05397 /lc | |
parent | 96c593562a50822f246284b1212e260a60bbbc9f (diff) |
more uniform handling of literals
Diffstat (limited to 'lc')
-rw-r--r-- | lc/Builtins.lc | 2 | ||||
-rw-r--r-- | lc/Internals.lc | 29 | ||||
-rw-r--r-- | lc/Prelude.lc | 23 |
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 | ||
13 | data Nat = Zero | Succ Nat | ||
14 | |||
15 | data List a = Nil | Cons a (List a) | 13 | data List a = Nil | Cons a (List a) |
16 | 14 | ||
17 | infixr 5 : | 15 | infixr 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 | ||
59 | data Ordering = LT | EQ | GT | 59 | data Ordering = LT | EQ | GT |
60 | 60 | ||
61 | data Nat = Zero | Succ Nat | ||
62 | |||
61 | -- builtin primitives | 63 | -- builtin primitives |
62 | primIntToWord :: Int -> Word | 64 | primIntToWord :: Int -> Word |
63 | primIntToFloat :: Int -> Float | 65 | primIntToFloat :: Int -> Float |
66 | primIntToNat :: Int -> Nat | ||
64 | primCompareInt :: Int -> Int -> Ordering | 67 | primCompareInt :: Int -> Int -> Ordering |
65 | primCompareWord :: Word -> Word -> Ordering | 68 | primCompareWord :: Word -> Word -> Ordering |
66 | primCompareFloat :: Float -> Float -> Ordering | 69 | primCompareFloat :: Float -> Float -> Ordering |
@@ -80,6 +83,9 @@ primIfThenElse :: Bool -> a -> a -> a | |||
80 | primIfThenElse True a b = a | 83 | primIfThenElse True a b = a |
81 | primIfThenElse False a b = b | 84 | primIfThenElse False a b = b |
82 | 85 | ||
86 | isEQ EQ = True | ||
87 | isEQ _ = False | ||
88 | |||
83 | -- fromInt is needed for integer literal | 89 | -- fromInt is needed for integer literal |
84 | class Num a where | 90 | class 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 | 107 | instance 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 | |||
112 | class Eq a where | ||
113 | (==) :: a -> a -> Bool -- todo: use (==) sign | ||
114 | |||
115 | infix 4 == | ||
116 | |||
117 | instance Eq String where a == b = isEQ (primCompareString a b) | ||
118 | instance Eq Char where a == b = isEQ (primCompareChar a b) | ||
119 | instance Eq Int where a == b = isEQ (primCompareInt a b) | ||
120 | instance Eq Float where a == b = isEQ (primCompareFloat a b) | ||
121 | instance Eq Bool where | ||
122 | True == True = True | ||
123 | False == False = True | ||
124 | _ == _ = False | ||
125 | instance 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)]) | |||
105 | foldr1 f [x] = x | 105 | foldr1 f [x] = x |
106 | foldr1 f (x: xs) = f x (foldr1 f xs) | 106 | foldr1 f (x: xs) = f x (foldr1 f xs) |
107 | 107 | ||
108 | isEQ EQ = True | ||
109 | isEQ _ = False | ||
110 | |||
111 | False ||| x = x | 108 | False ||| x = x |
112 | True ||| x = True | 109 | True ||| x = True |
113 | 110 | ||
@@ -118,20 +115,12 @@ False &&& x = False | |||
118 | 115 | ||
119 | infixr 3 &&& | 116 | infixr 3 &&& |
120 | 117 | ||
121 | class Eq a where | ||
122 | (===) :: a -> a -> Bool -- todo: use (==) sign | ||
123 | |||
124 | infix 4 === | ||
125 | |||
126 | instance 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 | {- |
133 | isKey _ [] = False | 122 | isKey _ [] = False |
134 | isKey s ((s', _): ss) = s === s' ||| isKey s ss | 123 | isKey s ((s', _): ss) = s == s' ||| isKey s ss |
135 | 124 | ||
136 | subList [] _ = [] | 125 | subList [] _ = [] |
137 | subList ((s, t): xs) ys = if isKey s ys then subList xs ys else (s, t): subList xs ys | 126 | subList ((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 | |||
140 | addList ((s, t): xs) ys = if isKey s ys then addList xs ys else (s, t): addList xs ys | 129 | addList ((s, t): xs) ys = if isKey s ys then addList xs ys else (s, t): addList xs ys |
141 | 130 | ||
142 | findEq x [] = 'Unit | 131 | findEq x [] = 'Unit |
143 | findEq (s, t) ((s', t'):xs) = if s === s' then 'T2 (t ~ t') (findEq (s, t) xs) else findEq (s, t) xs | 132 | findEq (s, t) ((s', t'):xs) = if s == s' then 'T2 (t ~ t') (findEq (s, t) xs) else findEq (s, t) xs |
144 | 133 | ||
145 | sameEq [] _ = 'Unit | 134 | sameEq [] _ = 'Unit |
146 | sameEq (x: xs) ys = 'T2 (findEq x ys) (sameEq xs ys) | 135 | sameEq (x: xs) ys = 'T2 (findEq x ys) (sameEq xs ys) |
@@ -162,11 +151,11 @@ record :: [(String, Type)] -> Type | |||
162 | unsafeCoerce :: forall a b . a -> b | 151 | unsafeCoerce :: forall a b . a -> b |
163 | 152 | ||
164 | isKeyC _ _ [] = 'Empty "" | 153 | isKeyC _ _ [] = 'Empty "" |
165 | isKeyC s t ((s', t'): ss) = if s === s' then t ~ t' else isKeyC s t ss | 154 | isKeyC 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 |
168 | project :: forall a (xs :: [(String, Type)]) . forall (s :: String) -> 'isKeyC s a xs => RecordC xs -> a | 157 | project :: forall a (xs :: [(String, Type)]) . forall (s :: String) -> 'isKeyC s a xs => RecordC xs -> a |
169 | project @a @((s', a'): xs) s @_ (RecordCons ts) | s === s' = fst (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts) | 158 | project @a @((s', a'): xs) s @_ (RecordCons ts) | s == s' = fst (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts) |
170 | project @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))) | 159 | project @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 |
251 | infixl 7 *, /, % | 240 | infixl 7 *, /, % |
252 | infixl 6 +, - | 241 | infixl 6 +, - |
253 | infix 4 ==, /=, <, <=, >=, > | 242 | infix 4 /=, <, <=, >=, > |
254 | 243 | ||
255 | infixr 3 && | 244 | infixr 3 && |
256 | infixr 2 || | 245 | infixr 2 || |
@@ -272,7 +261,7 @@ a % b = PrimMod a b | |||
272 | neg a = PrimNeg a | 261 | neg a = PrimNeg a |
273 | 262 | ||
274 | -- comparison | 263 | -- comparison |
275 | a == b = PrimEqual a b | 264 | --a == b = PrimEqual a b |
276 | a /= b = PrimNotEqual a b | 265 | a /= b = PrimNotEqual a b |
277 | a < b = PrimLessThan a b | 266 | a < b = PrimLessThan a b |
278 | a <= b = PrimLessThanEqual a b | 267 | a <= b = PrimLessThanEqual a b |