Commit 71f9b83a authored by Damien Morard's avatar Damien Morard
Browse files

Add ex1

parent ba902707
.DS_Store
/.build
/Packages
/*.xcodeproj
{
"object": {
"pins": [
{
"package": "LogicKit",
"repositoryURL": "https://github.com/kyouko-taiga/LogicKit",
"state": {
"branch": "master",
"revision": "32b7b2cf30d44468cbb290fefb17ca2f2a1c6d4d",
"version": null
}
}
]
},
"version": 1
}
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Exercice1",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/kyouko-taiga/LogicKit", .branch("master")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Exercice1",
dependencies: ["LogicKit"]),
.testTarget(
name: "Exercice1Tests",
dependencies: ["Exercice1"]),
]
)
# Exercice1
A description of this package.
import LogicKit
// Generator
let zero: Term = "zero"
// Free variable
let x: Term = .var("x")
let y: Term = .var("y")
let z: Term = .var("z")
let empty: Term = "empty"
let res: Term = .var("res")
let head: Term = .var("head")
let tail: Term = .var("tail")
let null: Term = "null"
// Easy way to write succ
func succ(_ x: Term) -> Term {
return .fact("succ", x)
}
// Convert an Int to its equivalent term
// For instance: n(3) -> succ(succ(succ(0)))
func n(_ x: Int) -> Term {
return x > 0
? succ(n(x - 1))
: zero
}
func cons(head: Term, tail: Term) -> Term {
return .fact("cons", head, tail)
}
let kb1: KnowledgeBase = [
.fact("zero", zero),
.fact("gt", succ(x), zero),
.rule("gt", succ(x), succ(y)) {
.fact("gt", x, y)
},
.fact("add", zero, y, y),
.rule("add", succ(x), y, z) {
.fact("add", x, succ(y), z)
},
.fact("empty", empty),
.fact("contains", cons(head: head, tail: tail), head),
.rule("contains", cons(head: head, tail: tail), x) {
.fact("contains", tail, x)
},
.fact("max", empty, null),
.fact("max", cons(head: head, tail: empty), head),
.rule("max", cons(head: head, tail: tail), x) {
.fact("max", tail, x) && .fact("gt", x, head)
},
.rule("max", cons(head: head, tail: tail), head) {
.fact("max", tail, x) && .fact("gt", head, x)
},
// .rule("max", cons(head: head, tail: tail), z) {
// .fact("max", tail, x) &&
// (.fact("gt", head, x) && z ~=~ head || .fact("gt", x, head) && z ~=~ x)
// },
.fact("gt", "5", "4"),
.fact("gt", "5", "2"),
.fact("gt", "4", "2"),
]
var p1 = kb1.ask(.fact("gt", n(2), n(1)))
var p2 = kb1.ask(.fact("gt", n(1), n(2)))
var p3 = kb1.ask(.fact("gt", n(0), n(0)))
print("Does n(2) is greather than n(1) ? ", p1.next() != nil)
print("Does n(1) is greather than n(2) ? ", p2.next() != nil)
print("Does n(0) is greather than n(0) ? ", p3.next() != nil)
var p4 = kb1.ask(.fact("add", n(3), n(2), z))
for binding in p4 {
print("Result of addition : ", binding["z"]!)
}
/* --------------------------------------------------------------------- */
//let list0: Term = cons(head: n(2), tail: empty)
let list1: Term = cons(head: n(2), tail: cons(head: n(3), tail: cons(head: n(1), tail: empty)))
//
//var r1 = kb1.ask(.fact("contains", list1, n(4))/*, logger: DefaultLogger(useFontAttributes: false)*/)
//
//print("Does n(4) in list ? ", r1.next() != nil)
var r2 = kb1.ask(.fact("max", list1, res), logger: DefaultLogger(useFontAttributes: true))
for binding in r2 {
print(binding["res"]!)
break
}
//var r3 = kb1.ask(.fact("max", list0, res))
//
//for binding in r3 {
// print(binding["res"]!)
// break
//}
import XCTest
import class Foundation.Bundle
final class Exercice1Tests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
// Some of the APIs that we use below are available in macOS 10.13 and above.
guard #available(macOS 10.13, *) else {
return
}
let fooBinary = productsDirectory.appendingPathComponent("Exercice1")
let process = Process()
process.executableURL = fooBinary
let pipe = Pipe()
process.standardOutput = pipe
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)
XCTAssertEqual(output, "Hello, world!\n")
}
/// Returns path to the built products directory.
var productsDirectory: URL {
#if os(macOS)
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return bundle.bundleURL.deletingLastPathComponent()
}
fatalError("couldn't find the products directory")
#else
return Bundle.main.bundleURL
#endif
}
static var allTests = [
("testExample", testExample),
]
}
import XCTest
#if !os(macOS)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(Exercice1Tests.allTests),
]
}
#endif
\ No newline at end of file
import XCTest
import Exercice1Tests
var tests = [XCTestCaseEntry]()
tests += Exercice1Tests.allTests()
XCTMain(tests)
\ No newline at end of file
# Exercices / TPs sémantique
Bonjour à tous et bienvenu dans la partie des exercices et TPs du cours de sémantique.
# Commençons par le commencement
Vous n'avez sûrement jamais utilisé Gitlab et ce n'est pas grave.
Vous devez normalement déjà être un peu familier avec Github et les bases restent similaires.
Je vais vous lister ici la liste
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment