Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created June 26, 2012 05:57
Show Gist options
  • Save einblicker/2993628 to your computer and use it in GitHub Desktop.
Save einblicker/2993628 to your computer and use it in GitHub Desktop.
eliminating array bound checking in F#
module ElimArrayBoundCheck =
type BIndex<'Tag>(idx) =
member this.Idx = idx
type BArray<'Tag, 'T>(arr : 'T[]) =
member this.lookup(bidx : BIndex<'Tag>) = arr.[bidx.Idx]
type C<'T, 'R> = interface
abstract s<'Tag> : BIndex<'Tag> * BArray<'Tag, 'T> -> 'R
abstract f : 'R
end
let check (arr : 'T[]) (idx : int) (c : C<'T, 'Result>) =
c.f(BIndex(idx), BArray(arr))
open ElimArrayBoundCheck
let main _ =
check [| 1; 2; 3 |] 1 { new C.C<_, _> with
member this.f(bidx1, barr1) =
check [| 1; 2; 3 |] 1 { new C.C<_, _> with
member this.f(bidx2, barr2) =
barr1.lookup(bidx1) |> ignore
barr2.lookup(bidx2) |> ignore
//barr2.lookup(bidx1) |> ignore
//barr1.lookup(bidx2) |> ignore
}
}
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment