-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ codegen ] more flexible array implementation on JS backends (#2966)
- Loading branch information
1 parent
edc000c
commit 2739c3a
Showing
7 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import System | ||
|
||
import Data.Nat | ||
|
||
data ArrayData : Type -> Type where [external] | ||
|
||
-- We use `Integer` for indexing here instead of `Int` as in `Data.IOArray.Prims` | ||
%extern prim__newArray : forall a . Integer -> a -> PrimIO (ArrayData a) | ||
%extern prim__arrayGet : forall a . ArrayData a -> Integer -> PrimIO a | ||
%extern prim__arraySet : forall a . ArrayData a -> Integer -> a -> PrimIO () | ||
|
||
record IOArray (n : Nat) a where | ||
constructor MkIOArray | ||
content : ArrayData a | ||
|
||
newArray : HasIO io => (n : Nat) -> a -> io (IOArray n a) | ||
newArray size v = pure (MkIOArray !(primIO (prim__newArray (cast size) v))) | ||
|
||
writeArray : | ||
HasIO io | ||
=> IOArray n a | ||
-> (m : Nat) | ||
-> {auto 0 lt : LT m n} | ||
-> a | ||
-> io () | ||
writeArray arr pos el = primIO (prim__arraySet (content arr) (cast pos) el) | ||
|
||
readArray : | ||
HasIO io | ||
=> IOArray n a | ||
-> (m : Nat) | ||
-> {auto 0 lt : LT m n} | ||
-> io a | ||
readArray arr pos = primIO (prim__arrayGet (content arr) (cast pos)) | ||
|
||
toList : {n : _} -> HasIO io => IOArray n a -> io (List a) | ||
toList arr = iter n reflexive [] | ||
where | ||
iter : (m : Nat) -> (0 lte : LTE m n) -> List a -> io (List a) | ||
iter 0 _ acc = pure acc | ||
iter (S k) p acc = readArray arr k >>= \el => iter k (lteSuccLeft p) (el :: acc) | ||
|
||
main : IO () | ||
main = do | ||
x <- newArray 20 "" | ||
writeArray x 11 "World" | ||
writeArray x 10 "Hello" | ||
printLn !(toList x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1/1: Building array (array.idr) | ||
Main> ["", "", "", "", "", "", "", "", "", "", "Hello", "World", "", "", "", "", "", "", "", ""] | ||
Main> Bye for now! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:exec main | ||
:q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
rm -rf build | ||
|
||
$1 --no-color --console-width 0 --cg node --no-banner array.idr < input |