Skip to content

Instantly share code, notes, and snippets.

@edwinb
edwinb / inBound.idr
Created November 25, 2012 21:15
List bounds
using (xs : List a)
data InBound : Nat -> List a -> Set where
inO : InBound O (x :: xs)
inS : InBound k xs -> InBound (S k) (x :: xs)
@edwinb
edwinb / vc.idr
Created November 26, 2012 18:33
Type class resolution as a (partial) decision procedure
-- Idris type classes actually take any kind of value as a parameter,
-- we're not restricted to Sets or parameterised Sets.
-- So we actually have 'value classes'. It seems we can use type class
-- resolution to make rudimentary decision procedures, then:
using (xs : List a)
data Elem : a -> List a -> Set where
Here : Elem x (x :: xs)
There : Elem x xs -> Elem x (y :: xs)
@edwinb
edwinb / rational.idr
Created December 3, 2012 15:20
Rational
data Succ : Nat -> Set where
yes : Succ (S k)
data RationalP = RatP Nat Nat
data Rational : Set where
Rat : (num : Nat) -> (den : Nat) -> Succ den -> Rational
total toRational : RationalP -> Rational
toRational (RatP x y) = Rat x (S y) yes
-- Not fine:
mergeBy : (a -> a -> Ordering) -> List a -> List a -> List a
mergeBy order [] right = right
mergeBy order left [] = left
mergeBy order (x::xs) (y::ys) =
case order x y of
LT => x :: mergeBy order xs (y :: ys)
_ => y :: mergeBy order (x :: xs) ys
@edwinb
edwinb / rewrite_pc.idr
Created July 9, 2013 14:24
rewrite ... ==> in ...
plus_comm : (n : Nat) -> (m : Nat) -> n + m = m + n
-- Base case
(O + m = m + O) <== plus_comm =
rewrite ((m + O = m) <== plusZeroRightNeutral) ==> (m = m) in refl
-- Step case
(S k + m = m + S k) <== plus_comm =
rewrite ((k + m = m + k) <== plus_comm) ==> (S (m + k) = m + S k) in
rewrite ((S (m + k) = m + S k) <== plusSuccRightSucc) in
@edwinb
edwinb / plus_comm.idr
Created July 31, 2013 20:10
plus commutes
plus_comm : (n : Nat) -> (m : Nat) -> (n + m = m + n)
-- Base case
(O + m = m + O) <== plus_comm =
rewrite ((m + O = m) <== plusZeroRightNeutral) ==>
(O + m = m) in refl
-- Step case
(S k + m = m + S k) <== plus_comm =
rewrite ((k + m = m + k) <== plus_comm) in
@edwinb
edwinb / reflect.idr
Last active December 20, 2015 15:08
Automated list associativity proofs, by reflection, in Idris.
module Reflect
import Decidable.Equality
using (xs : List a, ys : List a, G : List (List a))
data Elem : a -> List a -> Type where
Stop : Elem x (x :: xs)
Pop : Elem x ys -> Elem x (y :: xs)
@edwinb
edwinb / gist:8479028
Created January 17, 2014 18:40
It Went Wrong
Stream.idr:39:9:Incomplete term Effects.>>= {m = IO} {a = PID} {xs =
Prelude.List.:: {a = EFFECT} (MkEff (ProtoT {a = Actions} {p = Type} {chan =
PID} (DoSend {princ = Type} (Symbol_ "Server") Command (\ t : Command =>
mkProcess {princ = Type} {t = ()} {xs = Prelude.List.:: {a = Type} (Symbol_
"Client") (Prelude.List.:: {a = Type} (Symbol_ "Server") (Prelude.List.Nil {a =
Type}))} (Symbol_ "Client") (case block in count 0 t t) (\ x : () => End)))
(Prelude.List.Nil {a = (Type, PID)})) (Msg Type PID)) (Prelude.List.:: {a =
EFFECT} (MkEff () Conc) (Prelude.List.:: {a = EFFECT} (MkEff () StdIO)
(Prelude.List.Nil {a = EFFECT})))} {xs' = \ v : PID => updateWith {a = EFFECT}
{ys = Prelude.List.:: {a = EFFECT} (MkEff () Conc) (Prelude.List.Nil {a =
@edwinb
edwinb / gist:8551794
Last active January 4, 2016 01:59
minus, with erasue
module erase
data LessThan : Nat -> Nat -> Type where
LtO : LessThan Z (S k)
LtS : LessThan x y -> LessThan (S x) (S y)
minus : (x : Nat) -> (y : Nat) -> LessThan y x -> Nat
minus (S k) Z LtO = S k
minus (S k) (S j) (LtS z) = minus k j
@edwinb
edwinb / err
Created January 27, 2014 00:08
Better error messages...
Problem:
--------
You tried to use the random number generator effect when it wasn't available.
New error message:
------------------
Expr.idr:31:6:When elaborating right hand side of eval:
Can't solve goal