Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some useful functions #55

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Stdlib/Data/Int.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ mod (ofNat m) (negSuc n) := ofNat (Nat.mod m (suc n));
mod (negSuc m) (ofNat n) := negNat (Nat.mod (suc m) n);
mod (negSuc m) (negSuc n) :=
negNat (Nat.mod (suc m) (suc n));

--- Absolute value
abs : Int -> Nat;
abs (ofNat n) := n;
abs (negSuc n) := suc n;
13 changes: 13 additions & 0 deletions Stdlib/Data/List.juvix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Stdlib.Data.List;

open import Stdlib.Data.Bool;
open import Stdlib.Data.Maybe;
open import Stdlib.Function;
open import Stdlib.Data.Nat;
open import Stdlib.Data.Ord;
Expand Down Expand Up @@ -223,3 +224,15 @@ quickSort {A} cmp :=
go (x :: xs) :=
qsHelper x (both go (partition (isGT ∘ cmp x) xs));
in go;

--- 𝒪(𝓃) Filters out every ;nothing; from a ;List;.
catMaybes : {A : Type} -> List (Maybe A) -> List A;
catMaybes nil := nil;
catMaybes (just h :: hs) := h :: catMaybes hs;
catMaybes (nothing :: hs) := catMaybes hs;

--- Applies a function to every item on a ;List; and concatenates the result.
--- 𝒪(𝓃), where 𝓃 is the number of items in the resulting list.
concatMap :
{A B : Type} -> (A -> List B) -> List A -> List B;
concatMap f := flatten ∘ map f;
2 changes: 1 addition & 1 deletion Stdlib/Data/Nat.juvix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Stdlib.Data.Nat;

open import Stdlib.Data.String;
open import Stdlib.Data.String.Base;
open import Stdlib.Data.Bool;

--- Inductive natural numbers. I.e. whole non-negative numbers.
Expand Down
9 changes: 6 additions & 3 deletions Stdlib/Data/String.juvix
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module Stdlib.Data.String;

--- Primitive representation of a sequence of characters.
builtin string
axiom String : Type;
open import Stdlib.Data.String.Base public;
open import Stdlib.Data.List;

--- Concatenation of two ;String;s.
infixr 5 ++str;
builtin string-concat
axiom ++str : String -> String -> String;

unlines : List String -> String;
unlines nil := "";
unlines (x :: xs) := x ++str "\n" ++str unlines xs;
5 changes: 5 additions & 0 deletions Stdlib/Data/String/Base.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Stdlib.Data.String.Base;

--- Primitive representation of a sequence of characters.
builtin string
axiom String : Type;
2 changes: 1 addition & 1 deletion Stdlib/Debug/Fail.juvix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Stdlib.Debug.Fail;

open import Stdlib.Data.String;
open import Stdlib.Data.String.Base;

--- Primitive that exits the program with an error message.
builtin fail
Expand Down
6 changes: 2 additions & 4 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ all: test

deps/quickcheck:
@mkdir -p deps/
@git clone https://github.com/anoma/juvix-quickcheck.git deps/quickcheck
@git -C deps/quickcheck checkout 2a23d8ed14d171fecf95892da401ddd37f96d40a
@mkdir -p deps/quickcheck/deps/stdlib
@rsync -av ../* deps/quickcheck/deps/stdlib --exclude "test"
@git clone --branch use-abs-stdlib https://github.com/anoma/juvix-quickcheck.git deps/quickcheck
$(MAKE) -C deps/quickcheck deps

build/Test: $(shell find ../ -name "*.juvix") $(wildcard deps/**/*.juvix) Test.juvix deps/quickcheck
@mkdir -p build
Expand Down