From d5c7a5ec858563fd9d3658dc721602ac46d6e1c3 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 20 May 2021 08:31:19 +0100 Subject: Publishing new version --- ...2010-05-07-lazy-evaluation-with-standarn-ml.gmi | 102 +++++++++++++++++++++ gemfeed/atom.xml | 102 ++++++++++++++++++++- gemfeed/index.gmi | 1 + index.gmi | 1 + 4 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.gmi diff --git a/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.gmi b/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.gmi new file mode 100644 index 00000000..7269b523 --- /dev/null +++ b/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.gmi @@ -0,0 +1,102 @@ +# Lazy Evaluation with Standard ML + +``` + + _____|~~\_____ _____________ + _-~ \ | \ + _- | ) \ |__/ \ \ + _- ) | | | \ \ + _- | ) / |--| | | + __-_______________ /__/_______| |_________ +( |---- | | + `---------------'--\\\\ .`--' -Glyde- + `|||| +``` + +> Written by Paul Buetow 2010-05-07 + +In contrast to Haskell, Standard SML does not use lazy evaluation by default, but eager evaluation. + +=> https://en.wikipedia.org/wiki/Eager_evaluation +=> https://en.wikipedia.org/wiki/Lazy_evaluation + + +You can solve certain problems with lazy evaluation easier than with eager evaluation. For example, you might want to list the number Pi or another infinite list of something. With the help of lazy evaluation each element of the list is calculated when it is accessed first, but not earlier. + +## Emulating lazy evaluation in SML + +However, it is possible to emulate lazy evaluation in most eager evaluation languages. This is how it is done with Standard ML (with some play with an infinite list of natural number tuples filtering out 0 elements): + +``` +type ’a lazy = unit -> ’a; + +fun force (f:’a lazy) = f (); +fun delay x = (fn () => x) : ’a lazy; + +datatype ’a sequ = NIL | CONS of ’a * ’a sequ lazy; + +fun first 0 s = [] + | first n NIL = [] + | first n (CONS (i,r)) = i :: first (n-1) (force r); + +fun filters p NIL = NIL + | filters p (CONS (x,r)) = + if p x + then CONS (x, fn () => filters p (force r)) + else + filters p (force r); + +fun nat_pairs () = + let + fun from_pair (x,0) = + CONS ((x,0), fn () => from_pair (0,x+1)) + | from_pair (up,dn) = + CONS ((up,dn), fn () => from_pair (up+1,dn-1)) + in from_pair (0,0) + end; + +(* Test +val test = first 10 (nat_pairs ()) +*) + +fun nat_pairs_not_null () = + filters (fn (x,y) => x > 0 andalso y > 0) (nat_pairs ()); + +(* Test +val test = first 10 (nat_pairs_not_null ()); +*) +``` + +=> http://smlnj.org/ + +## Real laziness with Haskell + +As Haskell already uses lazy evaluation by default, there is no need to construct a new data type. Lists in Haskell are lazy by default. You will notice that the code is also much shorter and easier to understand than the SML version because of that. + +``` +{- Just to make it look like the ML example -} +first = take +filters = filter + +{- Implementation -} +nat_pairs = from_pair 0 0 + where + from_pair x 0 = [x,0] : from_pair 0 (x+1) + from_pair up dn = [up,dn] : from_pair (up+1) (dn-1) + +{- Test: +first 10 nat_pairs +-} + +nat_pairs_not_null = filters (\[x,y] -> x > 0 && y > 0) nat_pairs + +{- Test: +first 10 nat_pairs_not_null +-} +``` + +=> http://www.haskell.org/ + +E-Mail me your thoughts at comments@mx.buetow.org! + +=> ../ Go back to the main site diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml index 9a4e3abc..46cf28a5 100644 --- a/gemfeed/atom.xml +++ b/gemfeed/atom.xml @@ -1,6 +1,6 @@ - 2021-05-19T22:03:14+01:00 + 2021-05-20T08:26:57+01:00 buetow.org feed Having fun with computers! @@ -2192,6 +2192,106 @@ BB

May the source be with you

You can find all of this on the GitHub page. There is also an "examples" folders containing some Fype scripts!

https://github.com/snonux/fype
+

E-Mail me your thoughts at comments@mx.buetow.org!

+ + + + + Lazy Evaluation with Standard ML + + gemini://buetow.org/gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.gmi + 2010-05-07T08:17:59+01:00 + + Paul Buetow + comments@mx.buetow.org + + In contrast to Haskell, Standard SML does not use lazy evaluation by default, but strict evaluation. . .....to read on please visit my site. + +
+

Lazy Evaluation with Standard ML

