Git Product home page Git Product logo

fsharp.dependent's Introduction

FSharp.Dependent

ARCHIVED: Seeking better way in https://github.com/cannorin/FSharp.TypeLevel

The FSharp.Dependent library can be installed from NuGet

PM> Install-Package FSharp.Dependent

This library simulates Dependent Types in F# using type-level natural numbers and (ab)using inline functions.

Example

#r "FSharp.Dependent.dll"
open FSharp.Dependent
open System

let three = Nat<3>.value
let eight = Nat<8>.value

let eleven = three + eight
// let err = three - eight
// error FS0001: internal error: Exception of type 'Microsoft.FSharp.Compiler.ConstraintSolver+LocallyAbortOperationThatFailsToResolveOverload' was thrown.

let xs = 1 ^+ 2 ^+ 3 ^+ 4 ^+ VNil
let ys = Vect.init three id |> Vect.map ((+) 5)

let zs = Vect.append xs ys
zs |> Vect.item Nat<5>.value |> printfn "%i" // 6
// zs |> Vect.item eleven |> printfn "%i"
// error FS0001: internal error: Exception of type 'Microsoft.FSharp.Compiler.ConstraintSolver+LocallyAbortOperationThatFailsToResolveOverload' was thrown.

1 ^+ Vect.empty |> Vect.head |> printfn "%i" // 1
// Vect.empty |> Vect.head |> printfn "%i"
// error FS0001: Type mismatch. Expecting a
//    'Vect<Z,'a> -> 'b'
// but given a
//    'Vect<Z,'c> -> 'c'
// This expression was expected to have type
//    'S<'a>'
// but here has type
//    'Z'

let inline randomLessThan n =
  let rand = Random().Next(0, natVal n - 1)
  rand |> RuntimeNat.LTE <| pred n // existential quantifier simulation

zs |> Vect.item (randomLessThan zs.length) |> printfn "%i"

let inline pleaseGiveMeLessThan limit x =
  Constraint.LTETerm(x, pred limit); // this does nothing in term-level but adds a constraint in type level, here abuses F#'s constraint solver!
  printfn "I'm happy with %i." (natVal x)

zs.length |> pleaseGiveMeLessThan eleven // I'm happy with 7.

let inline winwin x = randomLessThan x |> pleaseGiveMeLessThan x

winwin eight

let (>>=) x f = Option.bind f x
let readNat name =
  printf "%s > " name;
  Console.ReadLine() |> Number.tryParse |> Option.map SomeNat

readNat "input length" >>= mapSomeNat (fun (len: S<SomeNatVariable<"length">>) ->
    let xs = Vect.init len (fun seed -> Random(seed).Next(0,100))
    while true do
      readNat "input index" >>= mapSomeNat (fun (x: RangedNat<Z, SomeNatVariable<"length">>) ->
          xs |> Vect.item x |> printfn "xs[%i] = %i" (natVal x)
        ) |> Option.defaultWith (fun () -> printfn "index is out of range!")
    done
  )

Samples & documentation

The library comes with comprehensible documentation. It can include tutorials automatically generated from *.fsx files in the content folder. The API reference is automatically generated from Markdown comments in the library implementation.

  • Tutorial contains a further explanation of this sample library.

  • API Reference contains automatically generated documentation for all types, modules and functions in the library. This includes additional brief samples on using most of the functions.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

fsharp.dependent's People

Contributors

cannorin avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

stjordanis

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.