LoginSignup
0
0

More than 5 years have passed since last update.

F#でcatamorphism/anamorhphism

Last updated at Posted at 2014-03-01

[F#] おーおーふぁんくたでFunctorをオブジェクトで実現するのを見て、その方向でcategorical programming with inductive and coinductive typesにあるcatamorphismとanamorphismのF#による実装を考えてみた。

type F<'c> =
    abstract fmap : ('a -> 'b) ->  F<'a> -> F<'b>

type Mu<'a> =
    In of F<Mu<'a>>

let unIn p =
    match p with
    | In(x) -> x

let rec cata phi x =
    let y = (x |> unIn)
    y |> y.fmap (cata phi) |> phi

type Nu<'a> =
    Wrap of F<Nu<'a>>

let out p =
    match p with
    | Wrap(x) -> x

let rec ana phi x = 
    let y : F<'c> = (x |> phi)
    Wrap(y.fmap(ana phi) y)

Mu,Nuの定義に直接Fを使っているが使わなくてもよい方法はあるかしら。

定義してみたはいいが使い道は不明。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0