WeixiangYan/CodeTransOcean huggingface dataset:
Task: Monty Hall problem


C++: #include <iostream>
#include <cstdlib>
#include <ctime>
 
int randint(int n)
{
  return (1.0*n*std::rand())/(1.0+RAND_MAX);
}

int other(int doorA, int doorB)
{
  int doorC;
  if (doorA == doorB)
  {
    doorC = randint(2);
    if (doorC >= doorA)
      ++doorC;
  }
  else
  {
    for (doorC = 0; doorC == doorA || doorC == doorB; ++doorC)
    {
      
    }
  }
  return doorC;
}

int check(int games, bool change)
{
  int win_count = 0;
  for (int game = 0; game < games; ++game)
  {
    int const winning_door = randint(3);
    int const original_choice = randint(3);
    int open_door = other(original_choice, winning_door);

    int const selected_door = change?
                                other(open_door, original_choice)
                              : original_choice;

    if (selected_door == winning_door)
      ++win_count;
  }
 
  return win_count;
}
 
int main()
{
  std::srand(std::time(0));

  int games = 10000;
  int wins_stay = check(games, false);
  int wins_change = check(games, true);
  std::cout << "staying: " << 100.0*wins_stay/games << "%, changing: " << 100.0*wins_change/games << "%\n";
}









Fortran: PROGRAM MONTYHALL
                            
  IMPLICIT NONE  

  INTEGER, PARAMETER :: trials = 10000
  INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
  LOGICAL :: door(3)
  REAL :: rnum

  CALL RANDOM_SEED
  DO i = 1, trials
     door = .FALSE.
     CALL RANDOM_NUMBER(rnum)
     prize = INT(3*rnum) + 1
     door(prize) = .TRUE.              
    
     CALL RANDOM_NUMBER(rnum)   
     choice = INT(3*rnum) + 1          

     DO
        CALL RANDOM_NUMBER(rnum)   
        show = INT(3*rnum) + 1 
        IF (show /= choice .AND. show /= prize) EXIT       
     END DO

     SELECT CASE(choice+show)          
       CASE(3)
          remaining = 3
       CASE(4)
          remaining = 2
       CASE(5)
          remaining = 1
     END SELECT

     IF (door(choice)) THEN           
        staycount = staycount + 1
     ELSE IF (door(remaining)) THEN   
        switchcount = switchcount + 1
     END IF
    
  END DO

  WRITE(*, "(A,F6.2,A)") "Chance of winning by not switching is", real(staycount)/trials*100, "%"
  WRITE(*, "(A,F6.2,A)") "Chance of winning by switching is", real(switchcount)/trials*100, "%" 

END PROGRAM MONTYHALL
Task: Monty Hall problem

C: 

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#define NumSim 1000000000 

void main() {
      unsigned long int i,stay=0;
      int ChosenDoor,WinningDoor;
      bool door[3]={0,0,0}; 
      
      srand(time(NULL));  
	 
	  for(i=0;i<=NumSim;i++){
	  
	      WinningDoor=rand() % 3; 
	  
	      ChosenDoor=rand() % 3;  
	  
	      if(door[WinningDoor]=true,door[ChosenDoor])stay++;
	  
	      door[WinningDoor]=false;
	  
      } 
	  
     printf("\nAfter %lu games, I won %u by staying.  That is %f%%. and I won by switching %lu That is %f%%",NumSim, stay, (float)stay*100.0/(float)i,abs(NumSim-stay),100-(float)stay*100.0/(float)i);
	
  }










Fortran: PROGRAM MONTYHALL
                            
  IMPLICIT NONE  

  INTEGER, PARAMETER :: trials = 10000
  INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
  LOGICAL :: door(3)
  REAL :: rnum

  CALL RANDOM_SEED
  DO i = 1, trials
     door = .FALSE.
     CALL RANDOM_NUMBER(rnum)
     prize = INT(3*rnum) + 1
     door(prize) = .TRUE.              
    
     CALL RANDOM_NUMBER(rnum)   
     choice = INT(3*rnum) + 1          

     DO
        CALL RANDOM_NUMBER(rnum)   
        show = INT(3*rnum) + 1 
        IF (show /= choice .AND. show /= prize) EXIT       
     END DO

     SELECT CASE(choice+show)          
       CASE(3)
          remaining = 3
       CASE(4)
          remaining = 2
       CASE(5)
          remaining = 1
     END SELECT

     IF (door(choice)) THEN           
        staycount = staycount + 1
     ELSE IF (door(remaining)) THEN   
        switchcount = switchcount + 1
     END IF
    
  END DO

  WRITE(*, "(A,F6.2,A)") "Chance of winning by not switching is", real(staycount)/trials*100, "%"
  WRITE(*, "(A,F6.2,A)") "Chance of winning by switching is", real(switchcount)/trials*100, "%" 

