Code Monkey home page Code Monkey logo

binat's Introduction

BiNat

My exercise in Idris, reimplementing natural number with O(log n).

Motivation

  • My practicing in Idris and proofs
  • Nat has very slow performance (try fromIntegerNat 100 * 100)
  • With Int or something primitive, we can't write proof by induction

Features

BiNat defines a natural number as a finite sequence of bits. By this, we have following features:

  • 0 is not a natural number
    • Because every sequence should start with 1
  • Costs O(log n) for defining a natural number n
  • Induction through function BiNat.Properties.Induction.induction
    • Note that n is not structurally smaller than n + 1
    • There is also complete induction BiNat.Properties.LT.completeInduction

Examples

Factorial function using BiNat.Properties.Induction.induction

import BiNat
import BiNat.Properties.Induction

fact : BiNat -> BiNat
fact = induction
  (\k => BiNat)                  -- All intermediate values are of type BiNat
  (\k, factk => (k + 1) * factk) -- Define fact (k + 1) using k and fact k
  J                              -- RHS of fact J = J

Fibonacci function using BiNat.Properties.Induction.induction

import BiNat
import BiNat.Properties.Induction

fib : BiNat -> BiNat
fib n = fst $ induction                     -- Taking first element of the pair (fib n, fib (n + 1))
  (\k => (BiNat, BiNat))                    -- Each accumulator is (fib k, fib (k + 1))
  (\k, (fib0, fib1) => (fib1, fib0 + fib1))
  (J, J)                                    -- (fib 1, fib 2) = (1, 1)
  n

Development

We use asdf to manage language versions.

Install asdf-idris plugin (beware that issue!) and then asdf install.

binat's People

Contributors

sekit avatar

Watchers

 avatar

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.