Skip to content

Add Hunk invariant static checks - #35

Open
ninioArtillero wants to merge 2 commits into
seereason:masterfrom
tweag:xg/context-checks
Open

Add Hunk invariant static checks#35
ninioArtillero wants to merge 2 commits into
seereason:masterfrom
tweag:xg/context-checks

Conversation

@ninioArtillero

@ninioArtillero ninioArtillero commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

This PR introduces Liquid Haskell static checks for the ContextSize to be a natural number and for the documented type Hunk c = [Diff [c]] invariant:

-- No two consecutive elements in a 'Hunk' are both applications
-- of 'First', 'Second', or 'Both', i.e. the list does not stutter
-- on 'Diff' constructors.

Without introducing any semantic change.

The Hunk invariant is encoded using a refinement type alias of the same name with a noStuttering predicate. Most notably, this is non-trivially checked in getContextDiffNumbered postcondition by specifying its local functions doPrefix and doSuffix to transform an input Hunk into an intermediate form {ds : [ValidListDiff c c] | noFFSS ds} that splitBothBoth transforms back into a Hunk. The input Hunk (actually a stronger variant which also specifies the value contents to be non-empty) is produced by getGroupedDiffBy.

The changes within getGroupedDiffBy are just reformating (to better accomodate the Liquid Haskell specifications) and the following renamings to improve readability:

go -> groupDiff
goFirsts -> leadingFirstss
goSeconds -> leading Seconds
goBoth -> leadingBoths

NOTE: Refinement type aliases--such as {-@ type Hunk c = { h : [ValidListDiff c c] | noStuttering h} @-} and {-@ type ContextDiff c = [Hunk c] @-}--override the corresponding type aliases--type Hunk c = [Diff [c]] and type Hunk c = [Diff [c] respectively--in Liquid Haskell specifications.

A new Data.Algorithm.Internal.Diff module is introduced to house PolyDiff and Diff definitions and their Liquid Haskell refinement type aliases, together with utility functions used for their definition and other helpers required in specifications. Its purpose is to avoid the cluthering of exported modules by functions intended only for Liquid Haskell specifications.

Comment thread src/Data/Algorithm/Diff.hs
Comment thread src/Internal/LiftedFunctions.hs Outdated
Comment thread src/Internal/Types.hs Outdated
@ninioArtillero
ninioArtillero force-pushed the xg/context-checks branch 3 times, most recently from 1abbed5 to 197736d Compare July 27, 2026 22:35
@ninioArtillero
ninioArtillero marked this pull request as ready for review July 27, 2026 22:35
@ninioArtillero
ninioArtillero force-pushed the xg/context-checks branch 2 times, most recently from 1dbd515 to e34c8e4 Compare July 28, 2026 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants