L4 Partition
Copyright 2026 Chris Liu, Dustin Tucker, James B. Wilson. All rights reserved. Not permitted for AI training nor public posting.
Scenario
You've used a Machine Learning ML algorithm to train a function classify that takes audio recordings of diesel engines and returns a code that identifies 457 engine failure categories.
Your present classify to your mechanics and they inform you that they only fix or replace 25 major components, even if the problem is something smaller inside one of the parts. The mechanics have agreed to answer which of the 25 major components goes with which of the 457 failures.
Scenario
Machine Learning ML algorithm classify identifies 457 engine failure categories.
Mechanics only fix or replace 25 major components, mechanics can tell uswhich of the 25 major components goes with which of the 457 failures.
Problem
You need to cut-down the classifier function to match the mechanic roles.
Discuss how to approach a solution.
Pros/Cons
After discussion everyone agrees the right option is to place some new code between the classify output and the mechanics use. But your engineering team is split on possible options.
- Redefine equality between classifications so that two error codes for the same part are now equal.
- Make for each of the 25 major components, make a list of which of the 457 failures would be fixed by replacing that part.
- Make a function
componentthat composes withclassifyand outputs the correct component.
Pros/Cons
- Redefine equality between classifications: two error codes for the same part are now equal.
- Pass around lists of which of the 457 errors go in each of the 25 parts.
- Make a function
componentthat composes withclassifyand outputs the correct component.
-
Discuss the pros/cons of each strategy.
Equality method
Eugene the engineer didn't wait. Here's his prototype in Swift.
struct FailureCode: Equatable {
let value: Int
init?(_ value: Int) {
guard (1...457).contains(value) else { return nil }
self.value = value
}
func printFailure() {
print("Failure Code: \(self.value)")
}
static func == (lhs: FailureCode, rhs: FailureCode) -> Bool {
// TODO: replace with mechanic codes
if (lhs.value < 250 && rhs.value < 250)
|| (lhs.value >= 250 && rhs.value >= 250) {
return true
}
return false
}
}
func classify<AudioInput>(audioFile: AudioInput) -> FailureCode {
// ML algorithm processes audio file and returns failure code (1-457)
// Placeholder implementation
return FailureCode(Int.random(in: 1...457))!
}
3 Diagnose if this is a problem and explain how it happened.
Text
Cool ideas from Swift
When we make partial functions (e.g. left inverses) we need to accept that some inputs have no outputs. Programs have many options include Option[A], Maybe a, throws IllegalArgumentException, but Swift makes it very natural.
-
func doSomething(input) -> Output?Here?means the output may fail. - add
!when you can prove the output is always there, no errors. -
guardlets you take an input likeIntwhich takes a huge range of values and insists they fit a restricted range. Avoids errors later. - tagged inputs: swift want you to keep the name that functions take as input as an annotation to the actual input. This helps recall what will happen to the input
Swift
https://swiftfiddle.com/
Equality method
Eugene the engineer didn't wait. Here's his prototype in Swift.
You notice equality can be true or false, but even when true you get different behavior from x and y.
let noise1 = "noise1"
let noise2 = "noise2"
let x = classify(audioFile: noise1)
let y = classify(audioFile: noise2)
print("1> x = classify(noise1)")
print("2> y = classify(noise2)")
print("3> x == y")
print(x == y) // True about half the time
print("4> x.printFailure()")
x.printFailure()
print("5> y.printFailure()")
y.printFailure() // Not generally the same as x!
3 Diagnose if this is a problem and explain how it happened.
Text
Cool ideas from Swift
When we make partial functions (e.g. left inverses) we need to accept that some inputs have no outputs. Programs have many options include Option[A], Maybe a, throws IllegalArgumentException, but Swift makes it very natural.
-
func doSomething(input) -> Output?Here?means the output may fail. - add
!when you can prove the output is always there, no errors. -
guardlets you take an input likeIntwhich takes a huge range of values and insists they fit a restricted range. Avoids errors later. - tagged inputs: swift want you to keep the name that functions take as input as an annotation to the actual input. This helps recall what will happen to the input
Partition method
After this equality shortcomings, Eugene the Engineer prototypes parts.
struct FailureCode: Equatable {
let value: Int
// TODO get actual partition
static let part1: ClosedRange<Int> = 1...10
static let part2: ClosedRange<Int> = 11...457
init?(_ value: Int) {
guard (1...457).contains(value) else { return nil }
self.value = value
}
func printFailure() {
if FailureCode.part1.contains(value) {
print("part 1")
} else {
print("part 2")
}
}
static func == (lhs: FailureCode, rhs: FailureCode) -> Bool {
let lhsInPart1 = FailureCode.part1.contains(lhs.value)
let rhsInPart1 = FailureCode.part1.contains(rhs.value)
let lhsInPart2 = FailureCode.part2.contains(lhs.value)
let rhsInPart2 = FailureCode.part2.contains(rhs.value)
return (lhsInPart1 && rhsInPart1) || (lhsInPart2 && rhsInPart2)
}
}
- Predict what will be necessary to continue in this way when there are 25 parts.
Partition method
After this equality shortcomings, Eugene the Engineer prototypes parts.
- Predict what will be necessary to continue in this way when there are 25 parts.
Function method.
Poor Eugene the Engineer is asked to start over with the function method.
- Design a solution that uses a surjective function and solves the problems Eugene has faced so far.
struct FailureCode: Equatable {
let value: Int
init?(_ value: Int) {
guard (1...457).contains(value) else { return nil }
self.value = value
}
}
struct MechanicCode: Equatable {
let value: String
init?(_ value: String) {
guard !value.isEmpty else { return nil }
self.value = value
}
}
func classify<AudioInput>(audioFile: AudioInput) -> FailureCode {
// ML algorithm processes audio file and returns failure code (1-457)
// Placeholder implementation
return FailureCode(Int.random(in: 1...457))!
}
func reclassify(rawCode :FailureCode) -> MechanicCode {
if rawCode.value < 250 {
return MechanicCode("part 1")!
} else {
return MechanicCode("part 2")!
}
}
let noise1 = "noise1"
let noise2 = "noise2"
let x = classify(audioFile: noise1)
let y = classify(audioFile: noise2)
let x_mechanic = reclassify(rawCode: x)
let y_mechanic = reclassify(rawCode: y)
print(x_mechanic == y_mechanic)
print(x_mechanic)
print(y_mechanic)
L4 Partition
By James Wilson
L4 Partition
A study of equality, partitions, and function resonance.
- 34