Skip to content

Commit

Permalink
Add functions to the Map module and reformat (#133)
Browse files Browse the repository at this point in the history
* Closes #132 
* Adds the intersection/difference/union functions to the Map module.
* Reformats the standard library for the recent changes in the Juvix
formatter.
  • Loading branch information
lukaszcz authored Nov 1, 2024
1 parent 37d3dd7 commit deaafbb
Show file tree
Hide file tree
Showing 41 changed files with 834 additions and 413 deletions.
20 changes: 12 additions & 8 deletions Stdlib/Cairo/Ec.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ import Stdlib.Data.Pair open;
module StarkCurve;
ALPHA : Field := 1;

BETA : Field := 0x6f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89;
BETA : Field :=
0x6f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89;

ORDER : Field := 0x800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f;
ORDER : Field :=
0x800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f;

GEN_X : Field := 0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca;
GEN_X : Field :=
0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca;

GEN_Y : Field := 0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f;
GEN_Y : Field :=
0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f;
end;

builtin ec_point
type Point :=
mkPoint@{
x : Field;
y : Field
y : Field;
};

module Internal;
Expand Down Expand Up @@ -57,9 +61,9 @@ double (point : Point) : Point :=
r_x := slope * slope - x - x;
r_y := slope * (x - r_x) - y;
in mkPoint@?{
x := r_x;
y := r_y
};
x := r_x;
y := r_y;
};

--- Adds two valid points on the EC.
add (point1 point2 : Point) : Point :=
Expand Down
12 changes: 7 additions & 5 deletions Stdlib/Cairo/Poseidon.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type PoseidonState :=
mkPoseidonState@{
s0 : Field;
s1 : Field;
s2 : Field
s2 : Field;
};

builtin poseidon
Expand All @@ -19,19 +19,21 @@ axiom poseidonHash : PoseidonState -> PoseidonState;

