(* Generated by coq-of-rust *)
Require Import CoqOfRust.CoqOfRust.

(* StructTuple
  {
    name := "Sheep";
    const_params := [];
    ty_params := [];
    fields := [];
  } *)

(* StructTuple
  {
    name := "Cow";
    const_params := [];
    ty_params := [];
    fields := [];
  } *)

(* Trait *)
(* Empty module 'Animal' *)

Module Impl_returning_traits_with_dyn_Animal_for_returning_traits_with_dyn_Sheep.
  Definition Self : Ty.t := Ty.path "returning_traits_with_dyn::Sheep".
  
  (*
      fn noise(&self) -> &'static str {
          "baaaaah!"
      }
  *)
  Definition noise (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ self ] =>
      ltac:(M.monadic
        (let self := M.alloc (| self |) in
        M.read (| Value.String "baaaaah!" |)))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom Implements :
    M.IsTraitInstance
      "returning_traits_with_dyn::Animal"
      Self
      (* Trait polymorphic types *) []
      (* Instance *) [ ("noise", InstanceField.Method noise) ].
End Impl_returning_traits_with_dyn_Animal_for_returning_traits_with_dyn_Sheep.

Module Impl_returning_traits_with_dyn_Animal_for_returning_traits_with_dyn_Cow.
  Definition Self : Ty.t := Ty.path "returning_traits_with_dyn::Cow".
  
  (*
      fn noise(&self) -> &'static str {
          "moooooo!"
      }
  *)
  Definition noise (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ self ] =>
      ltac:(M.monadic
        (let self := M.alloc (| self |) in
        M.read (| Value.String "moooooo!" |)))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom Implements :
    M.IsTraitInstance
      "returning_traits_with_dyn::Animal"
      Self
      (* Trait polymorphic types *) []
      (* Instance *) [ ("noise", InstanceField.Method noise) ].
End Impl_returning_traits_with_dyn_Animal_for_returning_traits_with_dyn_Cow.

(*
fn random_animal(random_number: f64) -> Box<dyn Animal> {
    if random_number < 0.5 {
        Box::new(Sheep {})
    } else {
        Box::new(Cow {})
    }
}
*)
Definition random_animal (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [], [ random_number ] =>
    ltac:(M.monadic
      (let random_number := M.alloc (| random_number |) in
      M.read (|
        M.match_operator (|
          M.alloc (| Value.Tuple [] |),
          [
            fun γ =>
              ltac:(M.monadic
                (let γ :=
                  M.use
                    (M.alloc (|
                      BinOp.lt (| M.read (| random_number |), M.read (| UnsupportedLiteral |) |)
                    |)) in
                let _ := M.is_constant_or_break_match (| M.read (| γ |), Value.Bool true |) in
                M.alloc (|
                  M.call_closure (|
                    M.get_associated_function (|
                      Ty.apply
                        (Ty.path "alloc::boxed::Box")
                        []
                        [ Ty.path "returning_traits_with_dyn::Sheep"; Ty.path "alloc::alloc::Global"
                        ],
                      "new",
                      []
                    |),
                    [ Value.StructTuple "returning_traits_with_dyn::Sheep" [] ]
                  |)
                |)));
            fun γ =>
              ltac:(M.monadic
                (M.alloc (|
                  M.call_closure (|
                    M.get_associated_function (|
                      Ty.apply
                        (Ty.path "alloc::boxed::Box")
                        []
                        [ Ty.path "returning_traits_with_dyn::Cow"; Ty.path "alloc::alloc::Global"
                        ],
                      "new",
                      []
                    |),
                    [ Value.StructTuple "returning_traits_with_dyn::Cow" [] ]
                  |)
                |)))
          ]
        |)
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_random_animal :
  M.IsFunction "returning_traits_with_dyn::random_animal" random_animal.

(*
fn main() {
    let random_number = 0.234;
    let animal = random_animal(random_number);
    println!(
        "You've randomly chosen an animal, and it says {}",
        animal.noise()
    );
}
*)
Definition main (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [], [] =>
    ltac:(M.monadic
      (M.read (|
        let~ random_number := M.copy (| UnsupportedLiteral |) in
        let~ animal :=
          M.alloc (|
            M.call_closure (|
              M.get_function (| "returning_traits_with_dyn::random_animal", [] |),
              [ M.read (| random_number |) ]
            |)
          |) in
        let~ _ :=
          let~ _ :=
            M.alloc (|
              M.call_closure (|
                M.get_function (| "std::io::stdio::_print", [] |),
                [
                  M.call_closure (|
                    M.get_associated_function (| Ty.path "core::fmt::Arguments", "new_v1", [] |),
                    [
                      M.alloc (|
                        Value.Array
                          [
                            M.read (|
                              Value.String "You've randomly chosen an animal, and it says "
                            |);
                            M.read (| Value.String "
" |)
                          ]
                      |);
                      M.alloc (|
                        Value.Array
                          [
                            M.call_closure (|
                              M.get_associated_function (|
                                Ty.path "core::fmt::rt::Argument",
                                "new_display",
                                [ Ty.apply (Ty.path "&") [] [ Ty.path "str" ] ]
                              |),
                              [
                                M.alloc (|
                                  M.call_closure (|
                                    M.get_trait_method (|
                                      "returning_traits_with_dyn::Animal",
                                      Ty.dyn [ ("returning_traits_with_dyn::Animal::Trait", []) ],
                                      [],
                                      "noise",
                                      []
                                    |),
                                    [ M.read (| animal |) ]
                                  |)
                                |)
                              ]
                            |)
                          ]
                      |)
                    ]
                  |)
                ]
              |)
            |) in
          M.alloc (| Value.Tuple [] |) in
        M.alloc (| Value.Tuple [] |)
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_main : M.IsFunction "returning_traits_with_dyn::main" main.
