Skip to content

Commit

Permalink
General parens and $ cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TotallyNotChase committed Jan 7, 2022
1 parent 8149e3d commit dcd85aa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Plutarch/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pconsBuiltin = phoistAcyclic $ pforce $ punsafeBuiltin PLC.MkCons
instance PLC.DefaultUni `PLC.Contains` PHaskellType a => PlutusType (PBuiltinList a) where
type PInner (PBuiltinList a) b = PBuiltinList a
pcon' :: forall s. PBuiltinList a s -> forall b. Term s (PInner (PBuiltinList a) b)
pcon' (PCons x xs) = pconsBuiltin # x # (pto xs)
pcon' (PCons x xs) = pconsBuiltin # x # pto xs
pcon' PNil = pconstant @(PBuiltinList a) []
pmatch' xs f =
pforce $
Expand Down
2 changes: 1 addition & 1 deletion Plutarch/DataRepr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pmatchDataRepr d handlers =

applyHandlers :: Term s (PBuiltinList PData) -> DataReprHandlers out defs s -> [Term s out]
applyHandlers _ DRHNil = []
applyHandlers args (DRHCons handler rest) = (handler $ punsafeCoerce args) : applyHandlers args rest
applyHandlers args (DRHCons handler rest) = handler (punsafeCoerce args) : applyHandlers args rest

go ::
(Dig, Term s out) ->
Expand Down
2 changes: 1 addition & 1 deletion Plutarch/Either.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ instance PlutusType (PEither a b) where
type PInner (PEither a b) c = (a :--> c) :--> (b :--> c) :--> c
pcon' (PLeft x) = plam $ \f (_ :: Term _ _) -> f # x
pcon' (PRight y) = plam $ \_ g -> g # y
pmatch' p f = p # (plam $ \x -> f . PLeft $ x) # (plam $ \y -> f . PRight $ y)
pmatch' p f = p # plam (f . PLeft) # plam (f . PRight)
32 changes: 16 additions & 16 deletions Plutarch/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ data PList (a :: k -> Type) (s :: k)
| PSNil

instance PlutusType (PList a) where
type PInner (PList a) c = (a :--> PList a :--> c) :--> (PDelayed c) :--> c
type PInner (PList a) c = (a :--> PList a :--> c) :--> PDelayed c :--> c

pcon' :: forall s. PList a s -> forall b. Term s (PInner (PList a) b)
pcon' (PSCons x xs) = plam $ \match_cons (_ :: Term _ _) -> match_cons # x # xs
pcon' PSNil = plam $ \_match_cons match_nil -> pforce match_nil
pmatch' xs f =
xs # (plam $ \x xs -> f (PSCons x xs)) # pdelay (f PSNil)
xs # plam (\x xs -> f (PSCons x xs)) # pdelay (f PSNil)

