junkcode
Check-in [ff0e1f8539]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Switching from Identity to Option solves the Bind type inference issue.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:ff0e1f8539daa8c05343dc4233a20567471ea7a3694cd60b71cc82452809c191
User & Date: jaccarmac 2018-01-29 05:45:46
Context
2018-01-29
05:57
Simplify further. check-in: 23c2fc6b5b user: jaccarmac tags: trunk
05:45
Switching from Identity to Option solves the Bind type inference issue. check-in: ff0e1f8539 user: jaccarmac tags: trunk
05:41
An alternate implementation of the typeclass approach. check-in: 1b76182634 user: jaccarmac tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to free-monad-vs-typeclass/Program.cs.

13
14
15
16
17
18
19
20
21
22
23
24
25
26
            var freeProgram = new ResourcePrinterFree<Unit>.Print("Hello, world!", () =>
                new ResourcePrinterFree<Unit>.Return(unit));
            freeProgram.Map(FreeInterpreter.Interpret);

            TypeclassProgram<ResourcePrinterTypeclassUnit, Unit>();
        }

        public static Identity<Unit> TypeclassProgram<ResourcePrinterTypeclassT, T>() where ResourcePrinterTypeclassT: struct, ResourcePrinterTypeclass<Identity<T>, T> =>
            default(MIdentity<ResourceWrapper>).Bind<MIdentity<Unit>, Identity<Unit>, Unit>(
                default(ResourcePrinterTypeclassT).AcquireResource<MIdentity<ResourceWrapper>, Identity<ResourceWrapper>>(),
                r => use(r, _ => default(ResourcePrinterTypeclassT).Print<MIdentity<Unit>, Identity<Unit>>(r, "Hello, world!"))
            );
    }
}







|
<
|
|
<


13
14
15
16
17
18
19
20

21
22

23
24
            var freeProgram = new ResourcePrinterFree<Unit>.Print("Hello, world!", () =>
                new ResourcePrinterFree<Unit>.Return(unit));
            freeProgram.Map(FreeInterpreter.Interpret);

            TypeclassProgram<ResourcePrinterTypeclassUnit, Unit>();
        }

        public static Option<Unit> TypeclassProgram<ResourcePrinterTypeclassT, T>() where ResourcePrinterTypeclassT: struct, ResourcePrinterTypeclass<Option<T>, T> =>

            default(ResourcePrinterTypeclassT).AcquireResource<MOption<ResourceWrapper>, Option<ResourceWrapper>>().Bind(r => 
                use(r, _ => default(ResourcePrinterTypeclassT).Print<MOption<Unit>, Option<Unit>>(r, "Hello, world!")));

    }
}

Changes to free-monad-vs-typeclass/ResourcePrinterTypeclass.cs.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

namespace free_monad_vs_typeclass {
    public interface ResourcePrinterTypeclass<MA, A> {
        MB Print<MonadB, MB>(ResourceWrapper resource, string output) where MonadB: struct, Monad<MB, Unit>;
        MB AcquireResource<MonadB, MB>() where MonadB: struct, Monad<MB, ResourceWrapper>;
    }

    public struct ResourcePrinterTypeclassUnit : ResourcePrinterTypeclass<Identity<Unit>, Unit> {
        public MB Print<MonadB, MB>(ResourceWrapper resource, string output) where MonadB: struct, Monad<MB, Unit> =>
            default(MonadB).Return(fun(() => resource.Print(output))());
        
        public MB AcquireResource<MonadB, MB>() where MonadB: struct, Monad<MB, ResourceWrapper> =>
            default(MonadB).Return(new ResourceWrapper());
    }
}







|







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

namespace free_monad_vs_typeclass {
    public interface ResourcePrinterTypeclass<MA, A> {
        MB Print<MonadB, MB>(ResourceWrapper resource, string output) where MonadB: struct, Monad<MB, Unit>;
        MB AcquireResource<MonadB, MB>() where MonadB: struct, Monad<MB, ResourceWrapper>;
    }

    public struct ResourcePrinterTypeclassUnit : ResourcePrinterTypeclass<Option<Unit>, Unit> {
        public MB Print<MonadB, MB>(ResourceWrapper resource, string output) where MonadB: struct, Monad<MB, Unit> =>
            default(MonadB).Return(fun(() => resource.Print(output))());
        
        public MB AcquireResource<MonadB, MB>() where MonadB: struct, Monad<MB, ResourceWrapper> =>
            default(MonadB).Return(new ResourceWrapper());
    }
}