END PROGRAM MONTYHALL
Task: Monty Hall problem




Go: package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	games := 100000
	r := rand.New(rand.NewSource(time.Now().UnixNano()))

	var switcherWins, keeperWins, shown int
	for i := 0; i < games; i++ {
		doors := []int{0, 0, 0}
		doors[r.Intn(3)] = 1 
		choice := r.Intn(3) 
		for shown = r.Intn(3); shown == choice || doors[shown] == 1; shown = r.Intn(3) {}
		switcherWins += doors[3 - choice - shown]
		keeperWins += doors[choice]
	}
	floatGames := float32(games)
	fmt.Printf("Switcher Wins: %d (%3.2f%%)\n",
		switcherWins, (float32(switcherWins) / floatGames * 100))
	fmt.Printf("Keeper Wins: %d (%3.2f%%)",
		keeperWins, (float32(keeperWins) / floatGames * 100))
}







Fortran: PROGRAM MONTYHALL
                            
  IMPLICIT NONE  

  INTEGER, PARAMETER :: trials = 10000
  INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
  LOGICAL :: door(3)
  REAL :: rnum

  CALL RANDOM_SEED
  DO i = 1, trials
     door = .FALSE.
     CALL RANDOM_NUMBER(rnum)
     prize = INT(3*rnum) + 1
     door(prize) = .TRUE.              
    
     CALL RANDOM_NUMBER(rnum)   
     choice = INT(3*rnum) + 1          

     DO
        CALL RANDOM_NUMBER(rnum)   
        show = INT(3*rnum) + 1 
        IF (show /= choice .AND. show /= prize) EXIT       
     END DO

     SELECT CASE(choice+show)          
       CASE(3)
          remaining = 3
       CASE(4)
          remaining = 2
       CASE(5)
          remaining = 1
     END SELECT

     IF (door(choice)) THEN           
        staycount = staycount + 1
     ELSE IF (door(remaining)) THEN   
        switchcount = switchcount + 1
     END IF
    
  END DO

  WRITE(*, "(A,F6.2,A)") "Chance of winning by not switching is", real(staycount)/trials*100, "%"
  WRITE(*, "(A,F6.2,A)") "Chance of winning by switching is", real(switchcount)/trials*100, "%" 

END PROGRAM MONTYHALL
Task: Monty Hall problem





Java: import java.util.Random;
public class Monty{
	public static void main(String[] args){
		int switchWins = 0;
		int stayWins = 0;
		Random gen = new Random();
		for(int plays = 0;plays < 32768;plays++ ){
			int[] doors = {0,0,0};
			doors[gen.nextInt(3)] = 1;
			int choice = gen.nextInt(3); 
			int shown; 
			do{
				shown = gen.nextInt(3);
			
			}while(doors[shown] == 1 || shown == choice);
			
			stayWins += doors[choice];
			
			
			switchWins += doors[3 - choice - shown];
		}
		System.out.println("Switching wins " + switchWins + " times.");
		System.out.println("Staying wins " + stayWins + " times.");
	}
}






Fortran: PROGRAM MONTYHALL
                            
  IMPLICIT NONE  

  INTEGER, PARAMETER :: trials = 10000
  INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
  LOGICAL :: door(3)
  REAL :: rnum

  CALL RANDOM_SEED
  DO i = 1, trials
     door = .FALSE.
     CALL RANDOM_NUMBER(rnum)
     prize = INT(3*rnum) + 1
     door(prize) = .TRUE.              
    
     CALL RANDOM_NUMBER(rnum)   
     choice = INT(3*rnum) + 1          

     DO
        CALL RANDOM_NUMBER(rnum)   
        show = INT(3*rnum) + 1 
        IF (show /= choice .AND. show /= prize) EXIT       
     END DO

     SELECT CASE(choice+show)          
       CASE(3)
          remaining = 3
       CASE(4)
          remaining = 2
       CASE(5)
          remaining = 1
     END SELECT

     IF (door(choice)) THEN           
        staycount = staycount + 1
     ELSE IF (door(remaining)) THEN   
        switchcount = switchcount + 1
     END IF
    
  END DO

  WRITE(*, "(A,F6.2,A)") "Chance of winning by not switching is", real(staycount)/trials*100, "%"
  WRITE(*, "(A,F6.2,A)") "Chance of winning by switching is", real(switchcount)/trials*100, "%" 

