mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Move test fixtures to samples/
This commit is contained in:
27
samples/standard-ml/Foo.sig
Normal file
27
samples/standard-ml/Foo.sig
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
signature LAZY_BASE =
|
||||
sig
|
||||
type 'a lazy
|
||||
exception Undefined
|
||||
val force: 'a lazy -> 'a
|
||||
val delay: (unit -> 'a) -> 'a lazy
|
||||
val undefined: 'a lazy
|
||||
end
|
||||
|
||||
signature LAZY' =
|
||||
sig
|
||||
include LAZY_BASE
|
||||
val isUndefined: 'a lazy -> bool
|
||||
val inject : 'a -> 'a lazy
|
||||
val toString: ('a -> string) -> 'a lazy -> string
|
||||
val eq: ''a lazy * ''a lazy -> bool
|
||||
val eqBy: ('a * 'a -> bool) -> 'a lazy * 'a lazy -> bool
|
||||
val compare: ('a * 'a -> order) -> 'a lazy * 'a lazy -> order
|
||||
val map: ('a -> 'b) -> 'a lazy -> 'b lazy
|
||||
|
||||
structure Ops:
|
||||
sig
|
||||
val ! : 'a lazy -> 'a (* force *)
|
||||
val ? : 'a -> 'a lazy (* inject *)
|
||||
end
|
||||
end
|
||||
75
samples/standard-ml/Foo.sml
Normal file
75
samples/standard-ml/Foo.sml
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
structure LazyBase:> LAZY_BASE =
|
||||
struct
|
||||
type 'a lazy = unit -> 'a
|
||||
|
||||
exception Undefined
|
||||
|
||||
fun delay f = f
|
||||
|
||||
fun force f = f()
|
||||
|
||||
val undefined = fn () => raise Undefined
|
||||
end
|
||||
|
||||
structure LazyMemoBase:> LAZY_BASE =
|
||||
struct
|
||||
|
||||
datatype 'a susp = NotYet of unit -> 'a
|
||||
| Done of 'a
|
||||
|
||||
type 'a lazy = unit -> 'a susp ref
|
||||
|
||||
exception Undefined
|
||||
|
||||
fun delay f =
|
||||
let
|
||||
val r = ref (NotYet f)
|
||||
in
|
||||
fn () => r
|
||||
end
|
||||
|
||||
fun force f =
|
||||
case f() of
|
||||
ref (Done x) => x
|
||||
| r as ref (NotYet f') =>
|
||||
let
|
||||
val a = f'()
|
||||
in
|
||||
r := Done a
|
||||
; a
|
||||
end
|
||||
|
||||
val undefined = fn () => raise Undefined
|
||||
end
|
||||
|
||||
functor LazyFn(B: LAZY_BASE): LAZY' =
|
||||
struct
|
||||
|
||||
open B
|
||||
|
||||
fun inject x = delay (fn () => x)
|
||||
|
||||
fun isUndefined x =
|
||||
(ignore (force x)
|
||||
; false)
|
||||
handle Undefined => true
|
||||
|
||||
fun toString f x = if isUndefined x then "_|_" else f (force x)
|
||||
|
||||
fun eqBy p (x,y) = p(force x,force y)
|
||||
fun eq (x,y) = eqBy op= (x,y)
|
||||
fun compare p (x,y) = p(force x,force y)
|
||||
|
||||
structure Ops =
|
||||
struct
|
||||
val ! = force
|
||||
val ? = inject
|
||||
end
|
||||
|
||||
fun map f x = delay (fn () => f (force x))
|
||||
|
||||
end
|
||||
|
||||
structure Lazy' = LazyFn(LazyBase)
|
||||
structure LazyMemo = LazyFn(LazyMemoBase)
|
||||
Reference in New Issue
Block a user