Skip to content
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: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ jobs:
run: if [ -f scripts/test ]; then nix develop -c bash ./scripts/test; fi

- name: Luacheck
run: nix develop -c luacheck --quiet --std lua51 --no-unused-args src/
run: nix develop -c luacheck --quiet --std lua51 --no-unused-args --max-line-length 130 src/

- name: Format check
run: nix fmt && git diff --exit-code
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.*
!/.gitignore
!/.github/
!/.tidyrc.json
!/.lua-format
/output/
10 changes: 10 additions & 0 deletions .lua-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# LuaFormatter config for the hand-written FFI under src/.
# 2-space indent. Keep simple functions on one line; column_limit sits a few
# columns under luacheck's 130 limit because lua-format under-counts the leading
# indent and trailing comma, so this keeps every emitted line within 130.
indent_width: 2
use_tab: false
column_limit: 126
continuation_indent_width: 2
keep_simple_function_one_line: true
keep_simple_control_block_one_line: true
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "source",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "source",
"width": 80
}
16 changes: 13 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ A PureScript→Lua FFI fork in the [`purescript-lua`](https://github.com/purescr

## Commands

All commands run inside the nix dev shell:

- Build: `nix develop -c ./scripts/build`
- Test (only if the fork has `scripts/test`): `nix develop -c bash ./scripts/test`
- Lint: `nix develop -c luacheck --quiet --std lua51 --no-unused-args src/`
- Lint: `nix develop -c luacheck --quiet --std lua51 --no-unused-args --max-line-length 130 src/`
- Format: `nix fmt` (check: `nix fmt && git diff --exit-code`)

## Formatting

`nix fmt` runs treefmt (`treefmt.nix`): nixfmt for Nix, `dhall format`, purs-tidy
for `*.purs` (config in `.tidyrc.json`), and LuaFormatter for the `*.lua` FFI
(config in `.lua-format`). LuaFormatter is used over StyLua because it keeps the
parentheses pslua's foreign-file parser requires. The Lua line budget is 130
columns, matching the `luacheck --max-line-length` above. The check is
content-based (`nix fmt && git diff --exit-code`) rather than `treefmt --ci`,
since the in-place formatters bump mtime even when content is unchanged, which
trips treefmt's `--fail-on-change`. CI and the pre-commit hook use it.

## Lua 5.1 target

Expand Down
23 changes: 22 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 39 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,33 @@
inputs.nixpkgs.follows = "nixpkgs";
};
pslua.url = "github:purescript-lua/purescript-lua";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, purescript-overlay, pslua }:
flake-utils.lib.eachDefaultSystem (system:
outputs =
{
self,
nixpkgs,
flake-utils,
purescript-overlay,
pslua,
treefmt-nix,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ purescript-overlay.overlays.default ];
};
in {
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
in
{
formatter = treefmtEval.config.build.wrapper;
checks.formatting = treefmtEval.config.build.check self;
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
dhall
Expand All @@ -31,8 +48,26 @@
spago-bin.spago-0_21_0
treefmt
];
# Install a content-based pre-commit hook. It compares the working
# tree diff before and after `nix fmt`, so it only objects to changes
# the formatter itself introduces (not the developer's existing
# unstaged work) and is not fooled by formatters that only bump mtime.
# Rewritten each shell entry to stay in sync with this flake.
shellHook = ''
hook=.git/hooks/pre-commit
if [ -d .git ]; then
printf '%s\n' \
'#!/usr/bin/env bash' \
'before=$(git diff)' \
'nix fmt >/dev/null 2>&1 || exit 0' \
'[ "$before" = "$(git diff)" ] || { echo "nix fmt changed files; re-stage them, then commit." >&2; exit 1; }' \
Comment thread
Unisay marked this conversation as resolved.
> "$hook"
chmod +x "$hook"
fi
'';
};
});
}
);

