Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.haskell > #342 > unrolled thread

How(approaches) to implement implementing lazy evaluation in the C (Imperative Programming)

Started byChethan U <evolutionmanager123@gmail.com>
First post2015-09-18 07:37 -0700
Last post2015-11-07 08:17 -0800
Articles 5 — 5 participants

Back to article view | Back to comp.lang.haskell


Contents

  How(approaches) to implement implementing lazy evaluation in the C (Imperative Programming) Chethan U <evolutionmanager123@gmail.com> - 2015-09-18 07:37 -0700
    Re: How(approaches) to implement implementing lazy evaluation in the C (Imperative Programming) fmassei@gmail.com - 2015-09-19 11:35 -0700
      Re: How(approaches) to implement implementing lazy evaluation in the C (Imperative Programming) Unknown <dog@gmail.com> - 2015-10-17 06:38 +0000
    Re: How(approaches) to implement implementing lazy evaluation in the C (Imperative Programming) Steven Hystad <steven.hystad@lunarinfrastructure.net> - 2015-09-19 23:50 -0600
    Re: How(approaches) to implement implementing lazy evaluation in the C (Imperative Programming) He-chien Tsai <depot051@gmail.com> - 2015-11-07 08:17 -0800

#342 — How(approaches) to implement implementing lazy evaluation in the C (Imperative Programming)

FromChethan U <evolutionmanager123@gmail.com>
Date2015-09-18 07:37 -0700
SubjectHow(approaches) to implement implementing lazy evaluation in the C (Imperative Programming)
Message-ID<07c4a873-63a7-4a3a-9318-d0baf50f099f@googlegroups.com>
Hello Sir,

How to implement implementing lazy evaluation in the C (Imperative Programming)?

Lazy evaluation is one of the key features in functional programming languages such as Haskell. This feature allows the programmers to work on infinite series quite easily.  Functional programming languages work at higher abstraction focusing on what needs to be done instead of on how it can be done.

Imperative programming languages focus more on how a goal can be achieved and hence work at a lower level of abstraction.  Therefore, it is natural to ask how such a feature can be implemented in imperative programming languages such as C.

What are the Various Approaches of implementing lazy evaluation in the C programming language.

What are the Constructs required for implementing lazy evaluation logic
 'C' programming language support for the required constructs
Thanks Sir

[toc] | [next] | [standalone]


#343

Fromfmassei@gmail.com
Date2015-09-19 11:35 -0700
Message-ID<5efd887b-e5e8-48f5-bab6-e695c425909a@googlegroups.com>
In reply to#342
On Friday, September 18, 2015 at 4:37:14 PM UTC+2, Nandhan wrote:
> Hello Sir,
> 
> How to implement implementing lazy evaluation in the C (Imperative
> Programming)?
> 
> Lazy evaluation is one of the key features in functional programming
> languages such as Haskell. This feature allows the programmers to work on
> infinite series quite easily.  Functional programming languages work at
> higher abstraction focusing on what needs to be done instead of on how it
> can be done.
> 
> Imperative programming languages focus more on how a goal can be achieved
> and hence work at a lower level of abstraction.  Therefore, it is natural
> to ask how such a feature can be implemented in imperative programming
> languages such as C.
> 
> What are the Various Approaches of implementing lazy evaluation in the C
> programming language.
> 
> What are the Constructs required for implementing lazy evaluation logic
>  'C' programming language support for the required constructs
> Thanks Sir
>

(First of all, how is this related to Haskell? Wouldn't comp.theory or
comp.lang.c be better NGs for this question?)

I will read you question as "is there a fast/quick way to implement a sort
of lazy evaluation in C?", because if you're searching a more general
answer, like "how does a compiler of functional language work?", well,
you're asking about more than half of the whole computer technology theory,
and I will just suggest you to start reading.

In C I would probably never even try to do it, but if it's just for having
some sort of fun, I'd go by implementing a coroutine mechanism and
generators on top. Both of them are somehow tricky but not difficult to
write, and sometimes they can even be a reasonable solution for a certain
set of problems.

Ciao!

[toc] | [prev] | [next] | [standalone]


#347

FromUnknown <dog@gmail.com>
Date2015-10-17 06:38 +0000
Message-ID<pan.2015.10.17.06.44.09@gmail.com>
In reply to#343
On Sat, 19 Sep 2015 11:35:06 -0700, fmassei wrote:

> On Friday, September 18, 2015 at 4:37:14 PM UTC+2, Nandhan wrote:
>> Hello Sir,
>> 
>> How to implement implementing lazy evaluation in the C (Imperative
>> Programming)?
>> 
>> Lazy evaluation is one of the key features in functional programming
>> languages such as Haskell. This feature allows the programmers to work
>> on infinite series quite easily.  Functional programming languages work
>> at higher abstraction focusing on what needs to be done instead of on
>> how it can be done.
>> 
>> Imperative programming languages focus more on how a goal can be
>> achieved and hence work at a lower level of abstraction.  Therefore, it
>> is natural to ask how such a feature can be implemented in imperative
>> programming languages such as C.
>> 
>> What are the Various Approaches of implementing lazy evaluation in the
>> C programming language.
>> 
>> What are the Constructs required for implementing lazy evaluation logic
>>  'C' programming language support for the required constructs
>> Thanks Sir
>>
>>
> (First of all, how is this related to Haskell? Wouldn't comp.theory or
> comp.lang.c be better NGs for this question?)
> 
> I will read you question as "is there a fast/quick way to implement a
> sort of lazy evaluation in C?", because if you're searching a more
> general answer, like "how does a compiler of functional language work?",
> well, you're asking about more than half of the whole computer
> technology theory, and I will just suggest you to start reading.
> 
> In C I would probably never even try to do it, but if it's just for
> having some sort of fun, I'd go by implementing a coroutine mechanism
> and generators on top. Both of them are somehow tricky but not difficult
> to write, and sometimes they can even be a reasonable solution for a
> certain set of problems.
> 
> Ciao!

