mdiep / Logician

Logic programming in Swift
MIT License
194 stars 10 forks source link

Build up complex values #7

Closed mdiep closed 8 years ago

mdiep commented 8 years ago

You should be able to solve for something more than just a primitive type by solving based on its properties.

mdiep commented 8 years ago

8 should do this or mostly do this.

mdiep commented 8 years ago

I was wrong. bimap can only handle 1-to-1 correspondence since function lack identity (unless I wanted to add an identifier, which is lame). So bimap does what it does.

Adding support for tuples (#17) will get part way there. The rest should come by adding a protocol with an associated type that breaks a complex value down into its parts (as a tuple). Because of the associated type, the mapping has an identity, which should enable things like this:

struct Point: Equatable {
    var x: Int
    var y: Int

    static func ==(lhs: Point, rhs: Point) -> Bool {
        return lhs.x == rhs.x && lhs.y == rhs.y
    }
}

    func testSolveWithBimap() {
        let points = solve { (point: Variable<Point>) in
            let other = Variable<Point>()
            return point.x == 6
                && point == other
                && (other.y == 11 || point.y == 12)
        }
        XCTAssertEqual(points.allValues(), [ Point(x: 6, y: 11), Point(x: 6, y: 12) ])
    }