Skip to content

dhall lint no longer sorts let bindings #2503

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

Merged
merged 2 commits into from
Apr 9, 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
53 changes: 0 additions & 53 deletions dhall/src/Dhall/Lint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module Dhall.Lint
) where

import Control.Applicative ((<|>))
import Data.List.NonEmpty (NonEmpty (..))

import Dhall.Syntax
( Binding (..)
Expand All @@ -38,7 +37,6 @@ import Dhall.Syntax

import qualified Data.Foldable as Foldable
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map as Map
import qualified Data.Text as Text
import qualified Dhall.Core as Core
import qualified Dhall.Map
Expand All @@ -63,7 +61,6 @@ lint = Dhall.Optics.rewriteOf subExpressions rewrite
<|> fixParentPath e
<|> removeLetInLet e
<|> addPreludeExtensions e
<|> sortImports e

-- | Remove unused `Let` bindings.
removeUnusedBindings :: Eq a => Expr s a -> Maybe (Expr s a)
Expand Down Expand Up @@ -212,53 +209,3 @@ useToMap (ListLit _ keyValues)
Nothing
useToMap _ =
Nothing

-- | This sorts `let` bindings to move imports to the front if doing so does not
-- change the behavior of the code.
sortImports :: Eq s => Expr s Import -> Maybe (Expr s Import)
sortImports oldExpression@(Let binding0 oldBody0)
| oldExpression == newExpression = Nothing
| otherwise = Just newExpression
where
toBool (Embed _ ) = False
toBool (Note _ e) = toBool e
toBool _ = True

process (seen, index) Binding{..} oldBody function = (pair, pairs, newBody)
where
order =
if b then index else Map.findWithDefault (0 :: Int) variable seen

b = toBool value

pair = (order, function)

~(pairs, newBody) =
label (Map.insert variable order seen, index + 1) oldBody

label state (Let binding oldBody) = (pair : pairs, newBody)
where
function = Let binding

~(pair, pairs, newBody) = process state binding oldBody function

label state (Note src (Let binding oldBody)) = (pair : pairs, newBody)
where
function e = Note src (Let binding e)

~(pair, pairs, newBody) = process state binding oldBody function

label _ body =
([], body)

~(pairs0, newBody0) = (pair :| pairs, newBody)
where
function = Let binding0

~(pair, pairs, newBody) =
process (Map.empty, 1) binding0 oldBody0 function

sortedFunctions = fmap snd (NonEmpty.sortWith fst pairs0)

newExpression = foldr id newBody0 sortedFunctions
sortImports _ = Nothing
11 changes: 0 additions & 11 deletions dhall/tests/lint/success/sortLetsA.dhall

This file was deleted.

11 changes: 0 additions & 11 deletions dhall/tests/lint/success/sortLetsB.dhall

This file was deleted.