END PROGRAM MONTYHALL
Task: Monty Hall problem







Python: 
from random import randrange

doors, iterations = 3,100000  

def monty_hall(choice, switch=False, doorCount=doors):
  
  door = [False]*doorCount
  
  door[randrange(doorCount)] = True

  chosen = door[choice]

  unpicked = door
  del unpicked[choice]

  
  
  
  alternative = True in unpicked

  if switch:
    return alternative
  else:
    return chosen

print "\nMonty Hall problem simulation:"
print doors, "doors,", iterations, "iterations.\n"

print "Not switching allows you to win",
print sum(monty_hall(randrange(3), switch=False)
          for x in range(iterations)),
print "out of", iterations, "times."
print "Switching allows you to win",
print sum(monty_hall(randrange(3), switch=True)
          for x in range(iterations)),
print "out of", iterations, "times.\n"




Fortran: PROGRAM MONTYHALL
                            
  IMPLICIT NONE  

  INTEGER, PARAMETER :: trials = 10000
  INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
  LOGICAL :: door(3)
  REAL :: rnum

  CALL RANDOM_SEED
  DO i = 1, trials
     door = .FALSE.
     CALL RANDOM_NUMBER(rnum)
     prize = INT(3*rnum) + 1
     door(prize) = .TRUE.              
    
     CALL RANDOM_NUMBER(rnum)   
     choice = INT(3*rnum) + 1          

     DO
        CALL RANDOM_NUMBER(rnum)   
        show = INT(3*rnum) + 1 
        IF (show /= choice .AND. show /= prize) EXIT       
     END DO

     SELECT CASE(choice+show)          
       CASE(3)
          remaining = 3
       CASE(4)
          remaining = 2
       CASE(5)
          remaining = 1
     END SELECT

     IF (door(choice)) THEN           
        staycount = staycount + 1
     ELSE IF (door(remaining)) THEN   
        switchcount = switchcount + 1
     END IF
    
  END DO

  WRITE(*, "(A,F6.2,A)") "Chance of winning by not switching is", real(staycount)/trials*100, "%"
  WRITE(*, "(A,F6.2,A)") "Chance of winning by switching is", real(switchcount)/trials*100, "%" 

END PROGRAM MONTYHALL
Task: Monty Hall problem






PHP: <?php
function montyhall($iterations){
	$switch_win = 0;
	$stay_win = 0;
		
	foreach (range(1, $iterations) as $i){
		$doors = array(0, 0, 0);
		$doors[array_rand($doors)] = 1;
		$choice = array_rand($doors);
		do {
			$shown = array_rand($doors);
		} while($shown == $choice || $doors[$shown] == 1);
			
		$stay_win += $doors[$choice];
		$switch_win += $doors[3 - $choice - $shown];
	}
		
	$stay_percentages = ($stay_win/$iterations)*100;
	$switch_percentages = ($switch_win/$iterations)*100;
		
	echo "Iterations: {$iterations} - ";
	echo "Stayed wins: {$stay_win} ({$stay_percentages}%) - ";
	echo "Switched wins: {$switch_win} ({$switch_percentages}%)";
}
	
        montyhall(10000);
?>





