Skip to content

Instantly share code, notes, and snippets.

View jindraivanek's full-sized avatar

Jindřich Ivánek jindraivanek

View GitHub Profile
@jindraivanek
jindraivanek / getAllDerivedTypes.fs
Created March 24, 2016 09:10
getAllDerivedTypes
[<Interface>]
type ITag =
abstract member ToLabel : unit -> string
type Row = Row of char
with interface ITag with member x.ToLabel() = let (Row c) = x in sprintf "%c" c
type Column = Column of char
with interface ITag with member x.ToLabel() = let (Column c) = x in sprintf "%c" c
let getAllDerivedTypes<'t> =
let t = typeof<'t>
let memoize (f: 'a -> 'b) =
let cache = System.Collections.Concurrent.ConcurrentDictionary<_, _>(HashIdentity.Structural)
fun x ->
cache.GetOrAdd(x, lazy (f x)).Force()
// This works, but emit warning: This and other recursive references to the object(s) being defined will be checked for initialization-soundness at runtime through the use of a delayed reference.
let rec fib' = memoize <| fun n -> if n<1 then 1 else fib' (n-1) + fib' (n-2)
//-----------------------------------
let memoizeRec f =
open System
open System.IO
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#r "packages/FSharp.Compiler.Service/lib/net45/FSharp.Compiler.Service.dll"
// Open the namespace with InteractiveChecker type
open Microsoft.FSharp.Compiler
open Microsoft.FSharp.Compiler.SourceCodeServices
module ShowTrait =
[<Trait>]
type Show<'T> =
abstract member show: 'T -> string
[<Witness>]
type ShowInt =
interface Show<int> with
member __.show(x) = x.ToString()
module T =
open System
type IBrand<'a, 'b> = interface end
type InProgress private () = class end
type Finished private () = class end
type Cancelled private () = class end
type Decision<'a> = Decision of 'a
module T =
open System
[<Trait>]
type Show<'a> =
abstract member show: 'a -> string
[<Witness>]
type ShowInt =
interface Show<int> with
module T =
open System
type Brand<'tag, 'b>(value: obj) =
member this.Apply() : 'c = value :?> _
[<Trait>]
type IBrand<'Kind, 'Tag, 'a> =
abstract inj: 'Kind -> Brand<'Tag, 'a>
abstract prj: Brand<'Tag, 'a> -> 'Kind
#load ".paket/load/main.group.fsx"
open FParsec
type Json = JString of string
| JNumber of float
| JBool of bool
| JNull
| JList of Json list
#load ".paket/load/main.group.fsx"
//used snippets from https://fsharpforfunandprofit.com/posts/understanding-parser-combinators-4/
open FParsec
type NewLineStyle = | NLnever | NLwrap | NLalways
type FmtOptions = { ColumnWidth : int option; Spaces : bool; IndentSize : int; ObjectNewLine : NewLineStyle; ArrayNewLine : NewLineStyle }
@jindraivanek
jindraivanek / File_ordering_alg.md
Last active January 10, 2018 08:40
Mechanic: file ordering alg

Ordering of files alg. is splitted into 3 parts:

  1. Collecting symbols from AST
  2. From symbols, find dependencies between files
  3. Construct oriented graph from dependencies and solve topological order on it

Collecting symbols from AST

For each file, from untyped tree we collect three types of symbols:

  • symbol definition, fully qualified (let bindings, type declaration, DU case constructors, record fields, members)