Add all the A tasks

This commit is contained in:
Ingy döt Net 2013-04-10 14:58:50 -07:00
parent 2dd7375f96
commit 051504d65b
1608 changed files with 18584 additions and 0 deletions

View file

@ -0,0 +1,11 @@
'''Abstract type''' is a type without instances or without definition.
For example in [[object-oriented programming]] using some languages, abstract types can be partial implementations of other types, which are to be derived there-from. An abstract type may provide implementation of some operations and/or components. Abstract types without any implementation are called '''interfaces'''. In the languages that do not support multiple [[inheritance]] ([[Ada]], [[Java]]), classes can, nonetheless, inherit from multiple interfaces. The languages with multiple inheritance (like [[C++]]) usually make no distinction between partially implementable abstract types and interfaces. Because the abstract type's implementation is incomplete, [[object-oriented programming | OO]] languages normally prevent instantiation from them (instantiation must derived from one of their descendant classes).
The term '''abstract datatype''' also may denote a type, with an implementation provided by the programmer rather than directly by the language (a '''built-in''' or an inferred type). Here the word ''abstract'' means that the implementation is abstracted away, irrelevant for the user of the type. Such implementation can and should be hidden if the language supports separation of implementation and specification. This hides complexity while allowing the implementation to change without repercussions on the usage. The corresponding software design practice is said to follow the [[wp:Information_hiding|information hiding principle]].
It is important not to confuse this ''abstractness'' (of implementation) with one of the '''abstract type'''. The latter is abstract in the sense that the set of its values is empty. In the sense of implementation abstracted away, all user-defined types are abstract.
In some languages, like for example in Objective Caml which is strongly statically typed, it is also possible to have '''abstract types''' that are not OO related and are not an abstractness too. These are ''pure abstract types'' without any definition even in the implementation and can be used for example for the type algebra, or for some consistence of the type inference. For example in this area, an abstract type can be used as a phantom type to augment another type as its parameter. <!-- An OCaml Guru would explain this better than me, a poor beginner... -->
'''Task''': show how an abstract type can be declared in the language. If the language makes a distinction between interfaces and partially implemented types illustrate both.

View file

@ -0,0 +1,5 @@
---
category:
- Object oriented
- Type System
note: Basic language learning

View file

@ -0,0 +1,13 @@
class abs definition abstract.
public section.
methods method1 abstract importing iv_value type f exporting ev_ret type i.
protected section.
methods method2 abstract importing iv_name type string exporting ev_ret type i.
methods add importing iv_a type i iv_b type i exporting ev_ret type i.
endclass.
class abs implementation.
method add.
ev_ret = iv_a + iv_b.
endmethod.
endclass.

View file

@ -0,0 +1,5 @@
interface inter.
methods: method1 importing iv_value type f exporting ev_ret type i,
method2 importing iv_name type string exporting ev_ret type i,
add importing iv_a type i iv_b type i exporting ev_ret type i.
endinterface.

View file

@ -0,0 +1,8 @@
package
{
public interface IInterface
{
function method1():void;
function method2(arg1:Array, arg2:Boolean):uint;
}
}

View file

@ -0,0 +1,3 @@
type Queue is limited interface;
procedure Enqueue (Lounge : in out Queue; Item : in out Element) is abstract;
procedure Dequeue (Lounge : in out Queue; Item : in out Element) is abstract;

View file

@ -0,0 +1,2 @@
type Scheduler is task interface;
procedure Plan (Manager : in out Scheduler; Activity : in out Job) is abstract;

View file

@ -0,0 +1,10 @@
with Ada.Finalization;
...
type Node is abstract new Ada.Finalization.Limited_Controlled and Queue with record
Previous : not null access Node'Class := Node'Unchecked_Access;
Next : not null access Node'Class := Node'Unchecked_Access;
end record;
overriding procedure Finalize (X : in out Node); -- Removes the node from its list if any
overriding procedure Dequeue (Lounge : in out Node; Item : in out Element);
overriding procedure Enqueue (Lounge : in out Node; Item : in out Element);
procedure Process (X : in out Node) is abstract; -- To be implemented

View file

