Mentions légales du service

Skip to content
Snippets Groups Projects
Commit f790db37 authored by Baptiste Pauget's avatar Baptiste Pauget
Browse files

Some examples

parent dd8bdbe0
No related branches found
No related tags found
No related merge requests found
/*-------- Fast Fourier transform --------*/
// Language: Surface
// Checking: ../compiler.exe Surf fft.surf
let vec_vec u v = $fold (+) 0 ($map2 (*) u v)
let mat_vec A u = $map (vec_vec u) A
let mat_mat A B = $map (mat_vec A) ($transpose B)
// Extract smaller divisor of n
let div n = 1 /*
let rec f k =
if k * k < n then 1 else
if n % k = 0 then k else
f (k+1)
in f (2)
**/
// Dummy exponential and pure imaginary unity
let exp x = 1+x*(1+x/2*(1+x/3*(1+0)))
let i = 1
let pi = 3
let rec fft <<n>> (x:[n]_) : [n]_ =
// Twiddle factors
let tw [j,k] = exp (2*i*pi*j*k/n) in
// One step of decomposition
let dft <<n,p>> =
|> $split
|> $map fft <<n>>
|> $transpose |> $map2 ($map2 (*)) tw
|> $map fft <<p>>
|> $flatten
in
// Recusive call
let size d = div n in
let size k = n / d in
if d = 1 then mat_vec tw x else
(dft <<d,k>> (x:>[k*d]_) :> [n]_)
let rec fft <<n>> : [n]_ -> [n]_ =
// Twiddle factors
let tw [j,k] = exp (2*i*pi*j*k/n) in
let size d = div n in
let size k = n / d in
if d = 1 then mat_vec tw else
:> [k*d]_
|> $split
|> $map fft <<d>>
|> $transpose |> $map2 ($map2 (*)) tw
|> $map fft <<k>>
|> $flatten
:> [n]_
// Twiddle factors
let tw n [j,k] = exp (2*i*pi*j*k/n)
let rec fft <<n>> : [n]_ -> [n]_ =
let size d = div n in
let size k = n / d in
if d = 1 then mat_vec (tw n) else
:> [k*d]_
|> $split
|> $map fft <<d>>
|> $transpose |> $map2 ($map2 (*)) (tw n)
|> $map fft <<k>>
|> $flatten
:> [n]_
let repeat <<n>> c [_<n] = c
let repeat2 <<n,p>> =
|> repeat <<n>>
|> repeat <<p>>
/*-------- Examples on type variables generalization --------*/
// Language: Surface
// Checking: ../compiler.exe Surf test-polymorphism.surf
let ign _ _ = false
// Generalization of quoted type vars (top-decl)
let f x = let g : 'a = fun x -> x in g (x) // SUCC
let f = let g (x:'a) = x in ign (g 0) (g 42 ) // SUCC
let f = let g (x:'a) = x in ign (g 0) (g true) // FAIL
let f = let g (x: _) = x in ign (g 0) (g true) // SUCC
let f x = let g (x:'a) = x in ign (g x) (g x) // SUCC
let f = let g (x:'a) (y:'b) = x=y in g true false // SUCC
let f: type a. a -> _ = fun x -> let g (x:'a) = x in ign (g x) (g true) // FAIL
let f: bool -> _ = fun x -> let g (x:'a) = x in ign (g x) (g true) // SUCC
let f: type a. a -> _ = fun x -> let g (x: _) = x in ign (g x) (g true) // SUCC
let z = let i x = x in i i 0 // SUCC
let z = let i x = (x:'a) in i i 0 // FAIL
// Type var escaping
let f (x:'c) : 'd = x // SUCC
let f (type c d) (x:c) : d = x // FAIL
let f (type a) (x:a) y = x = y // SUCC
let f x = let g: type a. a -> _ = fun y -> x = y in g (x) // FAIL
let f: type a. a -> _ = fun x y -> x = y // FAIL
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment