on 047be9e help sign in
Idris-dev/src/Idris/Primitives.hs
325 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)]
48bigIntToInt x = foreign_ tyInt "bigIntToInt" [(x, tyBigInt)]
49strToBigInt x = foreign_ tyBigInt "strToBig" [(x, tyString)]
50bigIntToStr x = foreign_ tyString "bigToStr" [(x, tyBigInt)]
51strToFloat x = foreign_ tyFloat "strToFloat" [(x, tyString)]
52floatToStr x = foreign_ tyString "floatToStr" [(x, tyFloat)]
53intToFloat x = foreign_ tyFloat "intToFloat" [(x, tyInt)]
54floatToInt x = foreign_ tyInt "floatToInt" [(x, tyFloat)]
56floatExp x = foreign_ tyFloat "exp" [(x, tyFloat)]
57floatLog x = foreign_ tyFloat "log" [(x, tyFloat)]
58floatSin x = foreign_ tyFloat "sin" [(x, tyFloat)]
59floatCos x = foreign_ tyFloat "cos" [(x, tyFloat)]
60floatTan x = foreign_ tyFloat "tan" [(x, tyFloat)]
61floatASin x = foreign_ tyFloat "asin" [(x, tyFloat)]
62floatACos x = foreign_ tyFloat "acos" [(x, tyFloat)]
63floatATan x = foreign_ tyFloat "atan" [(x, tyFloat)]
64floatFloor x = foreign_ tyFloat "floor" [(x, tyFloat)]
65floatCeil x = foreign_ tyFloat "ceil" [(x, tyFloat)]
66floatSqrt x = foreign_ tyFloat "sqrt" [(x, tyFloat)]
68strIndex x i = foreign_ tyChar "strIndex" [(x, tyString), (i, tyInt)]
69strHead x = foreign_ tyChar "strHead" [(x, tyString)]
70strTail x = foreign_ tyString "strTail" [(x, tyString)]
71strCons x xs = foreign_ tyString "strCons" [(x, tyChar), (xs, tyString)]
72strRev x = foreign_ tyString "strrev" [(x, tyString)]
73strEq x y = foreign_ tyInt "streq" [(x, tyString), (y, tyString)]
74strLt x y = foreign_ tyInt "strlt" [(x, tyString), (y, tyString)]
76total = Total []
77partial = Partial NotCovering
79primitives =
80 -- operators
81 [Prim (UN "prim__addInt") (ty [IType, IType] IType) 2 (iBin (+))
82 (eOp E.plus_) total,
83 Prim (UN "prim__subInt") (ty [IType, IType] IType) 2 (iBin (-))
84 (eOp E.minus_) total,
85 Prim (UN "prim__mulInt") (ty [IType, IType] IType) 2 (iBin (*))
86 (eOp E.times_) total,
87 Prim (UN "prim__divInt") (ty [IType, IType] IType) 2 (iBin (div))
88 (eOp E.divide_) partial,
89 Prim (UN "prim__eqInt") (ty [IType, IType] IType) 2 (biBin (==))
90 (eOp E.eq_) total,
91 Prim (UN "prim__ltInt") (ty [IType, IType] IType) 2 (biBin (<))
92 (eOp E.lt_) total,
93 Prim (UN "prim__lteInt") (ty [IType, IType] IType) 2 (biBin (<=))
94 (eOp E.lte_) total,
95 Prim (UN "prim__gtInt") (ty [IType, IType] IType) 2 (biBin (>))
96 (eOp E.gt_) total,
97 Prim (UN "prim__gteInt") (ty [IType, IType] IType) 2 (biBin (>=))
98 (eOp E.gte_) total,
99 Prim (UN "prim__eqChar") (ty [ChType, ChType] IType) 2 (bcBin (==))
100 (eOp E.eq_) total,
101 Prim (UN "prim__ltChar") (ty [ChType, ChType] IType) 2 (bcBin (<))
102 (eOp E.lt_) total,
103 Prim (UN "prim__lteChar") (ty [ChType, ChType] IType) 2 (bcBin (<=))
104 (eOp E.lte_) total,
105 Prim (UN "prim__gtChar") (ty [ChType, ChType] IType) 2 (bcBin (>))
106 (eOp E.gt_) total,
107 Prim (UN "prim__gteChar") (ty [ChType, ChType] IType) 2 (bcBin (>=))
108 (eOp E.gte_) total,
109 Prim (UN "prim__addBigInt") (ty [BIType, BIType] BIType) 2 (bBin (+))
110 (eOpFn tyBigInt tyBigInt "addBig") total,
111 Prim (UN "prim__subBigInt") (ty [BIType, BIType] BIType) 2 (bBin (-))
112 (eOpFn tyBigInt tyBigInt "subBig") total,
113 Prim (UN "prim__mulBigInt") (ty [BIType, BIType] BIType) 2 (bBin (*))
114 (eOpFn tyBigInt tyBigInt "mulBig") total,
115 Prim (UN "prim__divBigInt") (ty [BIType, BIType] BIType) 2 (bBin (div))
116 (eOpFn tyBigInt tyBigInt "divBig") partial,
117 Prim (UN "prim__eqBigInt") (ty [BIType, BIType] IType) 2 (bbBin (==))
118 (eOpFn tyBigInt tyInt "eqBig") total,
119 Prim (UN "prim__ltBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<))
120 (eOpFn tyBigInt tyInt "ltBig") total,
121 Prim (UN "prim__lteBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<=))
122 (eOpFn tyBigInt tyInt "leBig") total,
123 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>))
124 (eOpFn tyBigInt tyInt "gtBig") total,
125 Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>=))
126 (eOpFn tyBigInt tyInt "geBig") total,
127 Prim (UN "prim__addFloat") (ty [FlType, FlType] FlType) 2 (fBin (+))
128 (eOp E.plusF_) total,
129 Prim (UN "prim__subFloat") (ty [FlType, FlType] FlType) 2 (fBin (-))
130 (eOp E.minusF_) total,
131 Prim (UN "prim__mulFloat") (ty [FlType, FlType] FlType) 2 (fBin (*))
132 (eOp E.timesF_) total,
133 Prim (UN "prim__divFloat") (ty [FlType, FlType] FlType) 2 (fBin (/))
134 (eOp E.divideF_) total,
135 Prim (UN "prim__eqFloat") (ty [FlType, FlType] IType) 2 (bfBin (==))
136 (eOp E.eqF_) total,
137 Prim (UN "prim__ltFloat") (ty [FlType, FlType] IType) 2 (bfBin (<))
138 (eOp E.ltF_) total,
139 Prim (UN "prim__lteFloat") (ty [FlType, FlType] IType) 2 (bfBin (<=))
140 (eOp E.lteF_) total,
141 Prim (UN "prim__gtFloat") (ty [FlType, FlType] IType) 2 (bfBin (>))
142 (eOp E.gtF_) total,
143 Prim (UN "prim__gteFloat") (ty [FlType, FlType] IType) 2 (bfBin (>=))
144 (eOp E.gteF_) total,
145 Prim (UN "prim__concat") (ty [StrType, StrType] StrType) 2 (sBin (++))
146 ([E.name "x", E.name "y"], (fun "append") @@ fun "x" @@ fun "y") total,
147 Prim (UN "prim__eqString") (ty [StrType, StrType] IType) 2 (bsBin (==))
148 ([E.name "x", E.name "y"], strEq (fun "x") (fun "y")) total,
149 Prim (UN "prim__ltString") (ty [StrType, StrType] IType) 2 (bsBin (<))
150 ([E.name "x", E.name "y"], strLt (fun "x") (fun "y")) total,
151 -- Conversions
152 Prim (UN "prim__strToInt") (ty [StrType] IType) 1 (c_strToInt)
153 ([E.name "x"], strToInt (fun "x")) total,
154 Prim (UN "prim__intToStr") (ty [IType] StrType) 1 (c_intToStr)
155 ([E.name "x"], intToStr (fun "x")) total,
156 Prim (UN "prim__charToInt") (ty [ChType] IType) 1 (c_charToInt)
157 ([E.name "x"], charToInt (fun "x")) total,
158 Prim (UN "prim__intToChar") (ty [IType] ChType) 1 (c_intToChar)
159 ([E.name "x"], intToChar (fun "x")) total,
160 Prim (UN "prim__intToBigInt") (ty [IType] BIType) 1 (c_intToBigInt)
161 ([E.name "x"], intToBigInt (fun "x")) total,
162 Prim (UN "prim__bigIntToInt") (ty [BIType] IType) 1 (c_bigIntToInt)
163 ([E.name "x"], bigIntToInt (fun "x")) total,
164 Prim (UN "prim__strToBigInt") (ty [StrType] BIType) 1 (c_strToBigInt)
165 ([E.name "x"], strToBigInt (fun "x")) total,
166 Prim (UN "prim__bigIntToStr") (ty [BIType] StrType) 1 (c_bigIntToStr)
167 ([E.name "x"], bigIntToStr (fun "x")) total,
168 Prim (UN "prim__strToFloat") (ty [StrType] FlType) 1 (c_strToFloat)
169 ([E.name "x"], strToFloat (fun "x")) total,
170 Prim (UN "prim__floatToStr") (ty [FlType] StrType) 1 (c_floatToStr)
171 ([E.name "x"], floatToStr (fun "x")) total,
172 Prim (UN "prim__intToFloat") (ty [IType] FlType) 1 (c_intToFloat)
173 ([E.name "x"], intToFloat (fun "x")) total,
174 Prim (UN "prim__floatToInt") (ty [FlType] IType) 1 (c_floatToInt)
175 ([E.name "x"], floatToInt (fun "x")) total,
177 Prim (UN "prim__floatExp") (ty [FlType] FlType) 1 (p_floatExp)
178 ([E.name "x"], floatExp (fun "x")) total,
179 Prim (UN "prim__floatLog") (ty [FlType] FlType) 1 (p_floatLog)
180 ([E.name "x"], floatLog (fun "x")) total,
181 Prim (UN "prim__floatSin") (ty [FlType] FlType) 1 (p_floatSin)
182 ([E.name "x"], floatSin (fun "x")) total,
183 Prim (UN "prim__floatCos") (ty [FlType] FlType) 1 (p_floatCos)
184 ([E.name "x"], floatCos (fun "x")) total,
185 Prim (UN "prim__floatTan") (ty [FlType] FlType) 1 (p_floatTan)
186 ([E.name "x"], floatTan (fun "x")) total,
187 Prim (UN "prim__floatASin") (ty [FlType] FlType) 1 (p_floatASin)
188 ([E.name "x"], floatASin (fun "x")) total,
189 Prim (UN "prim__floatACos") (ty [FlType] FlType) 1 (p_floatACos)
190 ([E.name "x"], floatACos (fun "x")) total,
191 Prim (UN "prim__floatATan") (ty [FlType] FlType) 1 (p_floatATan)
192 ([E.name "x"], floatATan (fun "x")) total,
193 Prim (UN "prim__floatSqrt") (ty [FlType] FlType) 1 (p_floatSqrt)
194 ([E.name "x"], floatSqrt (fun "x")) total,
195 Prim (UN "prim__floatFloor") (ty [FlType] FlType) 1 (p_floatFloor)
196 ([E.name "x"], floatFloor (fun "x")) total,
197 Prim (UN "prim__floatCeil") (ty [FlType] FlType) 1 (p_floatCeil)
198 ([E.name "x"], floatCeil (fun "x")) total,
200 Prim (UN "prim__strHead") (ty [StrType] ChType) 1 (p_strHead)
201 ([E.name "x"], strHead (fun "x")) partial,
202 Prim (UN "prim__strTail") (ty [StrType] StrType) 1 (p_strTail)
203 ([E.name "x"], strTail (fun "x")) partial,
204 Prim (UN "prim__strCons") (ty [ChType, StrType] StrType) 2 (p_strCons)
205 ([E.name "x", E.name "xs"], strCons (fun "x") (fun "xs")) total,
206 Prim (UN "prim__strIndex") (ty [StrType, IType] ChType) 2 (p_strIndex)
207 ([E.name "x", E.name "i"], strIndex (fun "x") (fun "i")) partial,
208 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
209 ([E.name "x"], strRev (fun "x")) total,
211 Prim (UN "prim__believe_me") believeTy 3 (p_believeMe)
212 ([E.name "a", E.name "b", E.name "x"], fun "x") total -- ahem
213 ]
215p_believeMe [_,_,x] = Just x
216p_believeMe _ = Nothing
218iBin op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
219iBin _ _ = Nothing
221bBin op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (BI (op x y))
222bBin _ _ = Nothing
224bBini op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (I (op x y))
225bBini _ _ = Nothing
227biBin op = iBin (\x y -> if (op x y) then 1 else 0)
228bbBin op = bBini (\x y -> if (op x y) then 1 else 0)
230fBin op [VConstant (Fl x), VConstant (Fl y)] = Just $ VConstant (Fl (op x y))
231fBin _ _ = Nothing
233bfBin op [VConstant (Fl x), VConstant (Fl y)] = let i = (if op x y then 1 else 0) in
234 Just $ VConstant (I i)
235bfBin _ _ = Nothing
237bcBin op [VConstant (Ch x), VConstant (Ch y)] = let i = (if op x y then 1 else 0) in
238 Just $ VConstant (I i)
239bcBin _ _ = Nothing
241bsBin op [VConstant (Str x), VConstant (Str y)]
242 = let i = (if op x y then 1 else 0) in
243 Just $ VConstant (I i)
244bsBin _ _ = Nothing
246sBin op [VConstant (Str x), VConstant (Str y)] = Just $ VConstant (Str (op x y))
247sBin _ _ = Nothing
249c_intToStr [VConstant (I x)] = Just $ VConstant (Str (show x))
250c_intToStr _ = Nothing
251c_strToInt [VConstant (Str x)] = case reads x of
252 [(n,"")] -> Just $ VConstant (I n)
253 _ -> Just $ VConstant (I 0)
254c_strToInt _ = Nothing
256c_intToChar [VConstant (I x)] = Just $ VConstant (Ch (toEnum x))
257c_intToChar _ = Nothing
258c_charToInt [VConstant (Ch x)] = Just $ VConstant (I (fromEnum x))
259c_charToInt _ = Nothing
261c_intToBigInt [VConstant (I x)] = Just $ VConstant (BI (fromIntegral x))
262c_intToBigInt _ = Nothing
263c_bigIntToInt [VConstant (BI x)] = Just $ VConstant (I (fromInteger x))
264c_bigIntToInt _ = Nothing
266c_bigIntToStr [VConstant (BI x)] = Just $ VConstant (Str (show x))
267c_bigIntToStr _ = Nothing
268c_strToBigInt [VConstant (Str x)] = case reads x of
269 [(n,"")] -> Just $ VConstant (BI n)
270 _ -> Just $ VConstant (BI 0)
271c_strToBigInt _ = Nothing
273c_floatToStr [VConstant (Fl x)] = Just $ VConstant (Str (show x))
274c_floatToStr _ = Nothing
275c_strToFloat [VConstant (Str x)] = case reads x of
276 [(n,"")] -> Just $ VConstant (Fl n)
277 _ -> Just $ VConstant (Fl 0)
278c_strToFloat _ = Nothing
280c_floatToInt [VConstant (Fl x)] = Just $ VConstant (I (truncate x))
281c_floatToInt _ = Nothing
283c_intToFloat [VConstant (I x)] = Just $ VConstant (Fl (fromIntegral x))
284c_intToFloat _ = Nothing
286p_fPrim f [VConstant (Fl x)] = Just $ VConstant (Fl (f x))
287p_fPrim f _ = Nothing
289p_floatExp = p_fPrim exp
290p_floatLog = p_fPrim log
291p_floatSin = p_fPrim sin
292p_floatCos = p_fPrim cos
293p_floatTan = p_fPrim tan
294p_floatASin = p_fPrim asin
295p_floatACos = p_fPrim acos
296p_floatATan = p_fPrim atan
297p_floatSqrt = p_fPrim sqrt
298p_floatFloor = p_fPrim (fromInteger . floor)
299p_floatCeil = p_fPrim (fromInteger . ceiling)
301p_strHead [VConstant (Str (x:xs))] = Just $ VConstant (Ch x)
302p_strHead _ = Nothing
303p_strTail [VConstant (Str (x:xs))] = Just $ VConstant (Str xs)
304p_strTail _ = Nothing
305p_strIndex [VConstant (Str xs), VConstant (I i)]
306 | i < length xs = Just $ VConstant (Ch (xs!!i))
307p_strIndex _ = Nothing
308p_strCons [VConstant (Ch x), VConstant (Str xs)] = Just $ VConstant (Str (x:xs))
309p_strCons _ = Nothing
310p_strRev [VConstant (Str xs)] = Just $ VConstant (Str (reverse xs))
311p_strRev _ = Nothing
313elabPrim :: Prim -> Idris ()
314elabPrim (Prim n ty i def epic tot)
315 = do updateContext (addOperator n ty i def)
316 setTotality n tot
317 i <- getIState
318 putIState i { idris_prims = (n, epic) : idris_prims i }
320elabPrims :: Idris ()
321elabPrims = do mapM_ (elabDecl toplevel)
322 (map (PData defaultSyntax (FC "builtin" 0))
323 [inferDecl, unitDecl, falseDecl, pairDecl, eqDecl])
324 mapM_ elabPrim primitives