def invariant

bend-lint-rules@0.2.0.0 · performance/loop_invariant_call.bend:141

import bend-lint-rules@0.2.0.0/performance/loop_invariant_call.bend as LoopInvariantCall
import Base
import bend-lint-api@0.2.0.0/linter/lint.bend as Lint

141# Whether `nodes`, the arguments of a call, are the same on every step: literals, the variables of `fixed`, and calls,
142# annotations or constructions of those alone; `fuel` bounds the depth.
143def invariant(+fuel: Nat, nodes: List<&2, Lint.Node>, +fixed: List<&2, U32>, ok: Bool) -> Bool:
144  match fuel nodes ok:
145    case _ _ False{}:
146      False{}
147    case _ Nil{} True{}:
148      True{}
149    case 0n _ True{}:
150      False{}
151    case 1n+f Con{Lint.Node{Lint.Literal{t}, n, s, k, fs}, rest} True{}:
152      invariant(f, rest, fixed, True{})
153    case 1n+f Con{Lint.Node{Lint.Variable{v}, n, s, k, fs}, rest} True{}:
154      invariant(f, rest, fixed, List.contains(~U32, ~U32.is_eq, fixed, v))
155    case 1n+f Con{Lint.Node{Lint.Application{}, n, s, Con{h, args}, fs}, rest} True{}:
156      invariant(f, rest, fixed, invariant(f, args, fixed, True{}))
157    case 1n+f Con{Lint.Node{Lint.Annotation{}, n, s, Con{x, ts}, fs}, rest} True{}:
158      invariant(f, rest, fixed, invariant(f, [x], fixed, True{}))
159    case 1n+f Con{Lint.Node{Lint.Construction{t}, n, s, k, fs}, rest} True{}:
160      invariant(f, rest, fixed, invariant(f, k, fixed, True{}))
161    case 1n+f Con{x, rest} True{}:
162      False{}

Uses (8)

Used by (1)