# --- Flake Local Nix Configuration ----------------------------
nixConfig = {
Expand Down
60 changes: 38 additions & 22 deletions src/Data/Enum.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module Data.Enum
( class Enum, succ, pred
, class BoundedEnum, cardinality, toEnum, fromEnum
( class Enum
, succ
, pred
, class BoundedEnum
, cardinality
, toEnum
, fromEnum
, toEnumWithDefaults
, Cardinality(..)
, enumFromTo
Expand Down Expand Up @@ -55,7 +60,7 @@ instance enumBoolean :: Enum Boolean where
succ false = Just true
succ _ = Nothing
pred true = Just false
pred _= Nothing
pred _ = Nothing

instance enumInt :: Enum Int where
succ n = if n < top then Just (n + 1) else Nothing
Expand Down Expand Up @@ -90,8 +95,10 @@ instance enumEither :: (BoundedEnum a, BoundedEnum b) => Enum (Either a b) where
pred (Right b) = maybe (Just (Left top)) (Just <<< Right) (pred b)

instance enumTuple :: (Enum a, BoundedEnum b) => Enum (Tuple a b) where
succ (Tuple a b) = maybe (flip Tuple bottom <$> succ a) (Just <<< Tuple a) (succ b)
pred (Tuple a b) = maybe (flip Tuple top <$> pred a) (Just <<< Tuple a) (pred b)
succ (Tuple a b) = maybe (flip Tuple bottom <$> succ a) (Just <<< Tuple a)
(succ b)
pred (Tuple a b) = maybe (flip Tuple top <$> pred a) (Just <<< Tuple a)
(pred b)

-- | Type class for finite enumerations.
-- |
Expand Down Expand Up @@ -189,7 +196,7 @@ enumFromTo = case _, _ of
| from < to -> unfoldr1 (go succ (<=) to) from
| otherwise -> unfoldr1 (go pred (>=) to) from
where
go step op to a = Tuple a (step a >>= \a' -> guard (a' `op` to) $> a')
go step op to a = Tuple a (step a >>= \a' -> guard (a' `op` to) $> a')

-- | Returns a sequence of elements from the first value, taking steps
-- | according to the difference between the first and second value, up to
Expand All @@ -205,7 +212,15 @@ enumFromTo = case _, _ of
-- |
-- | The example shows `Array` return values, but the result can be any type
-- | with an `Unfoldable1` instance.
enumFromThenTo :: forall f a. Unfoldable f => Functor f => BoundedEnum a => a -> a -> a -> f a
enumFromThenTo
:: forall f a
. Unfoldable f
=> Functor f
=> BoundedEnum a
=> a
-> a
-> a
-> f a
enumFromThenTo = unsafePartial \a b c ->
let
a' = fromEnum a
Expand All @@ -214,9 +229,9 @@ enumFromThenTo = unsafePartial \a b c ->
in
(toEnum >>> fromJust) <$> unfoldr (go (b' - a') c') a'
where
go step to e
| e <= to = Just (Tuple e (e + step))
| otherwise = Nothing
go step to e
| e <= to = Just (Tuple e (e + step))
| otherwise = Nothing

-- | Produces all successors of an `Enum` value, excluding the start value.
upFrom :: forall a u. Enum a => Unfoldable u => a -> u a
Expand Down Expand Up @@ -271,7 +286,8 @@ defaultPred toEnum' fromEnum' a = toEnum' (fromEnum' a - 1)
-- |
-- | Runs in `O(n)` where `n` is `fromEnum top`
defaultCardinality :: forall a. Bounded a => Enum a => Cardinality a
defaultCardinality = Cardinality $ go 1 (bottom :: a) where
defaultCardinality = Cardinality $ go 1 (bottom :: a)
where
go i x =
case succ x of
Just x' -> go (i + 1) x'
Expand All @@ -285,17 +301,15 @@ defaultCardinality = Cardinality $ go 1 (bottom :: a) where
-- | Runs in `O(n)` where `n` is `fromEnum a`.
defaultToEnum :: forall a. Bounded a => Enum a => Int -> Maybe a
defaultToEnum i' =
if i' < 0
then Nothing
else go i' bottom
if i' < 0 then Nothing
else go i' bottom
where
go i x =
if i == 0
then Just x
-- We avoid using >>= here because it foils tail-call optimization
else case succ x of
Just x' -> go (i - 1) x'
Nothing -> Nothing
if i == 0 then Just x
-- We avoid using >>= here because it foils tail-call optimization
else case succ x of
Just x' -> go (i - 1) x'
Nothing -> Nothing

-- | Provides a default implementation for `fromEnum`.
-- |
Expand All @@ -304,7 +318,8 @@ defaultToEnum i' =
-- |
-- | Runs in `O(n)` where `n` is `fromEnum a`.
defaultFromEnum :: forall a. Enum a => a -> Int
defaultFromEnum = go 0 where
defaultFromEnum = go 0
where
go i x =
case pred x of
Just x' -> go (i + 1) x'
Expand All @@ -314,7 +329,8 @@ diag :: forall a. a -> Tuple a a
diag a = Tuple a a

charToEnum :: Int -> Maybe Char
charToEnum n | n >= toCharCode bottom && n <= toCharCode top = Just (fromCharCode n)
charToEnum n | n >= toCharCode bottom && n <= toCharCode top = Just
(fromCharCode n)
charToEnum _ = Nothing

foreign import toCharCode :: Char -> Int
Expand Down
6 changes: 4 additions & 2 deletions src/Data/Enum/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ genBoundedEnum :: forall m a. MonadGen m => BoundedEnum a => m a
genBoundedEnum =
case succ bottom of
Just a →
let possibilities = enumFromTo a top :: Array a
in elements (bottom :| possibilities)
let
possibilities = enumFromTo a top :: Array a
in
elements (bottom :| possibilities)
Nothing →
pure bottom
Loading
Loading