Search code examples
f#dsltagless-finalf#+fsharp-typeclasses

Implementing Tagless Final Encoding in F# with SRTP


I'd like to transform my F# OOP version of Tagless Final into a typical FP approach and I'm thinking to use Statically Resolved Type Parameters of Type Classes from OO.

What I've done is

open System
open FSharpPlus

type UserName = string
type DataResult<'t> = DataResult of 't with
    static member Map ( x:DataResult<'t>  , f) =
        match x with 
        | DataResult t -> DataResult (f t)

creating the SRTP I need

type Cache = 
    static member inline getOfCache cacheImpl data =
        ( ^T : (member getFromCache : 't -> DataResult<'t> option) (cacheImpl, data))
    static member inline storeOfCache cacheImpl data =
        ( ^T : (member storeToCache : 't -> unit) (cacheImpl, data))

type DataSource() =
    static member inline getOfSource dataSourceImpl data =
        ( ^T : (member getFromSource : 't -> DataResult<'t>) (dataSourceImpl, data))
    static member inline storeOfSource dataSourceImpl data =
        ( ^T : (member storeToSource : 't -> unit) (dataSourceImpl, data))

and their concrete implementations

type CacheNotInCache() = 
        member this.getFromCache _ = None
        member this.storeCache _ = () 

type CacheInCache() =
        member this.getFromCache user = monad { 
           return! DataResult user |> Some}
        member this.storeCache _ = () 

type  DataSourceNotInCache() = 
          member this.getFromSource user = monad { 
               return! DataResult user } 

type  DataSourceInCache()  =
          member this.getFromSource _  = 
              raise (NotImplementedException())        

by which I can define a tagless final DSL

let requestData (cacheImpl: ^Cache) (dataSourceImpl: ^DataSource) (userName:UserName) = monad {
    match Cache.getOfCache cacheImpl userName with
    | Some dataResult -> 
            return! map ((+) "cache: ") dataResult
    | None -> 
            return! map ((+) "source: ") (DataSource.getOfSource dataSourceImpl userName) }

and that kind of works as follows

[<EntryPoint>]
let main argv =
    let cacheImpl1 = CacheInCache() 
    let dataSourceImpl1 = DataSourceInCache()
    let cacheImpl2 = CacheNotInCache() 
    let dataSourceImpl2 = DataSourceNotInCache()
    requestData cacheImpl1 dataSourceImpl1 "john" |> printfn "%A"
    //requestData (cacheImpl2 ) dataSourceImpl2 "john" |> printfn "%A"
    0 

The problem is that I'm getting the warning

construct causes code to be less generic than indicated by the type annotations

for both cacheImpl1 and dataSourceImpl1 and so I can't reuse requestData for the other case. Is there a way to detour this issue?


Solution

  • I'm not familiar with the abstraction you're trying to implement, but looking at your code it seems you're missing an inline modifier here:

    let inline requestData (cacheImpl: ^Cache) (dataSourceImpl: ^DataSource) (userName:UserName) = monad {
        match Cache.getOfCache cacheImpl userName with
        | Some dataResult -> 
                return! map ((+) "cache: ") dataResult
        | None -> 
                return! map ((+) "source: ") (DataSource.getOfSource dataSourceImpl userName) }
    

    As a side note, you can simplify your map function like this:

    type DataResult<'t> = DataResult of 't with
        static member Map (DataResult t, f) = DataResult (f t)