Representation of conversion systems in sml

I need some help writing code that:

Given two functions, say f1 and f2, and an initial input of i1 for f1, I will feed i1 to f1 and any return it returns, I feed to f2 and whatever f2 returns, I feed to f1, and so on. ...

So it looks like this: funny pair (m1, m2, i1) = ...

m1 and m2 are here in fact the final state transformers, such that m1 = (state, f1). the state here is the state in which we have i1. f1 takes (state, input) and returns output (next state, oput), after which oput is fed to m1, etc.

For clarity, this is a converter system. This means that two FSTs with additional inputs and outputs can be executed in parallel, with the output of each serving as an input for the other.

It is supposed to return, say, a list of outputted outputs.

To help, I've already written a function run that takes fst m and a list of inputs, gives a list of the outputs received when running m on the inputs.

However, my head turned over when trying to write this function, because I kind of entered an infinite loop, and my code was incredibly long as long as it can be done easily using a run through my helper function.

Any ideas?

+3


source to share


2 answers


Thanks for the push! Your ideas are correct.

So this is typically how it works: You are actually using lazy evaluation. Here we are working with our own lazy structure anyway (you can create your own structures in ml).

Using the run function mentioned earlier, I can make a function that runs m1 on i1 and then calls it in the mutually recursive jest beneth it function. Finally, I'll call the function all together! This is how it looks like:



  fun pair(m1, m2, i1)=
    let
      fun p1 () = run (m1) (delay(fn() => Gen(i1,p2())))
      and p2 () = run (m2) (p1())
    in
      p1()
    end

      

Here delay and Gen are part of my structure. Gen represents a stream with i1 as the first element and p2 () as the rest. delay takes a function and usually represents a piece of laziness in this implementation. Using mutually recursive functions (functions that call each other are activated by typing "and" instead of "fun" as shown above), I could go back and forth and so on.

There is another easier way to implement this, believe it or not, but this is for a start. If you can improve this answer (or another solution) in any way, feel free to share it! Thanks you

+1


source


Interest Ask. I think you have to use lazy evaluation somehow. I'm not sure how to use it as I've never done it and I have to admit that I didn't really dig into it, but after a short "googleing" I think I can provide some useful links.

So, I first assumed:

fun pairFirst f1 f2 i1 =
    fn () => pairFirst f2 f1 (f1 i1)

      

as you would do in LISP, but it clearly doesn't work in SML. So I was looking for her.

First, I found out that SML actually supports lazy evaluation: http://www.cs.cmu.edu/~rwh/introsml/core/lazydata.htm

Quote: "First, the lazy SML / NJ evaluation engines should be activated by evaluating the following declarations:

Compiler.Control.Lazy.enabled := true; open Lazy;

"

I tried it but that didn't work either, so I googled some more: https://github.com/trptcolin/euler-sml/blob/master/lazy_utils.sml

Quote: "(* laziest details are taken from programming in standard ML, Robert Harper * notable exception: Compiler.Control.Lazy.enabled moved to Control.lazysml *)

Control.lazysml := true; open Lazy;

"

From the content of these two links, I built my second guess:



Control.lazysml := true;
open Lazy;

fun lazy pair (f1: 'a -> 'a, f2: 'a -> 'a, i1: 'a) : 'a susp  =
    pair (f2, f1, (f1 i1))

      

SML somehow "swallows" it:

- [opening /home/spela/test.sml]
val it = () : unit
opening Lazy

  datatype 'a susp = $ of 'a
val pair = fn : ('a -> 'a) * ('a -> 'a) * 'a -> 'a susp
val pair_ = fn : ('a -> 'a) * ('a -> 'a) * 'a -> 'a
val it = () : unit

      

It works? I have no idea:)

- pair ((fn x => x + 1), (fn y => y - 1), 1);
val it = $$ : int susp

      

I have not read these links, but I also found an article that I also did not read, but I believe it provides the answers you are looking for:

http://www.cs.mcgill.ca/~bpientka/courses/cs302-fall10/handouts/lazy-hof.pdf

I believe these links may answer your questions.

If anyone is familiar with this topic, PLEASE answer the question, I think it would be interesting for many of us.

Best regards, Ε pela

+1


source







All Articles