Skip to content

Instantly share code, notes, and snippets.

@eulerfx
eulerfx / universal_construction.fs
Created October 15, 2017 21:52
Universal Construction
open Marvel
open System.Collections.Concurrent
open System.Collections.Generic
type Pid = int
type Op = {
op : string
sn : SN
}
I Δ(I) I Δ(I)
(0,⊥) (0,⊥) (1,⊥) (1,⊥)
(⊥,0) (⊥,0) (⊥,1) (⊥,1)
(0,0) (0,0) (1,1) (1,1)
(0,1) (0,0), (1,1) (1,0) (0,0), (1,1)
@eulerfx
eulerfx / unique-id-task-spec.md
Last active August 12, 2017 20:20
Unique-id Task Specification
I Δ(I)
(0,⊥,⊥) (0,⊥,⊥), (1,⊥,⊥), (2,⊥,⊥)
(⊥,0,⊥) (⊥,0,⊥), (⊥,1,⊥), (⊥,2,⊥)
(⊥,⊥,0) (⊥,⊥,0), (⊥,⊥,1), (⊥,⊥,2)
(0,0,⊥) (0,1,⊥), (1,0,⊥), (0,2,⊥), (2,0,⊥), (2,1,⊥), (1,2,⊥)
(0,⊥,0) (0,⊥,1), (1,⊥,0), (0,⊥,2), (2,⊥,0), (2,⊥,1), (1,⊥,2)
(⊥,0,0) (⊥,0,1), (⊥,1,0), (⊥,0,2), (⊥,2,0), (⊥,2,1), (⊥,1,2)
(0,0,0) (0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)
@eulerfx
eulerfx / opt-parse.fs
Created June 21, 2017 12:04
F# applicative CLI option parser
let readInt (s:string) =
match System.Int32.TryParse s with
| true,i -> Some i
| _ -> None
type Opt<'a> = Opt of name:string * defaultValue:'a option * read:(string -> 'a option)
with
static member Name<'a> (Opt(n,_,_) : Opt<'a>) = n
static member Read<'a> (Opt(_,_,r) : Opt<'a>) = r
static member Default<'a> (Opt(_,d,_) : Opt<'a>) = d
open System
open Microsoft.FSharp.Reflection
open Newtonsoft.Json
open Newtonsoft.Json.Converters
type OptionConverter() =
inherit JsonConverter()
override x.CanConvert(t) =
t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<option<_>>
@eulerfx
eulerfx / TaskHelper.cs
Created March 22, 2012 19:00
TPL retry logic
/// <summary>
/// Utility methods for <see cref="System.Threading.Tasks.Task"/>.
/// </summary>
public static class TaskHelper
{
/// <summary>
/// Returns a task which retries the task returned by the specified task provider.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="taskProvider">The task returning function.</param>
@eulerfx
eulerfx / fad.fs
Last active May 14, 2016 15:53
F# automatic differentiation
/// A value together with its derivative.
[<NoComparison>]
type D<'a> = D of 'a * 'a
module D =
[<GeneralizableValue>]
let inline GenericTwo () =
LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne
@eulerfx
eulerfx / Tcp.fs
Created May 14, 2016 09:14
F# sockets, framing, sessions
/// Operations on Berkley sockets.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Socket =
/// Executes an async socket operation.
let exec (alloc:unit -> SocketAsyncEventArgs, free:SocketAsyncEventArgs -> unit) (config:SocketAsyncEventArgs -> unit) (op:SocketAsyncEventArgs -> bool) (map:SocketAsyncEventArgs -> 'a) =
Async.FromContinuations <| fun (ok, error, _) ->
let args = alloc ()
@eulerfx
eulerfx / DVar.fs
Created May 14, 2016 13:07
DVar - dependent/dynamic variables
open System
open System.Threading
/// A dependant variable.
type DVar<'a> = private { cell : 'a ref ; event : Event<'a> }
/// Operations on dependant variables.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module DVar =
@eulerfx
eulerfx / AsyncSeqVsScalaz-stream.md
Last active March 4, 2016 21:48
Informal proof that F# AsyncSeq is equal in expressive power to scalaz-stream Process when specialized to Task

There is a great purely functional streaming/IO library in Scala called scalaz-stream. It is itself based upon a Haskell library called machines. They provide powerful abstractions to express compositional effectful computations. These libraries rely on certain type system features in both Scala and Haskell which are unavailable in F# - namely existential types and higher kinds. Higher kinds allow the monad representing the side-effects to be abstracted over. If however we specialize this to a specific monad, we can get around the lack of existentials as well.

Tomas Petricek created a type called asynchronous sequence which provides similar capabilities. The F# AsyncSeq type is declared as follows:

type AsyncSeq<'a> = Async<AsyncSeqInner<'a>>

and AsyncSeqInner<'a> = 
    | Nil
 | Cons of 'a * AsyncSeq&lt;'a&gt;