Java'daki sınıfları Haskell'in functor'larına benzer şekilde tanımlamaya çalışıyorum. olarak vesile, bir funktor tanımlanır: Bir functor uygulamak istiyorsanızJava'daki İşlevler
/**
* Programming languages allow only (just simply enough) endofunctor, that are functors from and to the same category.
* In this case, the category is the one of the datatypes (in here Type, in order to make it more clear)
*/
public interface EndoFunctor<X extends Type> extends Type {
/**
* The basic implementation for any element fx
* @param map Transformation function for the type parameter
* @param fx Element of the current class
* @param <Y> Target type
* @return transformed element through map
*/
<Y extends Type> EndoFunctor<Y> fmap(Function<X,Y> map, EndoFunctor<X> fx);
}
, ben bu kod ile
public class Id<X extends Type> implements EndoFunctor<X> {
protected X witness;
Id(X witness) { this.witness = witness; }
@Override
public <Y extends Type> Id<Y> fmap(Function<X, Y> map, Id<X> fx) {
return new Id<>(map.apply(fx.witness));
}
}
sorun gibi bir şey yazmak zorunda Id<X>
türüyle uyumlu olmamasıdır EndoFunctor<X>
. Nasıl, (Benim nesne bir Id<T>
olduğunu biliyoruz çünkü yani, her tür K<T>
EndoFunctor<T>
uygulayan ve bir harita işlevi T->U
verilirse, o zaman K<U>
herhangi tiplemeleri olmadan, bir değer olarak döndürülür EndoFunctor
arayüzü böyle de fmap
belirleyebilir daha sonra fmap
sonucu "Id<U>
olması gerekir, ve bu yüzden tür tipi EndoFunctor<U>
sonucunu downcast down)?
Bunun yerine, 'EndoFunctor fmap (İşlev map) 'kullanmamış olmanızın bir nedeni var mı? Bu şekilde, her örnek kendi örnek değişkenlerini kullanır. I.e., 'new Id döndürme <> (map.apply (this.witness))'. –
afsantos
Bu gözlemin soruma cevap verebileceğini sanmıyorum. Haritanız ('fmap2 (x)') 'fmap (x, this)' olarak tanımlanabilir, bu yüzden bu tip problemi gerçekten çözmez. – jackb
Bu arada, işlevi bir funker'in tanımına daha çok benzetmek için bu şekilde tanımladım, yani [F: (a-> b) -> (Fa -> Fb)] (http: // Bu durumda F, EndoFunctor olan latex.codecogs.com/gif.download?F%5Ccolon%20%28a%5Cto%20b%29%5Cto%20%28Fa%5Cto%20Fb%29). – jackb