-
Notifications
You must be signed in to change notification settings - Fork 12
/
Functions.idr
58 lines (49 loc) · 1.48 KB
/
Functions.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Python.Functions
import Python.Objects
import Python.Fields
import Python.Telescope
import Python.IO
%default total
%access public export
Function : (t : Telescope a) -> Signature
Function {a = a} t f = case f of
"__call__" => Call {a = a} t
_ => Object f
infix 5 ~>
||| Infix alias for functions with fixed arguments.
(~>) : List Type -> Type -> Type
(~>) args ret = Obj . Function $ simple args ret
infix 5 ~~>
(~~>) : List Type -> Type -> Field
(~~>) args ret = Attr $ args ~> ret
fun : (t : Telescope a) -> Field
fun t = Attr . Obj $ Function t
||| Strip the given tuple `xs` to the list of runtime-relevant values.
strip : (t : Telescope a) -> (args : a) -> List Dyn
strip (Return _) () = []
strip (Bind (Pi _ ) tf) (x ** xs) = toDyn x :: strip (tf x) xs
strip (Bind (Forall _ ) tf) (x ** xs) = strip (tf x) xs
strip (Bind (Default _ d) tf) (x ** xs) = toDyn x :: strip (tf $ fromMaybe d x) xs
infixl 4 $.
||| Duck-typed function call.
($.) :
{t : Telescope a}
-> (f : Obj sig)
-> {auto pf : sig "__call__" = Call t}
-> (args : a)
-> PIO $ retTy t args
($.) {t = t} (MkObj f) args =
unRaw <$>
foreign FFI_Py "_idris_call"
(Ptr -> List Dyn -> PIO (Raw $ Telescope.retTy t args))
f
(strip t args)
infixl 4 $:
||| Duck-typed function call, useful for chaining.
($:) :
{t : Telescope a}
-> (f : PIO (Obj sig))
-> {auto pf : sig "__call__" = Call t}
-> (args : a)
-> PIO $ retTy t args
($:) meth args = meth >>= \m => m $. args