Fortran: PROGRAM MONTYHALL
                            
  IMPLICIT NONE  

  INTEGER, PARAMETER :: trials = 10000
  INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
  LOGICAL :: door(3)
  REAL :: rnum

  CALL RANDOM_SEED
  DO i = 1, trials
     door = .FALSE.
     CALL RANDOM_NUMBER(rnum)
     prize = INT(3*rnum) + 1
     door(prize) = .TRUE.              
    
     CALL RANDOM_NUMBER(rnum)   
     choice = INT(3*rnum) + 1          

     DO
        CALL RANDOM_NUMBER(rnum)   
        show = INT(3*rnum) + 1 
        IF (show /= choice .AND. show /= prize) EXIT       
     END DO

     SELECT CASE(choice+show)          
       CASE(3)
          remaining = 3
       CASE(4)
          remaining = 2
       CASE(5)
          remaining = 1
     END SELECT

     IF (door(choice)) THEN           
        staycount = staycount + 1
     ELSE IF (door(remaining)) THEN   
        switchcount = switchcount + 1
     END IF
    
  END DO

  WRITE(*, "(A,F6.2,A)") "Chance of winning by not switching is", real(staycount)/trials*100, "%"
  WRITE(*, "(A,F6.2,A)") "Chance of winning by switching is", real(switchcount)/trials*100, "%" 

END PROGRAM MONTYHALL
Task: Monty Hall problem






PHP: <?php
function montyhall($iterations){
	$switch_win = 0;
	$stay_win = 0;
		
	foreach (range(1, $iterations) as $i){
		$doors = array(0, 0, 0);
		$doors[array_rand($doors)] = 1;
		$choice = array_rand($doors);
		do {
			$shown = array_rand($doors);
		} while($shown == $choice || $doors[$shown] == 1);
			
		$stay_win += $doors[$choice];
		$switch_win += $doors[3 - $choice - $shown];
	}
		
	$stay_percentages = ($stay_win/$iterations)*100;
	$switch_percentages = ($switch_win/$iterations)*100;
		
	echo "Iterations: {$iterations} - ";
	echo "Stayed wins: {$stay_win} ({$stay_percentages}%) - ";
	echo "Switched wins: {$switch_win} ({$switch_percentages}%)";
}
	
        montyhall(10000);
?>
Task: Monty Hall problem






PHP: <?php
function montyhall($iterations){
	$switch_win = 0;
	$stay_win = 0;
		
	foreach (range(1, $iterations) as $i){
		$doors = array(0, 0, 0);
		$doors[array_rand($doors)] = 1;
		$choice = array_rand($doors);
		do {
			$shown = array_rand($doors);
		} while($shown == $choice || $doors[$shown] == 1);
			
		$stay_win += $doors[$choice];
		$switch_win += $doors[3 - $choice - $shown];
	}
		
	$stay_percentages = ($stay_win/$iterations)*100;
	$switch_percentages = ($switch_win/$iterations)*100;
		
	echo "Iterations: {$iterations} - ";
	echo "Stayed wins: {$stay_win} ({$stay_percentages}%) - ";
	echo "Switched wins: {$switch_win} ({$switch_percentages}%)";
}
	
        montyhall(10000);
?>






Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;

procedure Monty_Stats is
   Num_Iterations : Positive := 100000;
   type Action_Type is (Stay, Switch);
   type Prize_Type is (Goat, Pig, Car);
   type Door_Index is range 1..3;
   package Random_Prize is new Ada.Numerics.Discrete_Random(Door_Index);
   use Random_Prize;
   Seed : Generator;
   Doors : array(Door_Index) of Prize_Type;
   
   procedure Set_Prizes is
      Prize_Index : Door_Index;
      Booby_Prize : Prize_Type := Goat;
   begin
      Reset(Seed);
      Prize_Index := Random(Seed);
      Doors(Prize_Index) := Car;
      for I in Doors'range loop
         if I /= Prize_Index then
            Doors(I) := Booby_Prize;
            Booby_Prize := Prize_Type'Succ(Booby_Prize);
         end if;
      end loop;
   end Set_Prizes;
   
   function Play(Action : Action_Type) return Prize_Type is
      Chosen : Door_Index := Random(Seed);
      Monty : Door_Index;
   begin
      Set_Prizes;
      for I in Doors'range loop
         if I /= Chosen and Doors(I) /= Car then
            Monty := I;
         end if;
      end loop;
      if Action = Switch then
         for I in Doors'range loop
            if I /= Monty and I /= Chosen then
               Chosen := I;
               exit;
            end if;
         end loop;
      end if;
      return Doors(Chosen);
   end Play;
   Winners : Natural;
   Pct : Float;
