on 02ef050 help sign in
Idris-dev/src/Idris/Primitives.hs
320 lines 14 kB history raw
1{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
3module Idris.Primitives(elabPrims) where
5import Idris.ElabDecls
6import Idris.ElabTerm
7import Idris.AbsSyntax
9import Core.TT
10import Core.Evaluate
12import Epic.Epic hiding (Term, Type, Name, fn)
13import qualified Epic.Epic as E
15data Prim = Prim { p_name :: Name,
16 p_type :: Type,
17 p_arity :: Int,
18 p_def :: [Value] -> Maybe Value,
19 p_epic :: ([E.Name], E.Term),
20 p_total :: Totality
21 }
23ty [] x = Constant x
24ty (t:ts) x = Bind (MN 0 "T") (Pi (Constant t)) (ty ts x)
26believeTy = Bind (UN "a") (Pi (Set (UVar (-2))))
27 (Bind (UN "b") (Pi (Set (UVar (-2))))
28 (Bind (UN "x") (Pi (V 1)) (V 1)))
30type ETm = E.Term
31type EOp = E.Op
33fun = E.fn
34ref = E.ref
36eOp :: EOp -> ([E.Name], ETm)
37eOp op = ([E.name "x", E.name "y"], E.op_ op (E.fn "x") (E.fn "y"))
39eOpFn :: E.Type -> E.Type -> String -> ([E.Name], ETm)
40eOpFn ty rty op = ([E.name "x", E.name "y"],
41 foreign_ rty op [(E.fn "x", ty), (E.fn "y", ty)])
43strToInt x = foreign_ tyInt "strToInt" [(x, tyString)]
44intToStr x = foreign_ tyString "intToStr" [(x, tyInt)]
45charToInt x = x
46intToChar x = x
47intToBigInt x = foreign_ tyBigInt "intToBigInt" [(x, tyInt)]
48strToBigInt x = foreign_ tyBigInt "strToBig" [(x, tyString)]
49bigIntToStr x = foreign_ tyString "bigToStr" [(x, tyBigInt)]
50strToFloat x = foreign_ tyFloat "strToFloat" [(x, tyString)]
51floatToStr x = foreign_ tyString "floatToStr" [(x, tyFloat)]
52intToFloat x = foreign_ tyFloat "intToFloat" [(x, tyInt)]
53floatToInt x = foreign_ tyInt "floatToInt" [(x, tyFloat)]
55floatExp x = foreign_ tyFloat "exp" [(x, tyFloat)]
56floatLog x = foreign_ tyFloat "log" [(x, tyFloat)]
57floatSin x = foreign_ tyFloat "sin" [(x, tyFloat)]
58floatCos x = foreign_ tyFloat "cos" [(x, tyFloat)]
59floatTan x = foreign_ tyFloat "tan" [(x, tyFloat)]
60floatASin x = foreign_ tyFloat "asin" [(x, tyFloat)]
61floatACos x = foreign_ tyFloat "acos" [(x, tyFloat)]
62floatATan x = foreign_ tyFloat "atan" [(x, tyFloat)]
63floatFloor x = foreign_ tyFloat "floor" [(x, tyFloat)]
64floatCeil x = foreign_ tyFloat "ceil" [(x, tyFloat)]
65floatSqrt x = foreign_ tyFloat "sqrt" [(x, tyFloat)]
67strIndex x i = foreign_ tyChar "strIndex" [(x, tyString), (i, tyInt)]
68strHead x = foreign_ tyChar "strHead" [(x, tyString)]
69strTail x = foreign_ tyString "strTail" [(x, tyString)]
70strCons x xs = foreign_ tyString "strCons" [(x, tyChar), (xs, tyString)]
71strRev x = foreign_ tyString "strrev" [(x, tyString)]
72strEq x y = foreign_ tyInt "streq" [(x, tyString), (y, tyString)]
73strLt x y = foreign_ tyInt "strlt" [(x, tyString), (y, tyString)]
75total = Total []
76partial = Partial NotCovering
78primitives =
79 -- operators
80 [Prim (UN "prim__addInt") (ty [IType, IType] IType) 2 (iBin (+))
81 (eOp E.plus_) total,
82 Prim (UN "prim__subInt") (ty [IType, IType] IType) 2 (iBin (-))
83 (eOp E.minus_) total,
84 Prim (UN "prim__mulInt") (ty [IType, IType] IType) 2 (iBin (*))
85 (eOp E.times_) total,
86 Prim (UN "prim__divInt") (ty [IType, IType] IType) 2 (iBin (div))
87 (eOp E.divide_) partial,
88 Prim (UN "prim__eqInt") (ty [IType, IType] IType) 2 (biBin (==))
89 (eOp E.eq_) total,
90 Prim (UN "prim__ltInt") (ty [IType, IType] IType) 2 (biBin (<))
91 (eOp E.lt_) total,
92 Prim (UN "prim__lteInt") (ty [IType, IType] IType) 2 (biBin (<=))
93 (eOp E.lte_) total,
94 Prim (UN "prim__gtInt") (ty [IType, IType] IType) 2 (biBin (>))
95 (eOp E.gt_) total,
96 Prim (UN "prim__gteInt") (ty [IType, IType] IType) 2 (biBin (>=))
97 (eOp E.gte_) total,
98 Prim (UN "prim__eqChar") (ty [ChType, ChType] IType) 2 (bcBin (==))
99 (eOp E.eq_) total,
100 Prim (UN "prim__ltChar") (ty [ChType, ChType] IType) 2 (bcBin (<))
101 (eOp E.lt_) total,
102 Prim (UN "prim__lteChar") (ty [ChType, ChType] IType) 2 (bcBin (<=))
103 (eOp E.lte_) total,
104 Prim (UN "prim__gtChar") (ty [ChType, ChType] IType) 2 (bcBin (>))
105 (eOp E.gt_) total,
106 Prim (UN "prim__gteChar") (ty [ChType, ChType] IType) 2 (bcBin (>=))
107 (eOp E.gte_) total,
108 Prim (UN "prim__addBigInt") (ty [BIType, BIType] BIType) 2 (bBin (+))
109 (eOpFn tyBigInt tyBigInt "addBig") total,
110 Prim (UN "prim__subBigInt") (ty [BIType, BIType] BIType) 2 (bBin (-))
111 (eOpFn tyBigInt tyBigInt "subBig") total,
112 Prim (UN "prim__mulBigInt") (ty [BIType, BIType] BIType) 2 (bBin (*))
113 (eOpFn tyBigInt tyBigInt "mulBig") total,
114 Prim (UN "prim__divBigInt") (ty [BIType, BIType] BIType) 2 (bBin (div))
115 (eOpFn tyBigInt tyBigInt "divBig") partial,
116 Prim (UN "prim__eqBigInt") (ty [BIType, BIType] IType) 2 (bbBin (==))
117 (eOpFn tyBigInt tyInt "eqBig") total,
118 Prim (UN "prim__ltBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<))
119 (eOpFn tyBigInt tyInt "ltBig") total,
120 Prim (UN "prim__lteBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<=))
121 (eOpFn tyBigInt tyInt "leBig") total,
122 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>))
123 (eOpFn tyBigInt tyInt "gtBig") total,
124 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>=))
125 (eOpFn tyBigInt tyInt "geBig") total,
126 Prim (UN "prim__addFloat") (ty [FlType, FlType] FlType) 2 (fBin (+))
127 (eOp E.plusF_) total,
128 Prim (UN "prim__subFloat") (ty [FlType, FlType] FlType) 2 (fBin (-))
129 (eOp E.minusF_) total,
130 Prim (UN "prim__mulFloat") (ty [FlType, FlType] FlType) 2 (fBin (*))
131 (eOp E.timesF_) total,
132 Prim (UN "prim__divFloat") (ty [FlType, FlType] FlType) 2 (fBin (/))
133 (eOp E.divideF_) total,
134 Prim (UN "prim__eqFloat") (ty [FlType, FlType] IType) 2 (bfBin (==))
135 (eOp E.eqF_) total,
136 Prim (UN "prim__ltFloat") (ty [FlType, FlType] IType) 2 (bfBin (<))
137 (eOp E.ltF_) total,
138 Prim (UN "prim__lteFloat") (ty [FlType, FlType] IType) 2 (bfBin (<=))
139 (eOp E.lteF_) total,
140 Prim (UN "prim__gtFloat") (ty [FlType, FlType] IType) 2 (bfBin (>))
141 (eOp E.gtF_) total,
142 Prim (UN "prim__gteFloat") (ty [FlType, FlType] IType) 2 (bfBin (>=))
143 (eOp E.gteF_) total,
144 Prim (UN "prim__concat") (ty [StrType, StrType] StrType) 2 (sBin (++))
145 ([E.name "x", E.name "y"], (fun "append") @@ fun "x" @@ fun "y") total,
146 Prim (UN "prim__eqString") (ty [StrType, StrType] IType) 2 (bsBin (==))
147 ([E.name "x", E.name "y"], strEq (fun "x") (fun "y")) total,
148 Prim (UN "prim__ltString") (ty [StrType, StrType] IType) 2 (bsBin (<))
149 ([E.name "x", E.name "y"], strLt (fun "x") (fun "y")) total,
150 -- Conversions
151 Prim (UN "prim__strToInt") (ty [StrType] IType) 1 (c_strToInt)
152 ([E.name "x"], strToInt (fun "x")) total,
153 Prim (UN "prim__intToStr") (ty [IType] StrType) 1 (c_intToStr)
154 ([E.name "x"], intToStr (fun "x")) total,
155 Prim (UN "prim__charToInt") (ty [ChType] IType) 1 (c_charToInt)
156 ([E.name "x"], charToInt (fun "x")) total,
157 Prim (UN "prim__intToChar") (ty [IType] ChType) 1 (c_intToChar)
158 ([E.name "x"], intToChar (fun "x")) total,
159 Prim (UN "prim__intToBigInt") (ty [IType] BIType) 1 (c_intToBigInt)
160 ([E.name "x"], intToBigInt (fun "x")) total,
161 Prim (UN "prim__strToBigInt") (ty [StrType] BIType) 1 (c_strToBigInt)
162 ([E.name "x"], strToBigInt (fun "x")) total,
163 Prim (UN "prim__bigIntToStr") (ty [BIType] StrType) 1 (c_bigIntToStr)
164 ([E.name "x"], bigIntToStr (fun "x")) total,
165 Prim (UN "prim__strToFloat") (ty [StrType] FlType) 1 (c_strToFloat)
166 ([E.name "x"], strToFloat (fun "x")) total,
167 Prim (UN "prim__floatToStr") (ty [FlType] StrType) 1 (c_floatToStr)
168 ([E.name "x"], floatToStr (fun "x")) total,
169 Prim (UN "prim__intToFloat") (ty [IType] FlType) 1 (c_intToFloat)
170 ([E.name "x"], intToFloat (fun "x")) total,
171 Prim (UN "prim__floatToInt") (ty [FlType] IType) 1 (c_floatToInt)
172 ([E.name "x"], floatToInt (fun "x")) total,
174 Prim (UN "prim__floatExp") (ty [FlType] FlType) 1 (p_floatExp)
175 ([E.name "x"], floatExp (fun "x")) total,
176 Prim (UN "prim__floatLog") (ty [FlType] FlType) 1 (p_floatLog)
177 ([E.name "x"], floatLog (fun "x")) total,
178 Prim (UN "prim__floatSin") (ty [FlType] FlType) 1 (p_floatSin)
179 ([E.name "x"], floatSin (fun "x")) total,
180 Prim (UN "prim__floatCos") (ty [FlType] FlType) 1 (p_floatCos)
181 ([E.name "x"], floatCos (fun "x")) total,
182 Prim (UN "prim__floatTan") (ty [FlType] FlType) 1 (p_floatTan)
183 ([E.name "x"], floatTan (fun "x")) total,
184 Prim (UN "prim__floatASin") (ty [FlType] FlType) 1 (p_floatASin)
185 ([E.name "x"], floatASin (fun "x")) total,
186 Prim (UN "prim__floatACos") (ty [FlType] FlType) 1 (p_floatACos)
187 ([E.name "x"], floatACos (fun "x")) total,
188 Prim (UN "prim__floatATan") (ty [FlType] FlType) 1 (p_floatATan)
189 ([E.name "x"], floatATan (fun "x")) total,
190 Prim (UN "prim__floatSqrt") (ty [FlType] FlType) 1 (p_floatSqrt)
191 ([E.name "x"], floatSqrt (fun "x")) total,
192 Prim (UN "prim__floatFloor") (ty [FlType] FlType) 1 (p_floatFloor)
193 ([E.name "x"], floatFloor (fun "x")) total,
194 Prim (UN "prim__floatCeil") (ty [FlType] FlType) 1 (p_floatCeil)
195 ([E.name "x"], floatCeil (fun "x")) total,
197 Prim (UN "prim__strHead") (ty [StrType] ChType) 1 (p_strHead)
198 ([E.name "x"], strHead (fun "x")) partial,
199 Prim (UN "prim__strTail") (ty [StrType] StrType) 1 (p_strTail)
200 ([E.name "x"], strTail (fun "x")) partial,
201 Prim (UN "prim__strCons") (ty [ChType, StrType] StrType) 2 (p_strCons)
202 ([E.name "x", E.name "xs"], strCons (fun "x") (fun "xs")) total,
203 Prim (UN "prim__strIndex") (ty [StrType, IType] ChType) 2 (p_strIndex)
204 ([E.name "x", E.name "i"], strIndex (fun "x") (fun "i")) partial,
205 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
206 ([E.name "x"], strRev (fun "x")) total,
208 Prim (UN "prim__believe_me") believeTy 3 (p_believeMe)
209 ([E.name "a", E.name "b", E.name "x"], fun "x") total -- ahem
210 ]
212p_believeMe [_,_,x] = Just x
213p_believeMe _ = Nothing
215iBin op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
216iBin _ _ = Nothing
218bBin op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (BI (op x y))
219bBin _ _ = Nothing
221bBini op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (I (op x y))
222bBini _ _ = Nothing
224biBin op = iBin (\x y -> if (op x y) then 1 else 0)
225bbBin op = bBini (\x y -> if (op x y) then 1 else 0)
227fBin op [VConstant (Fl x), VConstant (Fl y)] = Just $ VConstant (Fl (op x y))
228fBin _ _ = Nothing
230bfBin op [VConstant (Fl x), VConstant (Fl y)] = let i = (if op x y then 1 else 0) in
231 Just $ VConstant (I i)
232bfBin _ _ = Nothing
234bcBin op [VConstant (Ch x), VConstant (Ch y)] = let i = (if op x y then 1 else 0) in
235 Just $ VConstant (I i)
236bcBin _ _ = Nothing
238bsBin op [VConstant (Str x), VConstant (Str y)]
239 = let i = (if op x y then 1 else 0) in
240 Just $ VConstant (I i)
241bsBin _ _ = Nothing
243sBin op [VConstant (Str x), VConstant (Str y)] = Just $ VConstant (Str (op x y))
244sBin _ _ = Nothing
246c_intToStr [VConstant (I x)] = Just $ VConstant (Str (show x))
247c_intToStr _ = Nothing
248c_strToInt [VConstant (Str x)] = case reads x of
249 [(n,"")] -> Just $ VConstant (I n)
250 _ -> Just $ VConstant (I 0)
251c_strToInt _ = Nothing
253c_intToChar [VConstant (I x)] = Just $ VConstant (Ch (toEnum x))
254c_intToChar _ = Nothing
255c_charToInt [VConstant (Ch x)] = Just $ VConstant (I (fromEnum x))
256c_charToInt _ = Nothing
258c_intToBigInt [VConstant (I x)] = Just $ VConstant (BI (fromIntegral x))
259c_intToBigInt _ = Nothing
261c_bigIntToStr [VConstant (BI x)] = Just $ VConstant (Str (show x))
262c_bigIntToStr _ = Nothing
263c_strToBigInt [VConstant (Str x)] = case reads x of
264 [(n,"")] -> Just $ VConstant (BI n)
265 _ -> Just $ VConstant (BI 0)
266c_strToBigInt _ = Nothing
268c_floatToStr [VConstant (Fl x)] = Just $ VConstant (Str (show x))
269c_floatToStr _ = Nothing
270c_strToFloat [VConstant (Str x)] = case reads x of
271 [(n,"")] -> Just $ VConstant (Fl n)
272 _ -> Just $ VConstant (Fl 0)
273c_strToFloat _ = Nothing
275c_floatToInt [VConstant (Fl x)] = Just $ VConstant (I (truncate x))
276c_floatToInt _ = Nothing
278c_intToFloat [VConstant (I x)] = Just $ VConstant (Fl (fromIntegral x))
279c_intToFloat _ = Nothing
281p_fPrim f [VConstant (Fl x)] = Just $ VConstant (Fl (f x))
282p_fPrim f _ = Nothing
284p_floatExp = p_fPrim exp
285p_floatLog = p_fPrim log
286p_floatSin = p_fPrim sin
287p_floatCos = p_fPrim cos
288p_floatTan = p_fPrim tan
289p_floatASin = p_fPrim asin
290p_floatACos = p_fPrim acos
291p_floatATan = p_fPrim atan
292p_floatSqrt = p_fPrim sqrt
293p_floatFloor = p_fPrim (fromInteger . floor)
294p_floatCeil = p_fPrim (fromInteger . ceiling)
296p_strHead [VConstant (Str (x:xs))] = Just $ VConstant (Ch x)
297p_strHead _ = Nothing
298p_strTail [VConstant (Str (x:xs))] = Just $ VConstant (Str xs)
299p_strTail _ = Nothing
300p_strIndex [VConstant (Str xs), VConstant (I i)]
301 | i < length xs = Just $ VConstant (Ch (xs!!i))
302p_strIndex _ = Nothing
303p_strCons [VConstant (Ch x), VConstant (Str xs)] = Just $ VConstant (Str (x:xs))
304p_strCons _ = Nothing
305p_strRev [VConstant (Str xs)] = Just $ VConstant (Str (reverse xs))
306p_strRev _ = Nothing
308elabPrim :: Prim -> Idris ()
309elabPrim (Prim n ty i def epic tot)
310 = do updateContext (addOperator n ty i def)
311 setTotality n tot
312 i <- getIState
313 putIState i { idris_prims = (n, epic) : idris_prims i }
315elabPrims :: Idris ()
316elabPrims = do mapM_ (elabDecl toplevel)
317 (map (PData defaultSyntax (FC "builtin" 0))
318 [inferDecl, unitDecl, falseDecl, pairDecl, eqDecl])
319 mapM_ elabPrim primitives