instance PEq a => PEq (PList a) where
(#==) xs ys = plistEquals # xs # ys
Expand All @@ -72,11 +72,11 @@ class PListLike (list :: (k -> Type) -> k -> Type) where

-- | Return the first element of a list. Partial, throws an error upon encountering an empty list.
phead :: PIsListLike list a => Term s (list a :--> a)
phead = pelimList (plam $ \x _xs -> x) perror
phead = phoistAcyclic $ pelimList (plam $ \x _xs -> x) perror

-- | Take the tail of a list, meaning drop its head. Partial, throws an error upon encountering an empty list.
ptail :: PIsListLike list a => Term s (list a :--> list a)
ptail = pelimList (plam $ \_x xs -> xs) perror
ptail = phoistAcyclic $ pelimList (plam $ \_x xs -> xs) perror

-- | / O(1) /. Check if a list is empty
pnull :: PIsListLike list a => Term s (list a :--> PBool)
Expand All @@ -98,7 +98,7 @@ type PIsListLike list a = (PListLike list, PElemConstraint list a)

-- | / O(n) /. Convert from any ListLike to any ListLike, provided both lists' element constraints are met.
pconvertLists :: forall f g a s. (PElemConstraint f a, PElemConstraint g a, PListLike f, PListLike g) => Term s (f a :--> g a)
pconvertLists =
pconvertLists = phoistAcyclic $
pfix #$ plam $ \self ->
pelimList
(plam $ \x xs -> pcons # x # (self # xs))
Expand Down Expand Up @@ -133,11 +133,11 @@ pelem =

-- | / O(n) /. Count the number of elements in the list
plength :: PIsListLike list a => Term s (list a :--> PInteger)
plength =
plength = phoistAcyclic $
plet
( pfix #$ plam $ \self ls n ->
pelimList
(plam $ \_x xs -> (self # xs # n + 1))
(plam $ \_x xs -> self # xs # n + 1)
n
# ls
)
Expand All @@ -151,27 +151,27 @@ pfoldr = phoistAcyclic $
plam $ \f z ->
precList
(\self x xs -> f # x # (self # xs))
(\_self -> z)
(const z)

-- | The same as 'pfoldr'', but with Haskell-level reduction function.
pfoldr' :: PIsListLike list a => (forall s. Term s a -> Term s b -> Term s b) -> Term s (b :--> list a :--> b)
pfoldr' f = phoistAcyclic $
plam $ \z ->
precList
(\self x xs -> f x (self # xs))
(\_self -> z)
(const z)

-- | / O(n) /. Check that predicate holds for all elements in a list
pall :: PIsListLike list a => Term s ((a :--> PBool) :--> list a :--> PBool)
pall = phoistAcyclic $
plam $ \predicate ->
pfoldr # (plam $ \x acc -> predicate # x #&& acc) # (pcon PTrue)
pfoldr # plam (\x acc -> predicate # x #&& acc) # pcon PTrue

-- | / O(n) /. Map a function over a list of elements
pmap :: (PListLike list, PElemConstraint list a, PElemConstraint list b) => Term s ((a :--> b) :--> list a :--> list b)
pmap = phoistAcyclic $
plam $ \f ->
precList (\self x xs -> pcons # (f # x) # (self # xs)) (\_self -> pnil)
precList (\self x xs -> pcons # (f # x) # (self # xs)) (const pnil)

-- | / O(n) /. Filter elements from a list that don't match the predicate.
pfilter :: PIsListLike list a => Term s ((a :--> PBool) :--> list a :--> list a)
Expand All @@ -185,7 +185,7 @@ pfilter =
(pcons # x # (self # xs))
(self # xs)
)
(\_self -> pnil)
(const pnil)

--------------------------------------------------------------------------------

Expand All @@ -206,7 +206,7 @@ pconcat =
( \self x xs ->
pcons # x # (self # xs)
)
(\_self -> ys)
(const ys)
# xs

-- | / O(min(n, m)) /. Zip two lists together with a passed function. If the lists are of differing lengths, cut to the shortest.
Expand Down Expand Up @@ -245,7 +245,7 @@ pzipWith' f =
pelimList
( plam $ \x xs ->
pelimList
(plam $ \y ys -> pcons # (f x y) # (self # xs # ys))
(plam $ \y ys -> pcons # f x y # (self # xs # ys))
pnil
# ly
)
Expand All @@ -260,12 +260,12 @@ pzip ::
, PElemConstraint list (PPair a b)
) =>
Term s (list a :--> list b :--> list (PPair a b))
pzip = pzipWith' $ \x y -> pcon (PPair x y)
pzip = phoistAcyclic $ pzipWith' $ \x y -> pcon (PPair x y)

-- Horribly inefficient.
plistEquals :: (PIsListLike list a, PElemConstraint list PBool, PEq a) => Term s (list a :--> list a :--> PBool)
plistEquals =
phoistAcyclic $
plam $ \xs ys ->
plength # xs #== plength # ys
#&& pfoldr' (#&&) # (pcon PTrue) # (pzipWith' (#==) # xs # ys)
#&& pfoldr' (#&&) # pcon PTrue # (pzipWith' (#==) # xs # ys)
2 changes: 1 addition & 1 deletion Plutarch/Maybe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ instance PlutusType (PMaybe a) where
pcon' :: forall s. PMaybe a s -> forall b. Term s (PInner (PMaybe a) b)
pcon' (PJust x) = plam $ \f (_ :: Term _ _) -> f # x
pcon' PNothing = plam $ \_ g -> pforce g
pmatch' x f = x # (plam $ \inner -> f (PJust inner)) # (pdelay $ f PNothing)
pmatch' x f = x # plam (f . PJust) # pdelay (f PNothing)

0 comments on commit dcd85aa

Please sign in to comment.