begin
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Stay) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Stay : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");
   Winners := 0;
   for I in 1..Num_Iterations loop
      if Play(Switch) = Car then
         Winners := Winners + 1;
      end if;
   end loop;
   Put("Switch : count" & Natural'Image(Winners) & " = ");
   Pct := Float(Winners * 100) / Float(Num_Iterations);
   Put(Item => Pct, Aft => 2, Exp => 0);
   Put_Line("%");

end Monty_Stats;
Task: Polymorphic copy



C#: using System;

class T
{
    public virtual string Name()
    {
        return "T";
    }

    public virtual T Clone()
    {
        return new T();
    }
}

class S : T
{
    public override string Name()
    {
        return "S";
    }

    public override T Clone()
    {
        return new S();
    }
}

class Program
{
    static void Main()
    {
        T original = new S();
        T clone = original.Clone();

        Console.WriteLine(original.Name());
        Console.WriteLine(clone.Name());
    }
}









Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Polymorphic_Copy is
   package Base is
      type T is tagged null record;
      type T_ptr is access all T'Class;
      function Name (X : T) return String;
   end Base;
   use Base;
   
   package body Base is
      function Name (X : T) return String is
      begin
         return "T";
      end Name;
   end Base;
   
      
   procedure Copier (X : T'Class) is
      Duplicate : T'Class := X;  
   begin
      Put_Line ("Copied " & Duplicate.Name); 
   end Copier;

      
   function Clone (X : T'Class) return T_ptr is
   begin
      return new T'Class(X);
   end Copier;

   package Derived is   
      type S is new T with null record;
      overriding function Name (X : S) return String;
   end Derived;
   use Derived;
   
   package body Derived is   
      function Name (X : S) return String is
      begin
         return "S";
      end Name;
   end Derived;

   Object_1 : T;
   Object_2 : S;
   Object_3 : T_ptr := Clone(T);
   Object_4 : T_ptr := Clone(S);
begin
   Copier (Object_1);
   Copier (Object_2);
   Put_Line ("Cloned " & Object_3.all.Name);
   Put_Line ("Cloned " & Object_4.all.Name);
end Test_Polymorphic_Copy;
Task: Polymorphic copy

C: int x;
int y = x;











Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Polymorphic_Copy is
   package Base is
      type T is tagged null record;
      type T_ptr is access all T'Class;
      function Name (X : T) return String;
   end Base;
   use Base;
   
   package body Base is
      function Name (X : T) return String is
      begin
         return "T";
      end Name;
   end Base;
   
      
   procedure Copier (X : T'Class) is
      Duplicate : T'Class := X;  
   begin
      Put_Line ("Copied " & Duplicate.Name); 
   end Copier;

      
   function Clone (X : T'Class) return T_ptr is
   begin
      return new T'Class(X);
   end Copier;

   package Derived is   
      type S is new T with null record;
      overriding function Name (X : S) return String;
   end Derived;
   use Derived;
   
   package body Derived is   
      function Name (X : S) return String is
      begin
         return "S";
      end Name;
   end Derived;

   Object_1 : T;
   Object_2 : S;
   Object_3 : T_ptr := Clone(T);
   Object_4 : T_ptr := Clone(S);
begin
   Copier (Object_1);
   Copier (Object_2);
   Put_Line ("Cloned " & Object_3.all.Name);
   Put_Line ("Cloned " & Object_4.all.Name);
end Test_Polymorphic_Copy;
Task: Polymorphic copy


C++: #include <iostream>

class T
{
public:
  virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
  virtual T* clone() { return new T(*this); }
  virtual ~T() {}
};

class S: public T
{
public:
  virtual void identify() { std::cout << "I am an S" << std::endl; }
  virtual S* clone() { return new S(*this); }
};

class X 
{
public:
  
  X(T* t): member(t) {}

  
  X(X const& other): member(other.member->clone()) {}

  
  X& operator=(X const& other)
  {
    T* new_member = other.member->clone();
    delete member;
    member = new_member;
  }

  
  ~X() { delete member; }

  
  void identify_member() { member->identify(); }

private:
  T* member;
};

int main()
{
  X original(new S);      
  X copy = original;      
  copy.identify_member(); 
}










Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Polymorphic_Copy is
   package Base is
      type T is tagged null record;
      type T_ptr is access all T'Class;
      function Name (X : T) return String;
   end Base;
   use Base;
   
   package body Base is
      function Name (X : T) return String is
      begin
         return "T";
      end Name;
   end Base;
   
      
   procedure Copier (X : T'Class) is
      Duplicate : T'Class := X;  
   begin
      Put_Line ("Copied " & Duplicate.Name); 
   end Copier;

      
   function Clone (X : T'Class) return T_ptr is
   begin
      return new T'Class(X);
   end Copier;

   package Derived is   
      type S is new T with null record;
      overriding function Name (X : S) return String;
   end Derived;
   use Derived;
   
   package body Derived is   
      function Name (X : S) return String is
      begin
         return "S";
      end Name;
   end Derived;

   Object_1 : T;
   Object_2 : S;
   Object_3 : T_ptr := Clone(T);
   Object_4 : T_ptr := Clone(S);
begin
   Copier (Object_1);
   Copier (Object_2);
   Put_Line ("Cloned " & Object_3.all.Name);
   Put_Line ("Cloned " & Object_4.all.Name);
end Test_Polymorphic_Copy;
Task: Polymorphic copy




Go: package main

import (
    "fmt"
    "reflect"
)


type i interface {
    identify() string
}


type t float64





type s struct {
    t
    kōan string
}



type r struct {
    t
    ch chan int
}




func (x t) identify() string {
    return "I'm a t!"
}




func (x s) identify() string {
    return "I'm an s!"
}

func main() {
    
    var t1 t = 5
    var s1 s = s{6, "one"}
    var r1 r = r{t: 7}

    
    var i1, i2, i3 i
    fmt.Println("Initial (zero) values of interface variables:")
    fmt.Println("i1:", i1)
    fmt.Println("i2:", i2)
    fmt.Println("i3:", i3)

    
    
    i1, i2, i3 = t1, s1, r1
    fmt.Println("\nPolymorphic:")
    fmt.Println("i1:", i1, "/", i1.identify(), "/", reflect.TypeOf(i1))
    fmt.Println("i2:", i2, "/", i2.identify(), "/", reflect.TypeOf(i2))
    fmt.Println("i3:", i3, "/", i3.identify(), "/", reflect.TypeOf(i3))

    
    i1c, i2c, i3c := i1, i2, i3

    
    i1, i2, i3 = s{3, "dog"}, r{t: 1}, t(2)

    
    
    fmt.Println("\nFirst set now modified:")
    fmt.Println("i1:", i1, "/", i1.identify(), "/", reflect.TypeOf(i1))
    fmt.Println("i2:", i2, "/", i2.identify(), "/", reflect.TypeOf(i2))
    fmt.Println("i3:", i3, "/", i3.identify(), "/", reflect.TypeOf(i3))

    fmt.Println("\nCopies made before modifications:")
    fmt.Println("i1c:", i1c, "/", i1c.identify(), "/", reflect.TypeOf(i1c))
    fmt.Println("i2c:", i2c, "/", i2c.identify(), "/", reflect.TypeOf(i2c))
    fmt.Println("i3c:", i3c, "/", i3c.identify(), "/", reflect.TypeOf(i3c))
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Polymorphic_Copy is
   package Base is
      type T is tagged null record;
      type T_ptr is access all T'Class;
      function Name (X : T) return String;
   end Base;
   use Base;
   
   package body Base is
      function Name (X : T) return String is
      begin
         return "T";
      end Name;
   end Base;
   
      
   procedure Copier (X : T'Class) is
      Duplicate : T'Class := X;  
   begin
      Put_Line ("Copied " & Duplicate.Name); 
   end Copier;

      
   function Clone (X : T'Class) return T_ptr is
   begin
      return new T'Class(X);
   end Copier;

   package Derived is   
      type S is new T with null record;
      overriding function Name (X : S) return String;
   end Derived;
   use Derived;
   
   package body Derived is   
      function Name (X : S) return String is
      begin
         return "S";
      end Name;
   end Derived;

   Object_1 : T;
   Object_2 : S;
   Object_3 : T_ptr := Clone(T);
   Object_4 : T_ptr := Clone(S);
begin
   Copier (Object_1);
   Copier (Object_2);
   Put_Line ("Cloned " & Object_3.all.Name);
   Put_Line ("Cloned " & Object_4.all.Name);
end Test_Polymorphic_Copy;