1{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
3module Idris.Primitives(elabPrims) where
15data Prim = Prim { p_name :: Name,
18 p_def :: [Value] -> Maybe Value,
19 p_lexp :: (Int, PrimFn),
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)))
31partial = Partial NotCovering
35 [Prim (UN "prim__addInt") (ty [IType, IType] IType) 2 (iBin (+))
37 Prim (UN "prim__subInt") (ty [IType, IType] IType) 2 (iBin (-))
39 Prim (UN "prim__mulInt") (ty [IType, IType] IType) 2 (iBin (*))
41 Prim (UN "prim__divInt") (ty [IType, IType] IType) 2 (iBin div)
43 Prim (UN "prim__modInt") (ty [IType, IType] IType) 2 (iBin mod)
45 Prim (UN "prim__andInt") (ty [IType, IType] IType) 2 (iBin (.&.))
47 Prim (UN "prim__orInt") (ty [IType, IType] IType) 2 (iBin (.|.))
49 Prim (UN "prim__xorInt") (ty [IType, IType] IType) 2 (iBin xor)
51 Prim (UN "prim__shLInt") (ty [IType, IType] IType) 2 (iBin shiftL)
53 Prim (UN "prim__shRInt") (ty [IType, IType] IType) 2 (iBin shiftR)
55 Prim (UN "prim__complInt") (ty [IType] IType) 1 (iUn complement)
57 Prim (UN "prim__eqInt") (ty [IType, IType] IType) 2 (biBin (==))
59 Prim (UN "prim__ltInt") (ty [IType, IType] IType) 2 (biBin (<))
61 Prim (UN "prim__lteInt") (ty [IType, IType] IType) 2 (biBin (<=))
63 Prim (UN "prim__gtInt") (ty [IType, IType] IType) 2 (biBin (>))
65 Prim (UN "prim__gteInt") (ty [IType, IType] IType) 2 (biBin (>=))
67 Prim (UN "prim__eqChar") (ty [ChType, ChType] IType) 2 (bcBin (==))
69 Prim (UN "prim__ltChar") (ty [ChType, ChType] IType) 2 (bcBin (<))
71 Prim (UN "prim__lteChar") (ty [ChType, ChType] IType) 2 (bcBin (<=))
73 Prim (UN "prim__gtChar") (ty [ChType, ChType] IType) 2 (bcBin (>))
75 Prim (UN "prim__gteChar") (ty [ChType, ChType] IType) 2 (bcBin (>=))
78 Prim (UN "prim__addW8") (ty [W8Type, W8Type] W8Type) 2 (w8Bin (+))
80 Prim (UN "prim__subW8") (ty [W8Type, W8Type] W8Type) 2 (w8Bin (-))
82 Prim (UN "prim__mulW8") (ty [W8Type, W8Type] W8Type) 2 (w8Bin (*))
85 Prim (UN "prim__addW16") (ty [W16Type, W16Type] W16Type) 2 (w16Bin (+))
87 Prim (UN "prim__subW16") (ty [W16Type, W16Type] W16Type) 2 (w16Bin (-))
89 Prim (UN "prim__mulW16") (ty [W16Type, W16Type] W16Type) 2 (w16Bin (*))
92 Prim (UN "prim__intToWord8") (ty [IType] W8Type) 1 intToWord8
94 Prim (UN "prim__intToWord16") (ty [IType] W16Type) 1 intToWord16
97 Prim (UN "prim__addBigInt") (ty [BIType, BIType] BIType) 2 (bBin (+))
99 Prim (UN "prim__subBigInt") (ty [BIType, BIType] BIType) 2 (bBin (-))
101 Prim (UN "prim__mulBigInt") (ty [BIType, BIType] BIType) 2 (bBin (*))
103 Prim (UN "prim__divBigInt") (ty [BIType, BIType] BIType) 2 (bBin div)
105 Prim (UN "prim__modBigInt") (ty [BIType, BIType] BIType) 2 (bBin mod)
107 Prim (UN "prim__eqBigInt") (ty [BIType, BIType] IType) 2 (bbBin (==))
109 Prim (UN "prim__ltBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<))
111 Prim (UN "prim__lteBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<=))
113 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>))
115 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>=))
117 Prim (UN "prim__addFloat") (ty [FlType, FlType] FlType) 2 (fBin (+))
119 Prim (UN "prim__subFloat") (ty [FlType, FlType] FlType) 2 (fBin (-))
121 Prim (UN "prim__mulFloat") (ty [FlType, FlType] FlType) 2 (fBin (*))
123 Prim (UN "prim__divFloat") (ty [FlType, FlType] FlType) 2 (fBin (/))
125 Prim (UN "prim__eqFloat") (ty [FlType, FlType] IType) 2 (bfBin (==))
127 Prim (UN "prim__ltFloat") (ty [FlType, FlType] IType) 2 (bfBin (<))
129 Prim (UN "prim__lteFloat") (ty [FlType, FlType] IType) 2 (bfBin (<=))
131 Prim (UN "prim__gtFloat") (ty [FlType, FlType] IType) 2 (bfBin (>))
133 Prim (UN "prim__gteFloat") (ty [FlType, FlType] IType) 2 (bfBin (>=))
135 Prim (UN "prim__concat") (ty [StrType, StrType] StrType) 2 (sBin (++))
136 (2, LStrConcat) total,
137 Prim (UN "prim__eqString") (ty [StrType, StrType] IType) 2 (bsBin (==))
139 Prim (UN "prim__ltString") (ty [StrType, StrType] IType) 2 (bsBin (<))
142 Prim (UN "prim__strToInt") (ty [StrType] IType) 1 (c_strToInt)
144 Prim (UN "prim__intToStr") (ty [IType] StrType) 1 (c_intToStr)
146 Prim (UN "prim__charToInt") (ty [ChType] IType) 1 (c_charToInt)
148 Prim (UN "prim__intToChar") (ty [IType] ChType) 1 (c_intToChar)
150 Prim (UN "prim__intToBigInt") (ty [IType] BIType) 1 (c_intToBigInt)
152 Prim (UN "prim__bigIntToInt") (ty [BIType] IType) 1 (c_bigIntToInt)
154 Prim (UN "prim__strToBigInt") (ty [StrType] BIType) 1 (c_strToBigInt)
156 Prim (UN "prim__bigIntToStr") (ty [BIType] StrType) 1 (c_bigIntToStr)
158 Prim (UN "prim__strToFloat") (ty [StrType] FlType) 1 (c_strToFloat)
159 (1, LStrFloat) total,
160 Prim (UN "prim__floatToStr") (ty [FlType] StrType) 1 (c_floatToStr)
161 (1, LFloatStr) total,
162 Prim (UN "prim__intToFloat") (ty [IType] FlType) 1 (c_intToFloat)
163 (1, LIntFloat) total,
164 Prim (UN "prim__floatToInt") (ty [FlType] IType) 1 (c_floatToInt)
165 (1, LFloatInt) total,
167 Prim (UN "prim__floatExp") (ty [FlType] FlType) 1 (p_floatExp)
169 Prim (UN "prim__floatLog") (ty [FlType] FlType) 1 (p_floatLog)
171 Prim (UN "prim__floatSin") (ty [FlType] FlType) 1 (p_floatSin)
173 Prim (UN "prim__floatCos") (ty [FlType] FlType) 1 (p_floatCos)
175 Prim (UN "prim__floatTan") (ty [FlType] FlType) 1 (p_floatTan)
177 Prim (UN "prim__floatASin") (ty [FlType] FlType) 1 (p_floatASin)
179 Prim (UN "prim__floatACos") (ty [FlType] FlType) 1 (p_floatACos)
181 Prim (UN "prim__floatATan") (ty [FlType] FlType) 1 (p_floatATan)
183 Prim (UN "prim__floatSqrt") (ty [FlType] FlType) 1 (p_floatSqrt)
185 Prim (UN "prim__floatFloor") (ty [FlType] FlType) 1 (p_floatFloor)
187 Prim (UN "prim__floatCeil") (ty [FlType] FlType) 1 (p_floatCeil)
190 Prim (UN "prim__strHead") (ty [StrType] ChType) 1 (p_strHead)
191 (1, LStrHead) partial,
192 Prim (UN "prim__strTail") (ty [StrType] StrType) 1 (p_strTail)
193 (1, LStrTail) partial,
194 Prim (UN "prim__strCons") (ty [ChType, StrType] StrType) 2 (p_strCons)
196 Prim (UN "prim__strIndex") (ty [StrType, IType] ChType) 2 (p_strIndex)
197 (2, LStrIndex) partial,
198 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
200 Prim (UN "prim__readString") (ty [PtrType] StrType) 1 (p_cantreduce)
201 (1, LReadStr) partial,
202 Prim (UN "prim__vm") (ty [] PtrType) 0 (p_cantreduce)
205 Prim (UN "prim__stdin") (ty [] PtrType) 0 (p_cantreduce)
207 Prim (UN "prim__believe_me") believeTy 3 (p_believeMe)
208 (3, LNoOp) partial -- ahem
211p_believeMe [_,_,x] = Just x
212p_believeMe _ = Nothing
214iUn op [VConstant (I x)] = Just $ VConstant (I (op x))
217iBin op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
220bBin op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (BI (op x y))
223bBini op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (I (op x y))
226biBin op = iBin (\x y -> if (op x y) then 1 else 0)
227bbBin op = bBini (\x y -> if (op x y) then 1 else 0)
229fBin op [VConstant (Fl x), VConstant (Fl y)] = Just $ VConstant (Fl (op x y))
232bfBin op [VConstant (Fl x), VConstant (Fl y)] = let i = (if op x y then 1 else 0) in
233 Just $ VConstant (I i)
236bcBin op [VConstant (Ch x), VConstant (Ch y)] = let i = (if op x y then 1 else 0) in
237 Just $ VConstant (I i)
240bsBin op [VConstant (Str x), VConstant (Str y)]
241 = let i = (if op x y then 1 else 0) in
242 Just $ VConstant (I i)
245sBin op [VConstant (Str x), VConstant (Str y)] = Just $ VConstant (Str (op x y))
248w8Bin op [VConstant (W8 x), VConstant (W8 y)] = Just $ VConstant (W8 (op x y))
251w16Bin op [VConstant (W16 x), VConstant (W16 y)] = Just $ VConstant (W16 (op x y))
254-- w32Bin op [VConstant (W32 x), VConstant (W32 y)] = Just $ VConstant (W32 (op x y))
255-- w32Bin _ _ = Nothing
257intToWord8 [VConstant (I x)] = Just $ VConstant (W8 $ fromIntegral x)
258intToWord8 _ = Nothing
259intToWord16 [VConstant (I x)] = Just $ VConstant (W16 $ fromIntegral x)
260intToWord16 _ = Nothing
262c_intToStr [VConstant (I x)] = Just $ VConstant (Str (show x))
263c_intToStr _ = Nothing
264c_strToInt [VConstant (Str x)] = case reads x of
265 [(n,"")] -> Just $ VConstant (I n)
266 _ -> Just $ VConstant (I 0)
267c_strToInt _ = Nothing
269c_intToChar [VConstant (I x)] = Just $ VConstant (Ch (toEnum x))
270c_intToChar _ = Nothing
271c_charToInt [VConstant (Ch x)] = Just $ VConstant (I (fromEnum x))
272c_charToInt _ = Nothing
274c_intToBigInt [VConstant (I x)] = Just $ VConstant (BI (fromIntegral x))
275c_intToBigInt _ = Nothing
276c_bigIntToInt [VConstant (BI x)] = Just $ VConstant (I (fromInteger x))
277c_bigIntToInt _ = Nothing
279c_bigIntToStr [VConstant (BI x)] = Just $ VConstant (Str (show x))
280c_bigIntToStr _ = Nothing
281c_strToBigInt [VConstant (Str x)] = case reads x of
282 [(n,"")] -> Just $ VConstant (BI n)
283 _ -> Just $ VConstant (BI 0)
284c_strToBigInt _ = Nothing
286c_floatToStr [VConstant (Fl x)] = Just $ VConstant (Str (show x))
287c_floatToStr _ = Nothing
288c_strToFloat [VConstant (Str x)] = case reads x of
289 [(n,"")] -> Just $ VConstant (Fl n)
290 _ -> Just $ VConstant (Fl 0)
291c_strToFloat _ = Nothing
293c_floatToInt [VConstant (Fl x)] = Just $ VConstant (I (truncate x))
294c_floatToInt _ = Nothing
296c_intToFloat [VConstant (I x)] = Just $ VConstant (Fl (fromIntegral x))
297c_intToFloat _ = Nothing
299p_fPrim f [VConstant (Fl x)] = Just $ VConstant (Fl (f x))
302p_floatExp = p_fPrim exp
303p_floatLog = p_fPrim log
304p_floatSin = p_fPrim sin
305p_floatCos = p_fPrim cos
306p_floatTan = p_fPrim tan
307p_floatASin = p_fPrim asin
308p_floatACos = p_fPrim acos
309p_floatATan = p_fPrim atan
310p_floatSqrt = p_fPrim sqrt
311p_floatFloor = p_fPrim (fromInteger . floor)
312p_floatCeil = p_fPrim (fromInteger . ceiling)
314p_strHead [VConstant (Str (x:xs))] = Just $ VConstant (Ch x)
316p_strTail [VConstant (Str (x:xs))] = Just $ VConstant (Str xs)
318p_strIndex [VConstant (Str xs), VConstant (I i)]
319 | i < length xs = Just $ VConstant (Ch (xs!!i))
320p_strIndex _ = Nothing
321p_strCons [VConstant (Ch x), VConstant (Str xs)] = Just $ VConstant (Str (x:xs))
323p_strRev [VConstant (Str xs)] = Just $ VConstant (Str (reverse xs))
326p_cantreduce _ = Nothing
328elabPrim :: Prim -> Idris ()
329elabPrim (Prim n ty i def sc tot)
330 = do updateContext (addOperator n ty i def)
333 putIState i { idris_scprims = (n, sc) : idris_scprims i }
336elabPrims = do mapM_ (elabDecl EAll toplevel)
337 (map (PData "" defaultSyntax (FC "builtin" 0) False)
338 [inferDecl, unitDecl, falseDecl, pairDecl, eqDecl])
339 mapM_ elabPrim primitives
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list