It seems to me that lazy evaluation is not the big advantage of haskell.
A relevant extract from my:
Newsgroups: comp.lang.oberon
Subject: Functional programming style for Oberon?
  is:
A most convincing argument, for me, for the functional style, is:--

   Let's create a list of all even numbers up to 100, and another list
   omitting the first five of them.

   The program written in Java.

   final int LIMIT=50;int[] a = new int[LIMIT];
   int[] b = new int[LIMIT - 5];
   for (int i=0;i < LIMIT;i++) {
       a[i] = (i+1)*2;
       if (i >=5) b[i - 5] = a[i];
   }

 The program written in ###

   let a = [2,4..100]
   let b = drop 5 a

   It is immediately clear that with ###, you can understand what's
   going on; whereas in Java, or any imperative language, you can barely
   tell what the code is supposed to do because you are overwhelmed with
   the low-level minutia. This effect increases as programs grow more
   complex. From this simplicity and abstraction flow most of ###'s
   advantages.
----------------------------- end of extract. --------------------
There's nothing new about this. Extracting from McCarthy [designed lisp
in 60s?]:-
These three procedures clearly share a common underlying pattern. They
   are  for  the  most  part identical, differing only in the name of the
   procedure, the function/job of a used to compute the term to be added,
   and the function that provides the next value of a. We could generate 
each
   of the procedures by filling in slots in the same template:
------ snip lisp code ---
   The presence of such a common pattern is strong evidence that there is
   a  useful  abstraction  waiting  to be brought to the surface. Indeed,
   mathematicians  long  ago identified the abstraction of summation of a
   series and invented ``sigma notation,''
   to express this concept. The power of sigma notation is that it allows
   mathematicians  to  deal  with  the concept of summation itself rather
   than  only  with  particular sums -- for example, to formulate general
   results about sums that are independent of the particular series being
   summed ...

[toc] | [prev] | [next] | [standalone]


#344

FromSteven Hystad <steven.hystad@lunarinfrastructure.net>
Date2015-09-19 23:50 -0600
Message-ID<1442728251.4630.153.camel@epic>
In reply to#342
On Fri, 2015-09-18 at 07:37 -0700, Chethan U wrote: 
> Hello Sir,
> 
> How to implement implementing lazy evaluation in the C (Imperative Programming)?
> 
> Lazy evaluation is one of the key features in functional programming languages such as Haskell. This feature allows the programmers to work on infinite series quite easily.  Functional programming languages work at higher abstraction focusing on what needs to be done instead of on how it can be done.
> 
> Imperative programming languages focus more on how a goal can be achieved and hence work at a lower level of abstraction.  Therefore, it is natural to ask how such a feature can be implemented in imperative programming languages such as C.
> 
> What are the Various Approaches of implementing lazy evaluation in the C programming language.
> 
> What are the Constructs required for implementing lazy evaluation logic
>  'C' programming language support for the required constructs
> Thanks Sir
You can handle lazy evaluation by simply packing a function pointer,
value, and bool into a struct. If !bool then evaluate function, else
value = function(); bool = true.

Simple. Right?

This gives you lazy evaluation, but failes to do two things that haskell
does for you. In haskell, or at least in GHC, evaluation is thread safe.
One thread can start evaluation, and another can wait for the value
without causing an error. Haskell also has garbage collection. When GHC
decides that a thunk (i.e. the function pointer) will never be evaluated
then GHC can GC the thunk and possibly the data that the thunk depends
on.

That's not so easy.

If you want an easy way to have all the lazy functional programing
advantages of haskell and also want all the speed of C, then maybe try
using GHC and GCC together. .c files can be included in a GHC project
and GHC will automatically forward .c files to GCC. GHC can also handle
linking .o files made by GCC.

So if you can't use GHC, then gook luck.

[toc] | [prev] | [next] | [standalone]


#352

FromHe-chien Tsai <depot051@gmail.com>
Date2015-11-07 08:17 -0800
Message-ID<857df5b6-b692-4532-862e-f08d16158adf@googlegroups.com>
In reply to#342
Nandhan於 2015年9月18日星期五 UTC+8下午10時37分14秒寫道:
> Hello Sir,
> 
> How to implement implementing lazy evaluation in the C (Imperative Programming)?
> 
> Lazy evaluation is one of the key features in functional programming languages such as Haskell. This feature allows the programmers to work on infinite series quite easily.  Functional programming languages work at higher abstraction focusing on what needs to be done instead of on how it can be done.
> 
> Imperative programming languages focus more on how a goal can be achieved and hence work at a lower level of abstraction.  Therefore, it is natural to ask how such a feature can be implemented in imperative programming languages such as C.
> 
> What are the Various Approaches of implementing lazy evaluation in the C programming language.
> 
> What are the Constructs required for implementing lazy evaluation logic
>  'C' programming language support for the required constructs
> Thanks Sir

Frege is a Haskell for the JVM coded in Java which also comes with lazy evaluation.
You can read Frege's runtime. There areDelayed.java and a lot of FunX.java for evaluation
https://github.com/Frege/frege/tree/master/frege/runtime

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.haskell


csiph-web