Debian Codegraph @

src/Data/List/Relation/Unary/Sufficient.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- 'Sufficient' lists: a structurally inductive view of lists xs
5 -- as given by xs ≡ non-empty prefix + sufficient suffix
6 --
7 -- Useful for termination arguments for function definitions
8 -- which provably consume a non-empty (but otherwise arbitrary) prefix
9 -- *without* having to resort to ancillary WF induction on length etc.
10 -- e.g. lexers, parsers etc.
11 --
12 -- Credited by Conor McBride as originally due to James McKinna
13 ------------------------------------------------------------------------
14
15 {-# OPTIONS --cubical-compatible --safe #-}
16
17 module Data.List.Relation.Unary.Sufficient where
18
19 open import Level using (Level; _⊔_)
20 open import Data.List.Base using (List; []; _∷_; [_]; _++_)
21 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
22
23 private
24 variable
25
26 a b : Level
27 A : Set a
28 x : A
29 xs : List A
30
31 ------------------------------------------------------------------------
32 -- Sufficient builder
33
34 suffAcc : {A : Set a} (B : List A → Set b) (xs : List A) → Set (a ⊔ b)
35 suffAcc B xs = ∀ {x} {prefix} suffix → xs ≡ x ∷ prefix ++ suffix → B suffix
36
37 ------------------------------------------------------------------------
38 -- Sufficient view
39
40 data Sufficient {A : Set a} : (xs : List A) → Set a where
41
42 acc : ∀ {xs} (ih : suffAcc Sufficient xs) → Sufficient xs
43
44
45 ------------------------------------------------------------------------
46 -- Sufficient properties
47
48 -- constructors
49
50 module Constructors where
51
52 []⁺ : Sufficient {A = A} []
53 []⁺ = acc λ _ ()
54
55 ∷⁺ : Sufficient xs → Sufficient (x ∷ xs)
56 ∷⁺ {xs = xs} suffices@(acc hyp) = acc λ { _ refl → suf _ refl }
57 where
58 suf : ∀ prefix {suffix} → xs ≡ prefix ++ suffix → Sufficient suffix
59 suf [] refl = suffices
60 suf (_ ∷ _) {suffix} eq = hyp suffix eq
61
62 -- destructors
63
64 module Destructors where
65
66 acc-inverse : ∀ ys → Sufficient (x ∷ xs ++ ys) → Sufficient ys
67 acc-inverse ys (acc hyp) = hyp ys refl
68
69 ++⁻ : ∀ xs {ys : List A} → Sufficient (xs ++ ys) → Sufficient ys
70 ++⁻ [] suffices = suffices
71 ++⁻ (x ∷ xs) {ys} suffices = acc-inverse ys suffices
72
73 ∷⁻ : Sufficient (x ∷ xs) → Sufficient xs
74 ∷⁻ {x = x} = ++⁻ [ x ]
75
76
77 ------------------------------------------------------------------------
78 -- Sufficient view covering property
79
80 module View where
81
82 open Constructors
83
84 sufficient : (xs : List A) → Sufficient xs
85 sufficient [] = []⁺
86 sufficient (x ∷ xs) = ∷⁺ (sufficient xs)
87
88 ------------------------------------------------------------------------
89 -- Recursion on the sufficient view
90
91 module _ (B : List A → Set b) (rec : ∀ ys → (ih : suffAcc B ys) → B ys)
92 where
93
94 open View
95
96 suffRec′ : ∀ {zs} → Sufficient zs → B zs
97 suffRec′ {zs} (acc hyp) = rec zs (λ xs eq → suffRec′ (hyp xs eq))
98
99 suffRec : ∀ zs → B zs
100 suffRec zs = suffRec′ (sufficient zs)