@ -0,0 +1,53 @@
module AbstractInterfaceExample where
open import Function
open import Data.Bool
open import Data.String
-- * One-parameter interface for the type `a' with only one method.
record VoiceInterface (a : Set) : Set where
constructor voice-interface
field say-method-of : a String
open VoiceInterface
-- * An overloaded method.
say : {a : Set} _ : VoiceInterface a a String
say instance = say-method-of instance
-- * Some data types.
data Cat : Set where
cat : Bool Cat
crazy! = true
plain-cat = false
-- | This cat is crazy?
crazy? : Cat Bool
crazy? (cat x) = x
-- | A 'plain' dog.
data Dog : Set where
dog : Dog
-- * Implementation of the interface (and method).
instance-for-cat : VoiceInterface Cat
instance-for-cat = voice-interface case where
case : Cat String
case x with crazy? x
... | true = "meeeoooowwwww!!!"
... | false = "meow!"
instance-for-dog : VoiceInterface Dog
instance-for-dog = voice-interface $ const "woof!"
-- * and then:
--
-- say dog => "woof!"
-- say (cat crazy!) => "meeeoooowwwww!!!"
-- say (cat plain-cat) => "meow!"
--

View file

@ -0,0 +1,5 @@
class Abs {
public function method1...
public function method2...
}

View file

@ -0,0 +1,5 @@
interface Inter {
function isFatal : integer
function operate (para : integer = 0)
operator -> (stream, isout)
}

View file

@ -0,0 +1,38 @@
use std
(: abstract class :)
class Abs
text name
AbsIface iface
class AbsIface
function(Abs)(int)->int method
let Abs_Iface = Cdata AbsIface@ {.method = nil}
.: new Abs :. -> Abs {let a = new(Abs); a.iface = Abs_Iface; a}
=: <Abs self>.method <int i> := -> int
(self.iface.method is nil) ? 0 , (call self.iface.method with self i)
(: implementation :)
class Sub <- Abs { int value }
let Sub_Iface = Cdata AbsIface@ {.method = (code of (nil the Sub).method 0)}
.: new Sub (<int value = -1>) :. -> Sub
let s = new (Sub)
s.iface = Sub_Iface
s.value = value
s
.: <Sub this>.method <int i> :. -> int {this.value + i}
(: example use :)
.:foobar<Abs a>:. {print a.method 12 ; del a}
foobar (new Sub 34) (: prints 46 :)
foobar (new Sub) (: prints 11 :)
foobar (new Abs) (: prints 0 :)

View file

@ -0,0 +1,28 @@
color(r, g, b){
static color
If !color
color := Object("base", Object("R", r, "G", g, "B", b
,"GetRGB", "Color_GetRGB"))
return Object("base", Color)
}
Color_GetRGB(clr) {
return "not implemented"
}
waterColor(r, g, b){
static waterColor
If !waterColor
waterColor := Object("base", color(r, g, b),"GetRGB", "WaterColor_GetRGB")
return Object("base", WaterColor)
}
WaterColor_GetRGB(clr){
return clr.R << 16 | clr.G << 8 | clr.B
}
test:
blue := color(0, 0, 255)
msgbox % blue.GetRGB() ; displays "not implemented"
blue := waterColor(0, 0, 255)
msgbox % blue.GetRGB() ; displays 255
return

View file

@ -0,0 +1,33 @@
#ifndef INTERFACE_ABS
#define INTERFACE_ABS
typedef struct sAbstractCls *AbsCls;
typedef struct sAbstractMethods {
int (*method1)(AbsCls c, int a);
const char *(*method2)(AbsCls c, int b);
void (*method3)(AbsCls c, double d);
} *AbstractMethods, sAbsMethods;
struct sAbstractCls {
AbstractMethods klass;
void *instData;
};
#define ABSTRACT_METHODS( cName, m1, m2, m3 ) \
static sAbsMethods cName ## _Iface = { &m1, &m2, &m3 }; \
AbsCls cName ## _Instance( void *clInst) { \
AbsCls ac = malloc(sizeof(struct sAbstractCls)); \
if (ac) { \
ac->klass = &cName ## _Iface; \
ac->instData = clInst; \
}\
return ac; }
#define Abs_Method1( c, a) (c)->klass->method1(c, a)
#define Abs_Method2( c, b) (c)->klass->method2(c, b)
#define Abs_Method3( c, d) (c)->klass->method3(c, d)
#define Abs_Free(c) \
do { if (c) { free((c)->instData); free(c); } } while(0);
#endif

View file

@ -0,0 +1,9 @@
#ifndef SILLY_H
#define SILLY_H
#include intefaceAbs.h
typedef struct sillyStruct *Silly;
extern Silly NewSilly( double, const char *);
extern AbsCls Silly_Instance(void *);
#endif

View file

@ -0,0 +1,42 @@
#include "silly.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct sillyStruct {
double v1;
char str[32];
};
Silly NewSilly(double vInit, const char *strInit)
{
Silly sily = malloc(sizeof( struct sillyStruct ));
sily->v1 = vInit;
sily->str[0] = '\0';
strncat(sily->str, strInit, 31);
return sily;
}
static
int MyMethod1( AbsCls c, int a)
{
Silly s = (Silly)(c->instData);
return a+strlen(s->str);
}
static
const char *MyMethod2(AbsCls c, int b)
{
Silly s = (Silly)(c->instData);
sprintf(s->str, "%d", b);
return s->str;
}
static
void MyMethod3(AbsCls c, double d)
{
Silly s = (Silly)(c->instData);
printf("InMyMethod3, %f\n",s->v1 * d);
}
ABSTRACT_METHODS( Silly, MyMethod1, MyMethod2, MyMethod3)

View file

@ -0,0 +1,13 @@
#include <stdio.h>
#include "silly.h"
int main()
{
AbsCls abster = Silly_Instance(NewSilly( 10.1, "Green Tomato"));
printf("AbsMethod1: %d\n", Abs_Method1(abster, 5));
printf("AbsMethod2: %s\n", Abs_Method2(abster, 4));
Abs_Method3(abster, 21.55);
Abs_Free(abster);
return 0;
}

View file

@ -0,0 +1 @@
(defprotocol Foo (foo [this]))

View file

@ -0,0 +1,17 @@
deferred class
AN_ABSTRACT_CLASS
feature
a_deferred_feature
-- a feature whose implementation is left to a descendent
deferred
end
an_effective_feature: STRING
-- deferred (abstract) classes may still include effective features
do
Result := "I am implemented!"
end
end

View file

@ -0,0 +1,25 @@
include 4pp/lib/foos.4pp
:: X()
class
method: method1
method: method2
end-class {
:method { ." Method 1 in X" cr } ; defines method1
}
;
:: Y()
extends X()
end-extends {
:method { ." Method 2 in Y" cr } ; defines method2
}
;
: Main
static Y() y
y => method1
y => method2
;
Main

View file

@ -0,0 +1,5 @@
interface {
Method1(value float64) int
SetName(name string)
GetName() string
}

View file

@ -0,0 +1,3 @@
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool

View file

@ -0,0 +1,5 @@
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
x /= y = not (x == y)
x == y = not (x /= y)

View file

@ -0,0 +1,2 @@
func :: (Eq a) => a -> Bool
func x = x == x

View file

@ -0,0 +1 @@
data Foo = Foo {x :: Integer, str :: String}

View file

@ -0,0 +1,3 @@
instance Eq Foo where
(Foo x1 str1) == (Foo x2 str2) =
(x1 == x2) && (str1 == str2)

View file

@ -0,0 +1,3 @@
class abstraction()
abstract method compare(l,r) # generates runerr(700, "method compare()")
end

View file

@ -0,0 +1,7 @@
public abstract class Abs {
abstract public int method1(double value);
abstract protected int method2(String name);
int add(int a, int b){
return a+b;
}
}

View file

@ -0,0 +1,5 @@
public interface Inter {
int method1(double value);
int method2(String name);
int add(int a, int b);
}

View file

@ -0,0 +1,34 @@
BaseClass = {}
function class ( baseClass )
local new_class = {}
local class_mt = { __index = new_class }
function new_class:new()
local newinst = {}
setmetatable( newinst, class_mt )
return newinst
end
if not baseClass then baseClass = BaseClass end
setmetatable( new_class, { __index = baseClass } )
return new_class
end
function abstractClass ( self )
local new_class = {}
local class_mt = { __index = new_class }
function new_class:new()
error("Abstract classes cannot be instantiated")
end
if not baseClass then baseClass = BaseClass end
setmetatable( new_class, { __index = baseClass } )
return new_class
end
BaseClass.class = class
BaseClass.abstractClass = abstractClass

View file

@ -0,0 +1,8 @@
A = class() -- New class A inherits BaseClass by default
AA = A:class() -- New class AA inherits from existing class A
B = abstractClass() -- New abstract class B
BB = B:class() -- BB is not abstract
A:new() -- Okay: New class instance
AA:new() -- Okay: New class instance
B:new() -- Error: B is abstract
BB:new() -- Okay: BB is not abstract

View file

@ -0,0 +1,7 @@
abstract class Abs {
abstract public function method1($value);
abstract protected function method2($name);
function add($a, $b){
return a + b;
}
}

View file

@ -0,0 +1,5 @@
interface Inter {
public function method1($value);
public function method2($name);
public function add($a, $b);
}

View file

@ -0,0 +1,14 @@
package AbstractFoo;
use strict;
sub frob { die "abstract" }
sub baz { die "abstract" }
sub frob_the_baz {
my $self = shift;
$self->frob($self->baz());
}
1;

View file

@ -0,0 +1,13 @@
package AbstractFoo;
use strict;
sub frob { ... }
sub baz { ... }
sub frob_the_baz {
my $self = shift;
$self->frob($self->baz());
}
1;

View file

@ -0,0 +1,12 @@
package AbstractFoo;
use Moose::Role;
requires qw/frob baz/;
sub frob_the_baz {
my $self = shift;
$self->frob($self->baz());
}
1;

View file

@ -0,0 +1,12 @@
package AbstractFoo;
use Role::Tiny;
requires qw/frob baz/;
sub frob_the_baz {
my $self = shift;
$self->frob($self->baz());
}
1;

View file

@ -0,0 +1,11 @@
# In PicoLisp there is no formal difference between abstract and concrete classes.
# There is just a naming convention where abstract classes start with a
# lower-case character after the '+' (the naming convention for classes).
# This tells the programmer that this class has not enough methods
# defined to survive on its own.
(class +abstractClass)
(dm someMethod> ()
(foo)
(bar) )

View file

@ -0,0 +1,13 @@
class BaseQueue(object):
"""Abstract/Virtual Class
"""
def __init__(self):
self.contents = list()
raise NotImplementedError
def Enqueue(self, item):
raise NotImplementedError
def Dequeue(self):
raise NotImplementedError
def Print_Contents(self):
for i in self.contents:
print i,

View file

@ -0,0 +1,21 @@
from abc import ABCMeta, abstractmethod
class BaseQueue():
"""Abstract Class
"""
__metaclass__ = ABCMeta
def __init__(self):
self.contents = list()
@abstractmethod
def Enqueue(self, item):
pass
@abstractmethod
def Dequeue(self):
pass
def Print_Contents(self):
for i in self.contents:
print i,

View file

@ -0,0 +1,13 @@
#lang racket
(define animal-interface (interface () say))
(define cat% (class* object% (animal-interface) (super-new))) ;; error
(define cat% (class* object% (animal-interface)
(super-new)
(define/public (say)
(display "meeeeew!"))))
(define tom (new cat%))
(send tom say)

View file

@ -0,0 +1,17 @@
require 'abstraction'
class AbstractQueue
abstract
def enqueue(object)
raise NotImplementedError
end
def dequeue
raise NotImplementedError
end
end
class ConcreteQueue < AbstractQueue
def enqueue(object)
puts "enqueue #{object.inspect}"
end
end

View file

@ -0,0 +1,10 @@
abstract class X {
type A
var B: A
val C: A
def D(a: A): A
}
trait Y {
val x: X
}

View file

@ -0,0 +1,9 @@
oo::class create AbstractQueue {
method enqueue item {
error "not implemented"
}
method dequeue {} {
error "not implemented"
}
self unexport create new
}