+
+
+      _____|~~\_____      _____________
+  _-~               \    |    \
+  _-    | )     \    |__/   \   \
+  _-         )   |   |  |     \  \
+  _-    | )     /    |--|      |  |
+ __-_______________ /__/_______|  |_________
+(                |----         |  |
+ `---------------'--\\\\      .`--'          -Glyde-
+                              `||||
+
+

Written by Paul Buetow 2010-05-07

+

In contrast to Haskell, Standard SML does not use lazy evaluation by default, but eager evaluation.

+https://en.wikipedia.org/wiki/Eager_evaluation
+https://en.wikipedia.org/wiki/Lazy_evaluation
+

You can solve certain problems with lazy evaluation easier than with eager evaluation. For example, you might want to list the number Pi or another infinite list of something. With the help of lazy evaluation each element of the list is calculated when it is accessed first, but not earlier.

+

Emulating lazy evaluation in SML

+

However, it is possible to emulate lazy evaluation in most eager evaluation languages. This is how it is done with Standard ML (with some play with an infinite list of natural number tuples filtering out 0 elements):

+
+type ’a lazy = unit -> ’a;
+
+fun force (f:’a lazy) = f ();
+fun delay x = (fn () => x) : ’a lazy;
+
+datatype ’a sequ = NIL | CONS of ’a * ’a sequ lazy;
+
+fun first 0 s = []
+  | first n NIL = []
+  | first n (CONS (i,r)) = i :: first (n-1) (force r);
+
+fun filters p NIL = NIL
+  | filters p (CONS (x,r)) =
+      if p x
+          then CONS (x, fn () => filters p (force r))
+      else
+          filters p (force r);
+
+fun nat_pairs () =
+    let
+        fun from_pair (x,0) =
+              CONS ((x,0), fn () => from_pair (0,x+1))
+          | from_pair (up,dn) =
+              CONS ((up,dn), fn () => from_pair (up+1,dn-1))
+        in from_pair (0,0)
+    end;
+
+(* Test
+val test = first 10 (nat_pairs ())
+*)
+
+fun nat_pairs_not_null () =
+    filters (fn (x,y) => x > 0 andalso y > 0) (nat_pairs ());
+
+(* Test
+val test = first 10 (nat_pairs_not_null ());
+*)
+
+http://smlnj.org/
+

Real laziness with Haskell

+

As Haskell already uses lazy evaluation by default, there is no need to construct a new data type. Lists in Haskell are lazy by default. You will notice that the code is also much shorter and easier to understand than the SML version because of that.

+
+{- Just to make it look like the ML example -}
+first = take
+filters = filter
+
+{- Implementation -}
+nat_pairs = from_pair 0 0
+    where
+        from_pair x 0 = [x,0] : from_pair 0 (x+1)
+        from_pair up dn = [up,dn] : from_pair (up+1) (dn-1)
+
+{- Test:
+first 10 nat_pairs
+-}
+
+nat_pairs_not_null = filters (\[x,y] -> x > 0 && y > 0) nat_pairs
+
+{- Test:
+first 10 nat_pairs_not_null
+-}
+
+http://www.haskell.org/

E-Mail me your thoughts at comments@mx.buetow.org!

diff --git a/gemfeed/index.gmi b/gemfeed/index.gmi index 8880df3e..ff764953 100644 --- a/gemfeed/index.gmi +++ b/gemfeed/index.gmi @@ -15,5 +15,6 @@ => ./2014-03-24-the-fibonacci.pl.c-polyglot.gmi 2014-03-24 - The fibonacci.pl.c Polyglot => ./2011-05-07-perl-daemon-service-framework.gmi 2011-05-07 - Perl Daemon (Service Framework) => ./2010-05-09-the-fype-programming-language.gmi 2010-05-09 - The Fype Programming Language +=> ./2010-05-07-lazy-evaluation-with-standarn-ml.gmi 2010-05-07 - Lazy Evaluation with Standard ML => ./2010-04-09-standard-ml-and-haskell.gmi 2010-04-09 - Standard ML and Haskell => ./2008-06-26-perl-poetry.gmi 2008-06-26 - Perl Poetry diff --git a/index.gmi b/index.gmi index 69281151..106299cd 100644 --- a/index.gmi +++ b/index.gmi @@ -67,5 +67,6 @@ I have switched blog software multiple times. I might be back filling some of th => ./gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi 2014-03-24 - The fibonacci.pl.c Polyglot => ./gemfeed/2011-05-07-perl-daemon-service-framework.gmi 2011-05-07 - Perl Daemon (Service Framework) => ./gemfeed/2010-05-09-the-fype-programming-language.gmi 2010-05-09 - The Fype Programming Language +=> ./gemfeed/2010-05-07-lazy-evaluation-with-standarn-ml.gmi 2010-05-07 - Lazy Evaluation with Standard ML => ./gemfeed/2010-04-09-standard-ml-and-haskell.gmi 2010-04-09 - Standard ML and Haskell => ./gemfeed/2008-06-26-perl-poetry.gmi 2008-06-26 - Perl Poetry -- cgit v1.2.3