GithubHelp home page GithubHelp logo

thautwarm / fstan Goto Github PK

View Code? Open in Web Editor NEW
42.0 6.0 4.0 78 KB

Higher abstraction infrastructures in F#(ad-hoc polymorphism, subtypeclassing, monad, hkt...), exactly what we've dreamed about for so long

License: Apache License 2.0

F# 100.00%
typeclasses subtypeclassing higher-kinded-types monad monoids functional-programming

fstan's Introduction

FSTan

Exactly a full-featured and practical implementation typeclasses and higher kinded types in F#.

For manuals check Guide.md, where you'll be told how to use these concise typeclasses, higher kinded types and constraints.

Motivation and Features

There are also other similar implementations in FSharp like Higher and FSharpPlus, but they're not able to provide all the features listed below, which motivate me to create a better one:

  • Support instance resolution.
  • Support ad-hoc polymorphism.
  • Support to create a typeclass and add constraints to it.
  • Support subtypeclassing.
  • Support to directly access type constructor.
  • Support default implementations for typeclass.
  • All above operations are quite lightweighted and not implemented in a magic way.

Yes, exactly, it's a what many and I have dreamed about for so long.

Limitation

  1. The performance might hurt in some scenarios, for each the datatype works with higher kinded types have to be upcasted to an unique abstract class, for instance, maybe<'a> has to be casted to hkt<Maybe, 'a>.

  2. For some builtin datatypes cannot be interfaced with hkt, an extra wrapper class is required to work with higher kined types.

    For instance, interface type listData<'a> is required for the builtin List<'a>.

    You can use wrap and unwrap to transform datatypes from List<'a> to hkt<mkList<ListSig>,'a>, vice versa.

     module List' = List
     type List'<'a> = List<'a>
    
    
     type mkList<'L>() =
         inherit monad<mkList<'L>>()
             static member wrap<'a> (x : List'<'a>): hkt<mkList<'L>, 'a> =
                 {wrap = x} :> _
             static member unwrap<'a> (x : hkt<mkList<'L>, 'a>): List'<'a> =
                 (x :?> _).wrap
    
             default si.bind<'a, 'b> (m: hkt<mkList<'L>, 'a>) (k: 'a -> hkt<mkList<'L>, 'b>): hkt<mkList<'L>, 'b> =
                 wrap <| List'.collect (unwrap << k) (unwrap m)
    
             default si.pure'<'a> (a: 'a): hkt<mkList<'L>, 'a> = wrap <| [a]
             interface show<mkList<'L>> with
                 member si.show (x: hkt<mkList<'L>, 'a>) =
                     let x = unwrap x
                     x.ToString()
    
     and listData<'L, 'a> =
         {wrap : List'<'a>}
         interface hkt<mkList<'L>, 'a>
    
    
    // create a concrete List type
    type ListSig() =
     // default implements following type classes:
     // - monad (functor and applicative are implemented simultaneously)
     // - show
    
     inherit mkList<ListSig>()
    type list<'a> = hkt<mkList<ListSig>, 'a>
    
    let test() =
    
         let listm: _ list = Do {
             let! x = wrap [1, 2, 3]
             wrap [x]
         }
         // listm : resolved to be list<int>
    
         let f (x: int) : string = ""
         fmap f listm
         // return value is resolved to be list<string>
  3. Cannot implement instance for datatypes that are not constructed by a type constructor. For instance, you cannot implement any typeclass for all primitives types like integers, floats and so on, unless you wrap them with an Identity type constructor.

fstan's People

Contributors

shalokshalom avatar thautwarm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.