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

(* Trait *)
(* Empty module 'HasArea' *)

(* StructRecord
  {
    name := "Rectangle";
    const_params := [];
    ty_params := [];
    fields := [ ("length", Ty.path "f64"); ("height", Ty.path "f64") ];
  } *)

Module Impl_core_fmt_Debug_for_generics_bounds_Rectangle.
  Definition Self : Ty.t := Ty.path "generics_bounds::Rectangle".
  
  (* Debug *)
  Definition fmt (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ self; f ] =>
      ltac:(M.monadic
        (let self := M.alloc (| self |) in
        let f := M.alloc (| f |) in
        M.call_closure (|
          M.get_associated_function (|
            Ty.path "core::fmt::Formatter",
            "debug_struct_field2_finish",
            []
          |),
          [
            M.read (| f |);
            M.read (| Value.String "Rectangle" |);
            M.read (| Value.String "length" |);
            M.SubPointer.get_struct_record_field (|
              M.read (| self |),
              "generics_bounds::Rectangle",
              "length"
            |);
            M.read (| Value.String "height" |);
            M.alloc (|
              M.SubPointer.get_struct_record_field (|
                M.read (| self |),
                "generics_bounds::Rectangle",
                "height"
              |)
            |)
          ]
        |)))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom Implements :
    M.IsTraitInstance
      "core::fmt::Debug"
      Self
      (* Trait polymorphic types *) []
      (* Instance *) [ ("fmt", InstanceField.Method fmt) ].
End Impl_core_fmt_Debug_for_generics_bounds_Rectangle.

(* StructRecord
  {
    name := "Triangle";
    const_params := [];
    ty_params := [];
    fields := [ ("length", Ty.path "f64"); ("height", Ty.path "f64") ];
  } *)

Module Impl_generics_bounds_HasArea_for_generics_bounds_Rectangle.
  Definition Self : Ty.t := Ty.path "generics_bounds::Rectangle".
  
  (*
      fn area(&self) -> f64 {
          self.length * self.height
      }
  *)
  Definition area (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ self ] =>
      ltac:(M.monadic
        (let self := M.alloc (| self |) in
        BinOp.Wrap.mul (|
          M.read (|
            M.SubPointer.get_struct_record_field (|
              M.read (| self |),
              "generics_bounds::Rectangle",
              "length"
            |)
          |),
          M.read (|
            M.SubPointer.get_struct_record_field (|
              M.read (| self |),
              "generics_bounds::Rectangle",
              "height"
            |)
          |)
        |)))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom Implements :
    M.IsTraitInstance
      "generics_bounds::HasArea"
      Self
      (* Trait polymorphic types *) []
      (* Instance *) [ ("area", InstanceField.Method area) ].
End Impl_generics_bounds_HasArea_for_generics_bounds_Rectangle.

(*
fn print_debug<T: Debug>(t: &T) {
    println!("{:?}", t);
}
*)
Definition print_debug (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [ T ], [ t ] =>
    ltac:(M.monadic
      (let t := M.alloc (| t |) in
      M.read (|
        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 "" |); M.read (| Value.String "
" |) ]
                      |);
                      M.alloc (|
                        Value.Array
                          [
                            M.call_closure (|
                              M.get_associated_function (|
                                Ty.path "core::fmt::rt::Argument",
                                "new_debug",
                                [ Ty.apply (Ty.path "&") [] [ T ] ]
                              |),
                              [ t ]
                            |)
                          ]
                      |)
                    ]
                  |)
                ]
              |)
            |) in
          M.alloc (| Value.Tuple [] |) in
        M.alloc (| Value.Tuple [] |)
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_print_debug : M.IsFunction "generics_bounds::print_debug" print_debug.

(*
fn area<T: HasArea>(t: &T) -> f64 {
    t.area()
}
*)
Definition area (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [ T ], [ t ] =>
    ltac:(M.monadic
      (let t := M.alloc (| t |) in
      M.call_closure (|
        M.get_trait_method (| "generics_bounds::HasArea", T, [], "area", [] |),
        [ M.read (| t |) ]
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_area : M.IsFunction "generics_bounds::area" area.

(*
fn main() {
    let rectangle = Rectangle {
        length: 3.0,
        height: 4.0,
    };
    let _triangle = Triangle {
        length: 3.0,
        height: 4.0,
    };

    print_debug(&rectangle);
    println!("Area: {}", rectangle.area());

    //print_debug(&_triangle);
    //println!("Area: {}", _triangle.area());
    // ^ TODO: Try uncommenting these.
    // | Error: Does not implement either `Debug` or `HasArea`.
}
*)
Definition main (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [], [] =>
    ltac:(M.monadic
      (M.read (|
        let~ rectangle :=
          M.alloc (|
            Value.StructRecord
              "generics_bounds::Rectangle"
              [
                ("length", M.read (| UnsupportedLiteral |));
                ("height", M.read (| UnsupportedLiteral |))
              ]
          |) in
        let~ _triangle :=
          M.alloc (|
            Value.StructRecord
              "generics_bounds::Triangle"
              [
                ("length", M.read (| UnsupportedLiteral |));
                ("height", M.read (| UnsupportedLiteral |))
              ]
          |) in
        let~ _ :=
          M.alloc (|
            M.call_closure (|
              M.get_function (|
                "generics_bounds::print_debug",
                [ Ty.path "generics_bounds::Rectangle" ]
              |),
              [ rectangle ]
            |)
          |) 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 "Area: " |); M.read (| Value.String "
" |) ]
                      |);
                      M.alloc (|
                        Value.Array
                          [
                            M.call_closure (|
                              M.get_associated_function (|
                                Ty.path "core::fmt::rt::Argument",
                                "new_display",
                                [ Ty.path "f64" ]
                              |),
                              [
                                M.alloc (|
                                  M.call_closure (|
                                    M.get_trait_method (|
                                      "generics_bounds::HasArea",
                                      Ty.path "generics_bounds::Rectangle",
                                      [],
                                      "area",
                                      []
                                    |),
                                    [ rectangle ]
                                  |)
                                |)
                              ]
                            |)
                          ]
                      |)
                    ]
                  |)
                ]
              |)
            |) in
          M.alloc (| Value.Tuple [] |) in
        M.alloc (| Value.Tuple [] |)
      |)))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

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