Added example Markov

Example Markov : simulation of a Markov chain.

Use of a Random module, external call to C rand() function.
This commit is contained in:
Gwenaël Delaval 2015-09-17 11:17:29 +02:00
parent 61f043856a
commit d5ee87b7ed
8 changed files with 98 additions and 0 deletions

21
examples/random/README Normal file
View file

@ -0,0 +1,21 @@
Simple Markov chain simulation by external call to random() C function :
- random.epi : Heptagon interface containing the declaration of
random() function
- random.h, random.c : "glue" between actual random() C function and
function calls generated by Heptagon
- markov.ept : contains node "process", simulating a two-state Markov
chain, with probability p (constant) at each step to go from one
state to the other
Function "random" and node "process" are declared unsafe to avoid
optimization removing calls to "random" ("random" has side effects).
Compilation and simulation :
heptc random.epi
heptc -target c -s process -hepts markov.ept
gcc -I . -I markov_c -I /usr/local/lib/heptagon/c random.c markov_c/_main.c markov_c/markov.c markov_c/markov_types.c -o markov
hepts -mod Markov -node process -exec ./markov

View file

@ -0,0 +1,20 @@
open Random
const p : float = 0.3
unsafe node process() = (o:bool)
let
automaton
state A
var c : bool;
do o = false; c = random() <. p;
until c then B
| not c then C
state B
var c : bool;
do o = true; c = random() <. p
until c then A
state C
do o = false
end
tel

15
examples/random/mathext.c Normal file
View file

@ -0,0 +1,15 @@
#include <stdlib.h>
#include "mathext.h"
void Mathext__power_step(float x, int n, Mathext__power_out *o) {
float r;
int i;
r = 1.0;
for (i = 1; i <= n; i++) {
r = r * x;
}
o->y = r;
}

View file

@ -0,0 +1,3 @@
(* output : y = x^n *)
val fun power(x:float; n:int) = (y:float)

14
examples/random/mathext.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef MATHEXT_H
#define MATHEXT_H
/* Example of a combinatorial function */
typedef struct Mathext__power_out {
float y;
} Mathext__power_out;
void Mathext__power_step(float x, int n, Mathext__power_out *o);
#endif

8
examples/random/random.c Normal file
View file

@ -0,0 +1,8 @@
#include <stdlib.h>
#include "random.h"
void Random__random_step(Random__random_out *o) {
o->z = ((double)random())/((double)RAND_MAX);
}

View file

@ -0,0 +1,3 @@
(* output : random float in interval [0,1] *)
unsafe val fun random() = (z:float)

14
examples/random/random.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef RANDOM_H
#define RANDOM_H
/* Example of a combinatorial function */
typedef struct Random__random_out {
float z;
} Random__random_out;
void Random__random_step(Random__random_out *o);
#endif