-
-
Notifications
You must be signed in to change notification settings - Fork 38
Experimental example translation for CI test #535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
InfiniteEchoes
wants to merge
34
commits into
main
Choose a base branch
from
gy@ExpSnippet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
4aa17bf
Experimental translation for CI test
InfiniteEchoes fefd563
Add traits.v
InfiniteEchoes 895a12e
Update `traits.v`
InfiniteEchoes 377ef39
Update `traits.v`
InfiniteEchoes bb472da
Update `traits.v`: Finished function definitions
InfiniteEchoes bc13690
`traits.v`: Remove comments
InfiniteEchoes 287f430
Minor fixes
InfiniteEchoes 5c83ccd
Update `traits.v`
InfiniteEchoes 6ed6eee
Update `traits.v`
InfiniteEchoes 8a94f10
Update `traits.v` according to comments
InfiniteEchoes 5cad995
Update `constants.v` according to comment
InfiniteEchoes 81fc18c
More minor fixes
InfiniteEchoes 83ce23f
More fixes according to comments
InfiniteEchoes f443776
Compiler-checked `constants.v`
InfiniteEchoes 555eac5
Compiler-passed `traits.v` with unfixed bug
InfiniteEchoes dcfe964
Fixed all bugs of `traits.v`
InfiniteEchoes dcfaf13
Add initial `erc.1155.v`
InfiniteEchoes 309606b
Update erc1155
InfiniteEchoes 33cf84c
More updates on `erc1155.v`
InfiniteEchoes 89ccad6
Update `traits.v`
InfiniteEchoes 09fb455
Finished defining `traits.v` for simulation(?)
InfiniteEchoes fdff43f
remove comments
InfiniteEchoes 419d9c7
minor fixes
InfiniteEchoes 9aa87c4
format fix
InfiniteEchoes e7c562f
draft update
InfiniteEchoes 344a47f
Merge branch 'main' into gy@ExpSnippet
InfiniteEchoes 40c77d7
snapshot
InfiniteEchoes ab0ba4a
Update `traits.v`
InfiniteEchoes c32c5fb
Fix `traits.v`
InfiniteEchoes 57b1481
Update `constants.v`
InfiniteEchoes 315d42b
Update `erc1155.v`
InfiniteEchoes f7c44b6
Merge branch 'main' into gy@ExpSnippet
InfiniteEchoes 29671f5
Fix file location after merging main
InfiniteEchoes 9897d26
Update according to main & delete redundant deps
InfiniteEchoes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
CoqOfRust/examples/default/examples/ink_contracts/simulations/constants.v
This file contains hidden or 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 @@ | ||
| (* custom_type/constants.v *) | ||
| Require Import CoqOfRust.CoqOfRust. | ||
| Require CoqOfRust.core.simulations.default. | ||
| Require Import CoqOfRust.core.simulations.option. | ||
| Require Import CoqOfRust.core.simulations.integer. | ||
| Require Import CoqOfRust.core.simulations.bool. | ||
| Require CoqOfRust.examples.default.examples.ink_contracts.simulations.lib. | ||
| Require Import CoqOfRust.simulations.M. | ||
|
|
||
| (* | ||
| static LANGUAGE: &str = "Rust"; | ||
| const THRESHOLD: i32 = 10; | ||
| *) | ||
|
|
||
| Definition LANGUAGE := mk_str "Rust". | ||
| Definition THRESHOLD := i32.Make 10. | ||
|
|
||
| (* | ||
| fn is_big(n: i32) -> bool { | ||
| // Access constant in some function | ||
| n > THRESHOLD | ||
| } | ||
| *) | ||
|
|
||
| Definition is_big | ||
| (n: Nat) : bool := n >? THRESHOLD. | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| (* | ||
| fn main() { | ||
| let n = 16; | ||
| } *) | ||
|
|
||
| Definition main : Unit := | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let n := i32.Make 16 in | ||
| (* pure Value.DeclaredButUndefined *) | ||
| (). | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
157 changes: 157 additions & 0 deletions
157
CoqOfRust/examples/default/examples/ink_contracts/simulations/traits.v
This file contains hidden or 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,157 @@ | ||
| (* traits/traits.rs *) | ||
|
|
||
| (* TODO: | ||
| 1. Check (if it's necessary) how to implement `TraitHasRun` for `Animal` and `Sheep` | ||
| 2. Check if the `main` function has defined correctly | ||
| *) | ||
|
|
||
| (* struct Sheep { | ||
| naked: bool, | ||
| name: &'static str, | ||
| } *) | ||
| Module Sheep. | ||
| Record t : Set := { | ||
| naked: bool, | ||
| name: string, | ||
| }. | ||
|
|
||
| Global Instance IsToValue : ToValue t := { | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Φ := Ty.path "traits::Sheep"; | ||
| φ x := | ||
| Value.StructRecord "traits::Sheep" [ | ||
| ("naked", φ x.(naked)); | ||
| ("name", φ x.(name)); | ||
| ]; | ||
| }. | ||
| End Sheep. | ||
|
|
||
| (* ** Simulation of functions ** *) | ||
| (* | ||
| fn new(name: &'static str) -> Sheep { | ||
| Sheep { | ||
| name: name, | ||
| naked: false, | ||
| } | ||
| } *) | ||
| (* NOTE: Is this the correct way to construct record in Coq? *) | ||
| Definition new (name: string) : traits.Sheep.t := | ||
| traits.Animal {| naked := false; | ||
| name := name; | ||
| |}. | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| (* | ||
| fn name(&self) -> &'static str { | ||
| self.name | ||
| } | ||
| *) | ||
| Definition name (self: traits.Sheep.t) : string := | ||
| self.name. | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| (* | ||
| fn noise(&self) -> &'static str { | ||
| if self.is_naked() { | ||
| "baaaaah?" | ||
| } else { | ||
| "baaaaah!" | ||
| } | ||
| } *) | ||
| Definition noise (self: traits.Sheep.t) : string := | ||
| if is_naked(self) then "baaaaah?" else "baaaaah!". | ||
|
|
||
| (* NOTE: unimplemented since it involves println *) | ||
| (* fn talk(&self) { | ||
| // For example, we can add some quiet contemplation. | ||
| println!("{} pauses briefly... {}", self.name, self.noise()); | ||
| } *) | ||
| Definition talk (self: traits.Sheep.t): unit := tt. | ||
|
|
||
| (* | ||
| impl Sheep { | ||
| fn is_naked(&self) -> bool { | ||
| self.naked | ||
| } | ||
| } | ||
| *) | ||
| Definition is_naked (self: traits.Sheep.t) : bool := | ||
| self.naked. | ||
|
|
||
| (* Simulation of a function that modifies a variable *) | ||
|
|
||
| Module State. | ||
| Definition t : Set := traits.Sheep.t. | ||
| End State. | ||
|
|
||
| (* | ||
| impl Sheep { | ||
| fn shear(&mut self) { | ||
| if self.is_naked() { | ||
| // Implementor methods can use the implementor's trait methods. | ||
| println!("{} is already naked...", self.name()); | ||
| } else { | ||
| println!("{} gets a haircut!", self.name); | ||
|
|
||
| self.naked = true; | ||
| } | ||
| } | ||
| } | ||
| *) | ||
| Definition shear (self: traits.Sheep.t) : MS? unit := | ||
| letS? '(storage) := readS? in | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if is_naked(self) then tt else | ||
| letS? _ = writeS? ( | ||
| storage <| traits.Animal.naked := true |>, | ||
| ) | ||
| in | ||
| returnS? tt. | ||
|
|
||
| (* | ||
| trait Animal { | ||
| // Associated function signature; `Self` refers to the implementor type. | ||
| fn new(name: &'static str) -> Self; | ||
|
|
||
| // Method signatures; these will return a string. | ||
| fn name(&self) -> &'static str; | ||
| fn noise(&self) -> &'static str; | ||
|
|
||
| // Traits can provide default method definitions. | ||
| fn talk(&self) { | ||
| println!("{} says {}", self.name(), self.noise()); | ||
| } | ||
| } | ||
| *) | ||
|
|
||
| Module Animal. | ||
| Class Trait (Self : Set) : Set := { | ||
| new (name: string) : traits.Sheep.t; | ||
| name (self: traits.Sheep.t) : string; | ||
| noise (self: traits.Sheep.t) : string; | ||
| talk (self: traits.Sheep.t) : unit; | ||
| }. | ||
|
|
||
| (* TODO: Define the `TraitHasRun` struct to express that `Sheep` implements `Animal` *) | ||
|
|
||
| End Animal. | ||
|
|
||
| (* | ||
| fn main() { | ||
| // Type annotation is necessary in this case. | ||
| let mut dolly: Sheep = Animal::new("Dolly"); | ||
| // TODO ^ Try removing the type annotations. | ||
|
|
||
| dolly.talk(); | ||
| dolly.shear(); | ||
| dolly.talk(); | ||
| } *) | ||
|
|
||
| Definition main : | ||
| MS? State.t unit := | ||
| let dolly := new("Dolly") in | ||
| let _ = talk(dolly) in | ||
| let _ = shear(dolly) in | ||
| let _ = talk(dolly) in | ||
InfiniteEchoes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (* NOTE: Is the following notation still in use? *) | ||
| (* let dolly := traits.Animal::["new"] "Dolly" in | ||
| let _ := dolly::["talk"] in | ||
| let _ := dolly::["shear"] in | ||
| let _ := dolly::["talk"] in *) | ||
| returnS? tt. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.