-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLib1.idr
151 lines (96 loc) · 3.49 KB
/
Lib1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
module Lib1
import System.FFI
import PrimIO
import Prelude
import Control.Linear.LIO
import Control.Monad.State.State
import Control.Monad.State
import Data.Fin
%foreign "C:getPointer, libsmall"
getPointer : PrimIO (Ptr Int)
%foreign "C:getArray, libsmall"
getArray : Int -> PrimIO (Ptr Int)
%foreign "C:freePointer, libsmall"
freePointer : (1 mem : Ptr Int) -> PrimIO ()
%foreign "C:readNumber, libsmall"
readPointer : (1 mem : Ptr Int) -> PrimIO Int
%foreign "C:readPointerOffset, libsmall"
readPointerOffset : Int -> (1 mem : Ptr Int) -> PrimIO Int
%foreign "C:writePointer, libsmall"
writePointer : (1 mem : Ptr Int) -> Int -> PrimIO (Ptr Int)
%foreign "C:writeOffset, libsmall"
writeOffset : Int -> (1 mem : Ptr Int) -> Int -> PrimIO ()
data MyPtr = W (Ptr Int)
alloc : ((1 mem : MyPtr) -> IO a) -> IO a
alloc f = let (>>=) = io_bind in do
x <- W <$> (fromPrim getPointer)
f x
write : Int -> ((1 mem : MyPtr) -> IO a) -> (1 mem : MyPtr) -> IO a
write int f (W ptr) = let (>>=) = io_bind in do
x <- fromPrim $ writePointer ptr int
f (W x)
read' : (Int -> (1 mem : MyPtr) -> IO a) -> MyPtr -> IO a
read' f (W ptr) = do
x <- fromPrim (readPointer ptr)
f x (W ptr)
read : (Int -> (1 mem : MyPtr) -> IO a) -> (1 _ : MyPtr) -> IO a
read f = assert_linear (read' f)
free : (1 mem : MyPtr) -> IO ()
free (W ptr) = fromPrim (freePointer ptr)
freeze' : MyPtr -> IO Int
freeze' (W ptr) = do
x <- fromPrim (readPointer ptr)
free (W ptr)
pure x
freeze : (1 mem : MyPtr) -> IO Int
freeze = assert_linear freeze'
data MyVect : (len : Nat) -> Type where
Arr : (Ptr Int) -> MyVect len
createArr : (size : Nat) -> ((1 _ : MyVect size) -> IO a) -> IO a
createArr size f = do
x <- fromPrim (getArray (cast size))
f (Arr x)
conv : (Fin m) -> Int
conv = cast . finToInteger
getIndex' : (index : Fin m) -> (Int -> (1 _ : MyVect m) -> IO a) -> MyVect m -> IO a
getIndex' index f a@(Arr ptr) = do
x <- fromPrim $ readPointerOffset (conv index) ptr
f x a
getIndex : (index : Fin m) -> (Int -> (1 _ : MyVect m) -> IO a) -> (1 _ : MyVect m) -> IO a
getIndex index f = assert_linear (getIndex' index f)
writeArr' : (index : Fin m) -> Int -> ((1 mem : MyVect m) -> IO a) -> (MyVect m) -> IO a
writeArr' index toWrite f a@(Arr ptr) = do
x <- fromPrim $ writeOffset (conv index) ptr toWrite
f a
writeArr : (index : Fin m) -> Int -> ((1 mem : MyVect m) -> IO a) -> (1 _ : MyVect m) -> IO a
writeArr index toWrite f = assert_linear (writeArr' index toWrite f)
freeArr : (1 mem : MyVect m) -> IO ()
freeArr (Arr ptr) = fromPrim $ freePointer ptr
test : IO ()
test = alloc (\x => (write 10 (\x2 => free x2) x))
-->>= : M a -> (a -> M b) -> M b
--action1 >>= (\x => action2 >>= (\x3 => action))
{-x <- alloc
x2 <- write 10 x
free x2
-}
x : IO ()
x = do
createArr 10 (writeArr 9 200 (freeArr))
print 10
main : IO ()
main = x
-- For Monad use?
writeM : Int -> (1 mem : MyPtr) -> ((1 mem : MyPtr) -> IO a) -> IO a
writeM int ptr f = write int f ptr
readM : (1 _ : MyPtr) -> (Int -> (1 mem : MyPtr) -> IO a) -> IO a
readM ptr f = read f ptr
app : (1 _ : (a -> b)) -> a -> b
app f g = f g
--alloc (\x => (alloc (\x2 => )))
-- read write and then free works, but free is IO so this is a terrible example
t : IO ()
t = let (>>=) = app in do
x <- alloc
x2 <- writeM 10 x
free x2