--- Hashes one element and retrieves a single field element output.
{-# eval: false #-}
poseidonHash1 (x : Field) : Field := PoseidonState.s0 (poseidonHash (mkPoseidonState x 0 1));
poseidonHash1 (x : Field) : Field :=
PoseidonState.s0 (poseidonHash (mkPoseidonState x 0 1));

--- Hashes two elements and retrieves a single field element output.
{-# eval: false #-}
poseidonHash2 (x y : Field) : Field := PoseidonState.s0 (poseidonHash (mkPoseidonState x y 2));
poseidonHash2 (x y : Field) : Field :=
PoseidonState.s0 (poseidonHash (mkPoseidonState x y 2));

--- Hashes n elements and retrieves a single field element output.
{-# eval: false #-}
poseidonHashList (list : List Field) : Field :=
let
go (acc : PoseidonState) : List Field -> PoseidonState
| [] := poseidonHash acc@PoseidonState{s0 := s0 + 1}
| [x] := poseidonHash acc@PoseidonState{ s0 := s0 + x; s1 := s1 + 1 }
| [x] := poseidonHash acc@PoseidonState{s0 := s0 + x; s1 := s1 + 1}
| (x1 :: x2 :: xs) :=
go (poseidonHash acc@PoseidonState{ s0 := s0 + x1; s1 := s1 + x2 }) xs;
go (poseidonHash acc@PoseidonState{s0 := s0 + x1; s1 := s1 + x2}) xs;
in PoseidonState.s0 (go (mkPoseidonState 0 0 0) list);
5 changes: 3 additions & 2 deletions Stdlib/Data/BinaryTree.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type BinaryTree (A : Type) :=
| node@{
left : BinaryTree A;
element : A;
right : BinaryTree A
right : BinaryTree A;
};

--- fold a tree in depth-first order
Expand All @@ -21,4 +21,5 @@ fold {A B} (f : A -> B -> B -> B) (acc : B) (tree : BinaryTree A) : B :=

length {A} (tree : BinaryTree A) : Nat := fold \{_ l r := 1 + l + r} 0 tree;

toList {A} (tree : BinaryTree A) : List A := fold \{e ls rs := e :: ls ++ rs} nil tree;
toList {A} (tree : BinaryTree A) : List A :=
fold \{e ls rs := e :: ls ++ rs} nil tree;
6 changes: 3 additions & 3 deletions Stdlib/Data/Bool.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ eqBoolI : Eq Bool :=
case x, y of
| true, true := true
| false, false := true
| _ := false
| _ := false;
};

{-# specialize: true, inline: case #-}
Expand All @@ -27,7 +27,7 @@ ordBoolI : Ord Bool :=
| false, false := Equal
| false, true := LessThan
| true, false := GreaterThan
| true, true := Equal
| true, true := Equal;
};

instance
Expand All @@ -36,5 +36,5 @@ showBoolI : Show Bool :=
show (x : Bool) : String :=
if
| x := "true"
| else := "false"
| else := "false";
};
4 changes: 2 additions & 2 deletions Stdlib/Data/Byte.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ eqByteI : Eq Byte := mkEq (Byte.==);
instance
showByteI : Show Byte :=
mkShow@{
show (x : Byte) : String := Show.show (Byte.toNat x)
show (x : Byte) : String := Show.show (Byte.toNat x);
};

{-# specialize: true, inline: case #-}
instance
fromNaturalByteI : FromNatural Byte :=
mkFromNatural@{
fromNat := Byte.fromNat
fromNat := Byte.fromNat;
};
10 changes: 5 additions & 5 deletions Stdlib/Data/Field.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ eqFieldI : Eq Field := mkEq (Field.==);
instance
showFieldI : Show Field :=
mkShow@{
show (f : Field) : String := Show.show (Field.toNat f)
show (f : Field) : String := Show.show (Field.toNat f);
};

{-# specialize: true, inline: case #-}
instance
fromNaturalFieldI : FromNatural Field :=
mkFromNatural@{
fromNat := Field.fromNat
fromNat := Field.fromNat;
};

{-# specialize: true, inline: case #-}
instance
naturalFieldI : Natural Field :=
mkNatural@{
+ := (Field.+);
* := (Field.*)
* := (Field.*);
};

{-# specialize: true, inline: case #-}
Expand All @@ -43,13 +43,13 @@ integralFieldI : Integral Field :=
mkIntegral@{
naturalI := naturalFieldI;
- := (Field.-);
fromInt := Field.fromInt
fromInt := Field.fromInt;
};

{-# specialize: true, inline: case #-}
instance
numericFieldI : Numeric Field :=
mkNumeric@{
integralI := integralFieldI;
/ := (Field./)
/ := (Field./);
};
8 changes: 4 additions & 4 deletions Stdlib/Data/Int.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ showIntI : Show Int := mkShow intToString;
instance
fromNaturalIntI : FromNatural Int :=
mkFromNatural@{
fromNat := ofNat
fromNat := ofNat;
};

{-# specialize: true, inline: case #-}
instance
naturalIntI : Natural Int :=
mkNatural@{
+ := (Int.+);
* := (Int.*)
* := (Int.*);
};

{-# specialize: true, inline: case #-}
Expand All @@ -54,13 +54,13 @@ integralIntI : Integral Int :=
mkIntegral@{
naturalI := naturalIntI;
- := (Int.-);
fromInt (x : Int) : Int := x
fromInt (x : Int) : Int := x;
};

{-# specialize: true, inline: case #-}
instance
divModIntI : DivMod Int :=
mkDivMod@{
div := Int.div;
mod := Int.mod
mod := Int.mod;
};
30 changes: 18 additions & 12 deletions Stdlib/Data/List.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import Stdlib.Trait.Ord open;
import Stdlib.Trait.Show open;
import Stdlib.Trait open;
import Stdlib.Trait.Partial open;
import Stdlib.Trait.Foldable open using {Foldable; module Polymorphic; fromPolymorphicFoldable};
import Stdlib.Trait.Foldable open using {
Foldable;
module Polymorphic;
fromPolymorphicFoldable;
};

--- π’ͺ(1). Partial function that returns the first element of a ;List;.
phead {A} {{Partial}} : List A -> A
Expand All @@ -30,7 +34,8 @@ eqListI {A} {{Eq A}} : Eq (List A) :=
| else := false;
in mkEq go;

isMember {A} {{Eq A}} (elem : A) (list : List A) : Bool := isElement (Eq.eq) elem list;
isMember {A} {{Eq A}} (elem : A) (list : List A) : Bool :=
isElement (Eq.eq) elem list;

instance
ordListI {A} {{Ord A}} : Ord (List A) :=
Expand All @@ -53,29 +58,30 @@ showListI {A} {{Show A}} : Show (List A) :=
| nil := "nil"
| (x :: xs) := Show.show x ++str " :: " ++str go xs;
in mkShow@{
show (list : List A) : String :=
case list of
| nil := "nil"
| s := "(" ++str go s ++str ")"
};
show (list : List A) : String :=
case list of
| nil := "nil"
| s := "(" ++str go s ++str ")";
};

{-# specialize: true, inline: case #-}
instance
functorListI : Functor List :=
mkFunctor@{
map := listMap
map := listMap;
};

{-# specialize: true, inline: true #-}
instance
monomorphicFunctorListI {A} : Monomorphic.Functor (List A) A := fromPolymorphicFunctor;
monomorphicFunctorListI {A} : Monomorphic.Functor (List A) A :=
fromPolymorphicFunctor;

{-# specialize: true, inline: case #-}
instance
polymorphicFoldableListI : Polymorphic.Foldable List :=
Polymorphic.mkFoldable@{
for := listFor;
rfor := listRfor
rfor := listRfor;
};

{-# specialize: true, inline: true #-}
Expand All @@ -88,11 +94,11 @@ applicativeListI : Applicative List :=
pure {A} (a : A) : List A := [a];
ap {A B} : (listOfFuns : List (A -> B)) -> (listOfAs : List A) -> List B
| [] _ := []
| (f :: fs) l := map f l ++ ap fs l
| (f :: fs) l := map f l ++ ap fs l;
};

instance
monadListI : Monad List :=
mkMonad@{
bind := flip concatMap
bind := flip concatMap;
};
15 changes: 10 additions & 5 deletions Stdlib/Data/List/Base.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ listRfor {A B} (fun : B -> A -> B) (acc : B) : (list : List A) -> B

--- Right-associative fold.
{-# specialize: [1] #-}
liftFoldr {A B} (fun : A -> B -> B) (acc : B) (list : List A) : B := listRfor (flip fun) acc list;
liftFoldr {A B} (fun : A -> B -> B) (acc : B) (list : List A) : B :=
listRfor (flip fun) acc list;

--- Left-associative and tail-recursive fold.
{-# specialize: [1] #-}
Expand Down Expand Up @@ -138,7 +139,8 @@ snoc {A} (list : List A) (elem : A) : List A := list ++ elem :: nil;

--- Concatenates a ;List; of ;List;s.
{-# isabelle-function: {name: "concat"} #-}
flatten {A} (listOfLists : List (List A)) : List A := listFoldl (++) nil listOfLists;
flatten {A} (listOfLists : List (List A)) : List A :=
listFoldl (++) nil listOfLists;

--- π’ͺ(𝓃). Inserts the given separator before every element in the given ;List;.
prependToAll {A} (separator : A) : (list : List A) -> List A
Expand Down Expand Up @@ -194,7 +196,8 @@ isEmpty {A} (list : List A) : Bool :=
--- π’ͺ(min(𝓂, 𝓃)). Returns a list containing the results of applying a function
--- to each pair of elements from the input lists.
{-# specialize: [1] #-}
zipWith {A B C} (fun : A -> B -> C) : (list1 : List A) -> (list2 : List B) -> List C
zipWith
{A B C} (fun : A -> B -> C) : (list1 : List A) -> (list2 : List B) -> List C
| nil _ := nil
| _ nil := nil
| (x :: xs) (y :: ys) := fun x y :: zipWith fun xs ys;
Expand Down Expand Up @@ -231,7 +234,8 @@ quickSort {A} {{Ord A}} (list : List A) : List A :=
go : List A -> List A
| nil := nil
| xs@(_ :: nil) := xs
| (x :: xs) := case partition \{y := y < x} xs of l1, l2 := go l1 ++ x :: go l2;
| (x :: xs) :=
case partition \{y := y < x} xs of l1, l2 := go l1 ++ x :: go l2;
in go list;

--- π’ͺ(𝓃) Filters out every ;nothing; from a ;List;.
Expand All @@ -244,7 +248,8 @@ syntax iterator concatMap {init := 0; range := 1};

--- 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} (fun : A -> List B) (list : List A) : List B := flatten (listMap fun list);
concatMap {A B} (fun : A -> List B) (list : List A) : List B :=
flatten (listMap fun list);

--- π’ͺ(𝓃 * 𝓂). Transposes a ;List; of ;List;s interpreted as a matrix.
transpose {A} : (listOfLists : List (List A)) -> List (List A)
Expand Down
Loading

0 comments on commit deaafbb

Please sign in to comment.