Does Haskell support anonymous instances of typeclass? -


i have following code in f# (it's book)

open system.collections.generic  type table<'t, 'u> =    abstract item : 't -> 'u    abstract discard : unit -> unit  let memoizeandpermitdiscard f =      let lookasidetable = new dictionary<_, _>(hashidentity.structural)      {new table<'t, 'u>       member t.item          get(n) =              if lookasidetable.containskey(n)                  lookasidetable.[n]              else                  let res = f n                  lookasidetable.add(n, res)                  res       member t.discard() =             lookasidetable.clear()} let rec fibfast =      memoizeandpermitdiscard (fun n ->        printfn "computing fibfast %d" n      if n <= 2 1 else fibfast.[n - 1] + fibfast.[n - 2]) 

as can see abstract type table take it's implementation in function memoizeandpermitdiscard. can haskell same?

i'm not f# expert either, believe you're describing create anonymous single-use subclass, declaring in @ point create object how implements methods of superclass or interface? it's anonymous class, not anonymous instance (or rather, it's no more anonymous other object-oriented instance, typically don't have names inherently, variable names storing references them).

it doesn't make sense haskell type classes/instances. reason haskell instance represents different oo instance.

the instances of oo classes objects (even instances of interfaces objects). of class' methods invoked on instance of class. makes sense create anonymous subclass of existing class or interface @ time create new object. how object implements required methods, alternative declaring whole named class of objects implement methods same way, instantiate in multiple places.

the instances of haskell classes types (which why the're called type classes). of methods of class must involve type somehow, there no guarantee take input of type. example, consider class1:

class monoid'   mempty' ::         mappend' :: -> -> 

it doesn't make sense object instance of monoid'; if create new object , wanted anonymously instantiate monoid', how define mempty'? mempty' isn't operation invoke on new object, it's operation receives no inputs @ (not implicit "this") , produces value2.

and there's things like:

class functor' f   fmap :: (a -> b) -> (f -> f b) 

nothing ever takes input of type f instance of functor'; doesn't make sense talk might, since instances of class functor' type constructors need type parameter result in type, not types contain values. again, makes no sense @ point i'm creating new object "and here's how object implements functor'").

it potentially make sense declare new anonymous type locally, , declare how implements type classes @ same time. haskell has no syntax it, no.

fortunately, don't need create anonymous classes/instances in order have one-off collection of functions conforms known interface. functions first-class values too, can have type fields functions. anywhere can create new value of type providing definition of function fields. example:

data myinterface = myinterface   { foo :: int -> bool   , bar :: int -> string   }  example :: myinterface -> int -> (bool, string) example impl x   = (foo impl x, bar impl x)  main =   let impl = myinterface { foo = even, bar = show }   print $ example impl 7 

the above program prints (false,"7").


1 i'm using monoid' rather monoid (and functor') because i'm using simplifications of real classes. can see real definition :info monoid in ghci (or documentation) if you're interested.

2 or alternatively, class monoid' mandates there exists value of each type instantiates it, , mempty' reference it.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -