-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allows domains to implement an interface
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/resources/regressions/features/domains/implements-interface-fail01.gobra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Any copyright is dedicated to the Public Domain. | ||
// http://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
// this testcase checks that a ghost value of a type implementing an interface | ||
// cannot be passed to a non-ghost context | ||
|
||
package ImplementsInterfaceFail01 | ||
|
||
ghost type intPair domain { | ||
func fst(intPair) int | ||
func snd(intPair) int | ||
func pair(int, int) intPair | ||
|
||
axiom { | ||
forall p intPair :: {fst(p)}{snd(p)} p == pair(fst(p),snd(p)) | ||
} // pair | ||
|
||
axiom { | ||
forall l, r int :: {pair(l,r)} l == fst(pair(l,r)) && r == snd(pair(l,r)) | ||
} | ||
} | ||
|
||
// the following clause should be permitted by the type-checker: | ||
intPair implements any | ||
|
||
func test() { | ||
x := pair(1,2) | ||
//:: ExpectedOutput(type_error) | ||
assert equal(x, x) // fails as `equal` is actual | ||
} | ||
|
||
decreases | ||
requires isComparable(a) | ||
pure func equal(a, b any) bool { | ||
return a == b | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/resources/regressions/features/domains/implements-interface-simple01.gobra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Any copyright is dedicated to the Public Domain. | ||
// http://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
// this testcase checks whether a domain can implement an interface | ||
|
||
package ImplementsInterfaceSimple01 | ||
|
||
ghost type intPair domain { | ||
func fst(intPair) int | ||
func snd(intPair) int | ||
func pair(int, int) intPair | ||
|
||
axiom { | ||
forall p intPair :: {fst(p)}{snd(p)} p == pair(fst(p),snd(p)) | ||
} // pair | ||
|
||
axiom { | ||
forall l, r int :: {pair(l,r)} l == fst(pair(l,r)) && r == snd(pair(l,r)) | ||
} | ||
} | ||
|
||
// the following clause should be permitted by the type-checker: | ||
intPair implements any | ||
|
||
func test() { | ||
x := pair(1,2) | ||
assert equal(x, x) // succeeds as `equal` is ghost | ||
//:: ExpectedOutput(assert_error:assertion_error) | ||
assert false | ||
} | ||
|
||
ghost | ||
decreases | ||
requires isComparable(a) | ||
pure func equal(a, b any) bool { | ||
return a == b | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/resources/regressions/features/domains/implements-interface-simple02.gobra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Any copyright is dedicated to the Public Domain. | ||
// http://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
// this testcase checks whether a domain can implement a non-empty interface | ||
|
||
package ImplementsInterfaceSimple01 | ||
|
||
ghost type intPair domain { | ||
func fst(intPair) int | ||
func snd(intPair) int | ||
func pair(int, int) intPair | ||
|
||
axiom { | ||
forall p intPair :: {fst(p)}{snd(p)} p == pair(fst(p),snd(p)) | ||
} // pair | ||
|
||
axiom { | ||
forall l, r int :: {pair(l,r)} l == fst(pair(l,r)) && r == snd(pair(l,r)) | ||
} | ||
} | ||
|
||
type Equality interface { | ||
ghost | ||
pure hash() int | ||
|
||
ghost | ||
isEqual(other Equality) bool | ||
} | ||
|
||
// the following clause should be permitted by the type-checker as intPair implements the interface: | ||
intPair implements Equality | ||
|
||
ghost | ||
decreases | ||
pure func (i intPair) hash() int { | ||
return 42 // best implementation of a hash function | ||
} | ||
|
||
ghost | ||
decreases | ||
func (i intPair) isEqual(other Equality) bool { | ||
if typeOf(other) == intPair { | ||
return fst(i) == fst(other.(intPair)) && snd(i) == snd(other.(intPair)) | ||
} | ||
return false | ||
} | ||
|
||
func test() { | ||
x := pair(1,2) | ||
assert x.hash() == 42 | ||
//:: ExpectedOutput(assert_error:assertion_error) | ||
assert false | ||
} |