1{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
3module Idris.Primitives(elabPrims) where
14data Prim = Prim { p_name :: Name,
17 p_def :: [Value] -> Maybe Value,
18 p_lexp :: (Int, PrimFn),
23ty (t:ts) x = Bind (MN 0 "T") (Pi (Constant t)) (ty ts x)
25believeTy = Bind (UN "a") (Pi (Set (UVar (-2))))
26 (Bind (UN "b") (Pi (Set (UVar (-2))))
27 (Bind (UN "x") (Pi (V 1)) (V 1)))
30partial = Partial NotCovering
34 [Prim (UN "prim__addInt") (ty [IType, IType] IType) 2 (iBin (+))
36 Prim (UN "prim__subInt") (ty [IType, IType] IType) 2 (iBin (-))
38 Prim (UN "prim__mulInt") (ty [IType, IType] IType) 2 (iBin (*))
40 Prim (UN "prim__divInt") (ty [IType, IType] IType) 2 (iBin (div))
42 Prim (UN "prim__modInt") (ty [IType, IType] IType) 2 (iBin (mod))
44 Prim (UN "prim__eqInt") (ty [IType, IType] IType) 2 (biBin (==))
46 Prim (UN "prim__ltInt") (ty [IType, IType] IType) 2 (biBin (<))
48 Prim (UN "prim__lteInt") (ty [IType, IType] IType) 2 (biBin (<=))
50 Prim (UN "prim__gtInt") (ty [IType, IType] IType) 2 (biBin (>))
52 Prim (UN "prim__gteInt") (ty [IType, IType] IType) 2 (biBin (>=))
54 Prim (UN "prim__eqChar") (ty [ChType, ChType] IType) 2 (bcBin (==))
56 Prim (UN "prim__ltChar") (ty [ChType, ChType] IType) 2 (bcBin (<))
58 Prim (UN "prim__lteChar") (ty [ChType, ChType] IType) 2 (bcBin (<=))
60 Prim (UN "prim__gtChar") (ty [ChType, ChType] IType) 2 (bcBin (>))
62 Prim (UN "prim__gteChar") (ty [ChType, ChType] IType) 2 (bcBin (>=))
64 Prim (UN "prim__addBigInt") (ty [BIType, BIType] BIType) 2 (bBin (+))
67 Prim (UN "prim__subBigInt") (ty [BIType, BIType] BIType) 2 (bBin (-))
69 Prim (UN "prim__mulBigInt") (ty [BIType, BIType] BIType) 2 (bBin (*))
71 Prim (UN "prim__divBigInt") (ty [BIType, BIType] BIType) 2 (bBin (div))
73 Prim (UN "prim__modBigInt") (ty [BIType, BIType] BIType) 2 (bBin (mod))
75 Prim (UN "prim__eqBigInt") (ty [BIType, BIType] IType) 2 (bbBin (==))
77 Prim (UN "prim__ltBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<))
79 Prim (UN "prim__lteBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<=))
81 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>))
83 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>=))
85 Prim (UN "prim__addFloat") (ty [FlType, FlType] FlType) 2 (fBin (+))
87 Prim (UN "prim__subFloat") (ty [FlType, FlType] FlType) 2 (fBin (-))
89 Prim (UN "prim__mulFloat") (ty [FlType, FlType] FlType) 2 (fBin (*))
91 Prim (UN "prim__divFloat") (ty [FlType, FlType] FlType) 2 (fBin (/))
93 Prim (UN "prim__eqFloat") (ty [FlType, FlType] IType) 2 (bfBin (==))
95 Prim (UN "prim__ltFloat") (ty [FlType, FlType] IType) 2 (bfBin (<))
97 Prim (UN "prim__lteFloat") (ty [FlType, FlType] IType) 2 (bfBin (<=))
99 Prim (UN "prim__gtFloat") (ty [FlType, FlType] IType) 2 (bfBin (>))
101 Prim (UN "prim__gteFloat") (ty [FlType, FlType] IType) 2 (bfBin (>=))
103 Prim (UN "prim__concat") (ty [StrType, StrType] StrType) 2 (sBin (++))
104 (2, LStrConcat) total,
105 Prim (UN "prim__eqString") (ty [StrType, StrType] IType) 2 (bsBin (==))
107 Prim (UN "prim__ltString") (ty [StrType, StrType] IType) 2 (bsBin (<))
110 Prim (UN "prim__strToInt") (ty [StrType] IType) 1 (c_strToInt)
112 Prim (UN "prim__intToStr") (ty [IType] StrType) 1 (c_intToStr)
114 Prim (UN "prim__charToInt") (ty [ChType] IType) 1 (c_charToInt)
116 Prim (UN "prim__intToChar") (ty [IType] ChType) 1 (c_intToChar)
118 Prim (UN "prim__intToBigInt") (ty [IType] BIType) 1 (c_intToBigInt)
120 Prim (UN "prim__bigIntToInt") (ty [BIType] IType) 1 (c_bigIntToInt)
122 Prim (UN "prim__strToBigInt") (ty [StrType] BIType) 1 (c_strToBigInt)
124 Prim (UN "prim__bigIntToStr") (ty [BIType] StrType) 1 (c_bigIntToStr)
126 Prim (UN "prim__strToFloat") (ty [StrType] FlType) 1 (c_strToFloat)
127 (1, LStrFloat) total,
128 Prim (UN "prim__floatToStr") (ty [FlType] StrType) 1 (c_floatToStr)
129 (1, LFloatStr) total,
130 Prim (UN "prim__intToFloat") (ty [IType] FlType) 1 (c_intToFloat)
131 (1, LIntFloat) total,
132 Prim (UN "prim__floatToInt") (ty [FlType] IType) 1 (c_floatToInt)
133 (1, LFloatInt) total,
135 Prim (UN "prim__floatExp") (ty [FlType] FlType) 1 (p_floatExp)
137 Prim (UN "prim__floatLog") (ty [FlType] FlType) 1 (p_floatLog)
139 Prim (UN "prim__floatSin") (ty [FlType] FlType) 1 (p_floatSin)
141 Prim (UN "prim__floatCos") (ty [FlType] FlType) 1 (p_floatCos)
143 Prim (UN "prim__floatTan") (ty [FlType] FlType) 1 (p_floatTan)
145 Prim (UN "prim__floatASin") (ty [FlType] FlType) 1 (p_floatASin)
147 Prim (UN "prim__floatACos") (ty [FlType] FlType) 1 (p_floatACos)
149 Prim (UN "prim__floatATan") (ty [FlType] FlType) 1 (p_floatATan)
151 Prim (UN "prim__floatSqrt") (ty [FlType] FlType) 1 (p_floatSqrt)
153 Prim (UN "prim__floatFloor") (ty [FlType] FlType) 1 (p_floatFloor)
155 Prim (UN "prim__floatCeil") (ty [FlType] FlType) 1 (p_floatCeil)
158 Prim (UN "prim__strHead") (ty [StrType] ChType) 1 (p_strHead)
159 (1, LStrHead) partial,
160 Prim (UN "prim__strTail") (ty [StrType] StrType) 1 (p_strTail)
161 (1, LStrTail) partial,
162 Prim (UN "prim__strCons") (ty [ChType, StrType] StrType) 2 (p_strCons)
164 Prim (UN "prim__strIndex") (ty [StrType, IType] ChType) 2 (p_strIndex)
165 (2, LStrIndex) partial,
166 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
168 Prim (UN "prim__readString") (ty [PtrType] StrType) 1 (p_cantreduce)
169 (1, LReadStr) partial,
170 Prim (UN "prim__vm") (ty [] PtrType) 0 (p_cantreduce)
173 Prim (UN "prim__stdin") (ty [] PtrType) 0 (p_cantreduce)
175 Prim (UN "prim__believe_me") believeTy 3 (p_believeMe)
176 (3, LNoOp) partial -- ahem
179p_believeMe [_,_,x] = Just x
180p_believeMe _ = Nothing
182iBin op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
185bBin op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (BI (op x y))
188bBini op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (I (op x y))
191biBin op = iBin (\x y -> if (op x y) then 1 else 0)
192bbBin op = bBini (\x y -> if (op x y) then 1 else 0)
194fBin op [VConstant (Fl x), VConstant (Fl y)] = Just $ VConstant (Fl (op x y))
197bfBin op [VConstant (Fl x), VConstant (Fl y)] = let i = (if op x y then 1 else 0) in
198 Just $ VConstant (I i)
201bcBin op [VConstant (Ch x), VConstant (Ch y)] = let i = (if op x y then 1 else 0) in
202 Just $ VConstant (I i)
205bsBin op [VConstant (Str x), VConstant (Str y)]
206 = let i = (if op x y then 1 else 0) in
207 Just $ VConstant (I i)
210sBin op [VConstant (Str x), VConstant (Str y)] = Just $ VConstant (Str (op x y))
213c_intToStr [VConstant (I x)] = Just $ VConstant (Str (show x))
214c_intToStr _ = Nothing
215c_strToInt [VConstant (Str x)] = case reads x of
216 [(n,"")] -> Just $ VConstant (I n)
217 _ -> Just $ VConstant (I 0)
218c_strToInt _ = Nothing
220c_intToChar [VConstant (I x)] = Just $ VConstant (Ch (toEnum x))
221c_intToChar _ = Nothing
222c_charToInt [VConstant (Ch x)] = Just $ VConstant (I (fromEnum x))
223c_charToInt _ = Nothing
225c_intToBigInt [VConstant (I x)] = Just $ VConstant (BI (fromIntegral x))
226c_intToBigInt _ = Nothing
227c_bigIntToInt [VConstant (BI x)] = Just $ VConstant (I (fromInteger x))
228c_bigIntToInt _ = Nothing
230c_bigIntToStr [VConstant (BI x)] = Just $ VConstant (Str (show x))
231c_bigIntToStr _ = Nothing
232c_strToBigInt [VConstant (Str x)] = case reads x of
233 [(n,"")] -> Just $ VConstant (BI n)
234 _ -> Just $ VConstant (BI 0)
235c_strToBigInt _ = Nothing
237c_floatToStr [VConstant (Fl x)] = Just $ VConstant (Str (show x))
238c_floatToStr _ = Nothing
239c_strToFloat [VConstant (Str x)] = case reads x of
240 [(n,"")] -> Just $ VConstant (Fl n)
241 _ -> Just $ VConstant (Fl 0)
242c_strToFloat _ = Nothing
244c_floatToInt [VConstant (Fl x)] = Just $ VConstant (I (truncate x))
245c_floatToInt _ = Nothing
247c_intToFloat [VConstant (I x)] = Just $ VConstant (Fl (fromIntegral x))
248c_intToFloat _ = Nothing
250p_fPrim f [VConstant (Fl x)] = Just $ VConstant (Fl (f x))
253p_floatExp = p_fPrim exp
254p_floatLog = p_fPrim log
255p_floatSin = p_fPrim sin
256p_floatCos = p_fPrim cos
257p_floatTan = p_fPrim tan
258p_floatASin = p_fPrim asin
259p_floatACos = p_fPrim acos
260p_floatATan = p_fPrim atan
261p_floatSqrt = p_fPrim sqrt
262p_floatFloor = p_fPrim (fromInteger . floor)
263p_floatCeil = p_fPrim (fromInteger . ceiling)
265p_strHead [VConstant (Str (x:xs))] = Just $ VConstant (Ch x)
267p_strTail [VConstant (Str (x:xs))] = Just $ VConstant (Str xs)
269p_strIndex [VConstant (Str xs), VConstant (I i)]
270 | i < length xs = Just $ VConstant (Ch (xs!!i))
271p_strIndex _ = Nothing
272p_strCons [VConstant (Ch x), VConstant (Str xs)] = Just $ VConstant (Str (x:xs))
274p_strRev [VConstant (Str xs)] = Just $ VConstant (Str (reverse xs))
277p_cantreduce _ = Nothing
279elabPrim :: Prim -> Idris ()
280elabPrim (Prim n ty i def sc tot)
281 = do updateContext (addOperator n ty i def)
284 putIState i { idris_scprims = (n, sc) : idris_scprims i }
287elabPrims = do mapM_ (elabDecl EAll toplevel)
288 (map (PData "" defaultSyntax (FC "builtin" 0) False)
289 [inferDecl, unitDecl, falseDecl, pairDecl, eqDecl])
290 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