Skip to content

Commit caf3b24

Browse files
committed
typeclass instance Id
1 parent 6b38d39 commit caf3b24

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/fantasy/typeclasses/apply.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export namespace Apply {
1212
const __name = "Apply"
1313
}
1414

15-
1615
export function ap<F extends ApplyInstances, A, B>(fab: $<F, (a: A) => B>, fa: $<F, A>): $<F, B> {
1716
let instance = (<any>Functor)[kind(fab)]
18-
return instance.ap(fab, fa) as $<F, B>
17+
let faba: $<F, [(a: A) => B, A]> = instance.product(fab, fa)
18+
return instance.map((aba: [(a: A) => B, A]) => aba[0](aba[1]), faba) as $<F, B>
1919
}
2020

2121
export function ap2<F extends ApplyInstances, A, B, C>(fabc: $<F, (a: A, b: B) => C>, fa: $<F, A>, fb: $<F, B>): $<F, C> {

src/fantasy/typeclasses/id.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { datatype, $ } from '.'
22
import { Functor } from './functor'
3+
import { ap, Apply } from './apply'
4+
import { Cartesian } from './cartesian'
5+
import { Applicative } from './applicative'
6+
import { FlatMap } from './flatmap'
7+
import { Monad } from './monad'
38

49
@datatype('Id')
510
export class Id<A> {
@@ -31,3 +36,75 @@ declare module './functor' {
3136
}
3237

3338
Functor.Id = new IdFunctor
39+
40+
export class IdCartesian implements Cartesian<"Id"> {
41+
product<A, B>(fa: Id<A>, fb: Id<B>): Id<[A, B]> {
42+
return new Id([fa.value, fb.value] as [A, B])
43+
}
44+
}
45+
46+
declare module './cartesian' {
47+
namespace Cartesian {
48+
export let Id: IdCartesian
49+
}
50+
}
51+
52+
Cartesian.Id = new IdCartesian
53+
54+
export class IdApply implements Apply<"Id"> {
55+
ap<A, B>(fab: Id<(a: A) => B>, fa: Id<A>): Id<B> {
56+
return ap<"Id", A, B>(fab, fa)
57+
}
58+
map = Functor.Id.map
59+
product = Cartesian.Id.product
60+
}
61+
62+
declare module './apply' {
63+
namespace Apply {
64+
export let Id: IdApply
65+
}
66+
}
67+
68+
Apply.Id = new IdApply
69+
70+
export class IdApplicative extends IdApply implements Applicative<"Id">{
71+
pure<A>(a: A): Id<A> {
72+
return new Id(a)
73+
}
74+
}
75+
76+
declare module './applicative' {
77+
namespace Applicative {
78+
export let Id: IdApplicative
79+
}
80+
}
81+
82+
Applicative.Id = new IdApplicative
83+
84+
85+
export class IdFlatMap extends IdApply implements FlatMap<"Id">{
86+
flatMap<A, B>(f: (a: A) => Id<B>, fa: Id<A>): Id<B> {
87+
return this.map(f, fa).value
88+
}
89+
}
90+
91+
declare module './flatmap' {
92+
namespace FlatMap {
93+
export let Id: IdFlatMap
94+
}
95+
}
96+
97+
FlatMap.Id = new IdFlatMap
98+
99+
100+
export class IdMonad extends IdApplicative implements Monad<"Id">{
101+
flatMap = FlatMap.Id.flatMap
102+
}
103+
104+
declare module './monad' {
105+
namespace Monad {
106+
export let Id: IdMonad
107+
}
108+
}
109+
110+
Monad.Id = new IdMonad

0 commit comments

Comments
 (0)