@@ -1,3 +1,5 @@
1+{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
2+
13module Idris.Primitives(elabPrims) where
24
35import Idris.ElabDecls
@@ -6,35 +8,67 @@ import Idris.AbsSyntax
68import Core.TT
79import Core.Evaluate
810
11+import Epic.Epic hiding (Term, Type, Name, fn)
12+import qualified Epic.Epic as E
13+
914data Prim = Prim { p_name :: Name,
1015 p_type :: Type,
1116 p_arity :: Int,
12− p_def :: [Value] -> Maybe Value
17+ p_def :: [Value] -> Maybe Value,
18+ p_epic :: ([E.Name], E.Term)
1319 }
1420
1521ty [] x = Constant x
1622ty (t:ts) x = Bind (MN 0 "T") (Pi (Constant t)) (ty ts x)
1723
24+type ETm = E.Term
25+type EOp = E.Op
26+
27+fun = E.fn
28+ref = E.ref
29+
30+eOp :: EOp -> ([E.Name], ETm)
31+eOp op = ([E.name "x", E.name "y"], E.op_ op (E.fn "x") (E.fn "y"))
32+
1833primitives =
19− [Prim (UN ["prim__addInt"]) (ty [IType, IType] IType) 2 (iBin (+)),
20− Prim (UN ["prim__subInt"]) (ty [IType, IType] IType) 2 (iBin (-)),
21− Prim (UN ["prim__mulInt"]) (ty [IType, IType] IType) 2 (iBin (*)),
22− Prim (UN ["prim__divInt"]) (ty [IType, IType] IType) 2 (iBin (div)),
23− Prim (UN ["prim__eqInt"]) (ty [IType, IType] IType) 2 (biBin (==)),
24− Prim (UN ["prim__ltInt"]) (ty [IType, IType] IType) 2 (biBin (<)),
25− Prim (UN ["prim__lteInt"]) (ty [IType, IType] IType) 2 (biBin (<=)),
26− Prim (UN ["prim__gtInt"]) (ty [IType, IType] IType) 2 (biBin (>)),
27− Prim (UN ["prim__gteInt"]) (ty [IType, IType] IType) 2 (biBin (>=)),
28− Prim (UN ["prim__addFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (+)),
29− Prim (UN ["prim__subFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (-)),
30− Prim (UN ["prim__mulFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (*)),
31− Prim (UN ["prim__divFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (/)),
32− Prim (UN ["prim__eqFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (==)),
33− Prim (UN ["prim__ltFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (<)),
34− Prim (UN ["prim__lteFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (<=)),
35− Prim (UN ["prim__gtFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (>)),
36− Prim (UN ["prim__gteFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (>=)),
34+ [Prim (UN ["prim__addInt"]) (ty [IType, IType] IType) 2 (iBin (+))
35+ (eOp E.plus_),
36+ Prim (UN ["prim__subInt"]) (ty [IType, IType] IType) 2 (iBin (-))
37+ (eOp E.minus_),
38+ Prim (UN ["prim__mulInt"]) (ty [IType, IType] IType) 2 (iBin (*))
39+ (eOp E.times_),
40+ Prim (UN ["prim__divInt"]) (ty [IType, IType] IType) 2 (iBin (div))
41+ (eOp E.divide_),
42+ Prim (UN ["prim__eqInt"]) (ty [IType, IType] IType) 2 (biBin (==))
43+ (eOp E.eq_),
44+ Prim (UN ["prim__ltInt"]) (ty [IType, IType] IType) 2 (biBin (<))
45+ (eOp E.lt_),
46+ Prim (UN ["prim__lteInt"]) (ty [IType, IType] IType) 2 (biBin (<=))
47+ (eOp E.lte_),
48+ Prim (UN ["prim__gtInt"]) (ty [IType, IType] IType) 2 (biBin (>))
49+ (eOp E.gt_),
50+ Prim (UN ["prim__gteInt"]) (ty [IType, IType] IType) 2 (biBin (>=))
51+ (eOp E.gte_),
52+ Prim (UN ["prim__addFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (+))
53+ (eOp E.plusF_),
54+ Prim (UN ["prim__subFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (-))
55+ (eOp E.minusF_),
56+ Prim (UN ["prim__mulFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (*))
57+ (eOp E.timesF_),
58+ Prim (UN ["prim__divFloat"]) (ty [FlType, FlType] FlType) 2 (fBin (/))
59+ (eOp E.divideF_),
60+ Prim (UN ["prim__eqFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (==))
61+ (eOp E.eqF_),
62+ Prim (UN ["prim__ltFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (<))
63+ (eOp E.ltF_),
64+ Prim (UN ["prim__lteFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (<=))
65+ (eOp E.lteF_),
66+ Prim (UN ["prim__gtFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (>))
67+ (eOp E.gtF_),
68+ Prim (UN ["prim__gteFloat"]) (ty [FlType, FlType] IType) 2 (bfBin (>=))
69+ (eOp E.gteF_),
3770 Prim (UN ["prim__concat"]) (ty [StrType, StrType] StrType) 2 (sBin (++))
71+ ([E.name "x", E.name "y"], (fun "append") @@ fun "x" @@ fun "y")
3872 ]
3973
4074iBin op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
@@ -53,7 +87,10 @@ sBin op [VConstant (Str x), VConstant (Str y)] = Just $ VConstant (Str (op x y))
5387sBin _ _ = Nothing
5488
5589elabPrim :: Prim -> Idris ()
56−elabPrim (Prim n ty i def) = updateContext (addOperator n ty i def)
90+elabPrim (Prim n ty i def epic)
91+ = do updateContext (addOperator n ty i def)
92+ i <- getIState
93+ putIState i { idris_prims = (n, epic) : idris_prims i }
5794
5895elabPrims :: Idris ()
5996elabPrims = do mapM_ (elabDecl toplevel)