john skaller | 28 Apr 03:26 2015

function product

This works:

fun \times[u1,u2,r1,r2] (f1:u1->r1,f2:u2->r2) : u1 * u2 -> r1 * r2 => 
    fun (x1:u1,x2:u2) => f1 x1, f2 x2;

fun \otimes[D1,C1,D,C] (f1:(D1->C1), f:(D->C)) : (D1 ** D) -> (C1 ** C) =>
  fun (a:D1 ** D) : C1 ** C => match a with
   | (x1,,x) => f1 x1,,f x 

fun i2s (x:int)=>x.str+"!";
fun i2i (x:int)=>x+1;

var fs = i2s,i2i,i2s,i2s;
var r = fs.0 \otimes fs.1 \otimes (fs.2 \times fs.3);

println$ r (1,2,3,4);

But it isn't pretty. The final \times is requires as the ground case
(and need parens because of grammar issues, \otimes is right assoc).

However \prod (f,g,h) still can't be defined, i have ravel (f,g,h) in the 
library for up to 5 cases.

john skaller

(Continue reading)

john skaller | 26 Apr 01:16 2015


I'm changing the display rules for mathmode.

First, { } will not be displayed in mathmode.
Use \{ \} if you want to see something.

Second, a Felix identifier will usually emit


which will cause it to be typeset in teletype font, similar to the usual
program text font I hope.

Next, I am thinking instead of the above, if you specify a font operator:

	\mathcal { ... }

that overrides \mathtt. In fact I think a stack of fonts will be maintained.

Fourth, i think I will make a single character identifier an exception
and typeset that using \mathit.

This means \mathit and \mathtt probably should NOT be used as operators!

In general formulae like

	\mathcal X
	\mathcal {x + y}
	\mathcal (x + y)

etc affect typesetting and are ALSO Felix functions. So take care
(Continue reading)

john skaller | 25 Apr 01:20 2015

Re: [felix] stumped

On 25/04/2015, at 3:44 AM, Shayne Fletcher wrote:

> On Fri, Apr 24, 2015 at 1:07 PM, john skaller <skaller <at>> wrote:
> fun ravel[u1,u2,r1,r2] (f1:u1->r1,f2:u2->r2) : u1 * u2 -> r1 * r2
> ‚ÄčNeat function.

But I want

	ravel[D1,C1,D2,C2,D3,C3,....D9,C9] (f1:D1->D2, f2:D2->C2 ... f9:D9->C9): D1 * D2 *..D9 -> C1 *C2 .. C9;

I can do

	str[D1,D2, ... D9] (p1:D1, p2:D2,.. p9:D9):string 

given str:T->string which should be harder, since it involves polymorphic recursion.

However the latter is just a string fold of the individual components. 
However I don't think Ocaml can do this using any method,
and I'm not sure Haskell can either (in fact Haskell doesn't
even HAVE tuples AFAIK)

Felix and C++ can do it.

However I can't generalise it, even if you want a bool fold of some property,
i.e. fix the return type.

The ravel doesn't need any typeclass, it can be done with a purely
(Continue reading)

john skaller | 24 Apr 19:07 2015


I am stumped at the moment defining certain operators on tuples.

If you look at src/lib/std/datatype/tuple.flx you will see there is general way
to apply a function

	T -> K
	T * T -> K

to a tuple, where K is a constant type. For example 

	T -> string         // Str
	T * T -> bool  // Eq

are defined. See also that we can do

	F T0, F T1 ... F TN -> F (T0, T1 .. TN)

in some cases, such as in src/lib/std/algebra/set.flx we can "almost" define

	\otimes (set_form[T0], set_form[T1],.. set_form[TN] )

I say almost because actually we can do 

var x = { x: int | x > 0} \otimes { x: double | x > 0.0} \otimes { x:string | x > "H" };
println$ (24,3.2,"Hello") \in x;

but this is right associative. What I REALLY want to define is:

	\otimes (s1,s2,s3);

(Continue reading)

john skaller | 24 Apr 09:56 2015

Maths DSSL

The below will give cute display in Felix code, if displayed by flx_web.
The "font" operators are just ordinary functions .. so you have to actually
define them if you want stuff to work!

// ordinary and mathmode
var x = X; // ordinary
var x = \(X\); // mathmode

// these are all TeX commands so x ^ \font y
// typesets correctly: note \sin is an OP symbol
// so x ^ \sin y fails, and x ^ \Gamma y works
// since \Gamma is an ORD, but the y is not raised.

// font operators
var x = \(\mathrm X\); // mathrm
var x = \(\mathit X\); // mathit
var x = \(\mathfrak X\); // mathfrak
var x = \(\mathbb X\); // mathbb
var x = \(\mathbf X\); // mathbf
var x = \(\mathcal X\); // mathcal
var x = \(\mathscr X\); // mathscr
var x = \(\mathsf X\); // mathscr
var x = \(\mathtt X\); // mathtt

// modifiers
var x = \(\acute X\); // acute
var x = \(\check X\); // check

var x = \( x ^ \binom {\mathtt x} {\mathtt y} \);
(Continue reading)

john skaller | 3 Apr 20:18 2015

negated comparisons

Next commit, you can write:

	x not < y


	\( x \not\less y \)

Both not and \not mean the same, however not is printed "not" as written,
whereas \not IN MATH MODE ONLY will cause the following TeX operator
to be negated by strikeout.

We need this because whilst TeX defines


it does not define \nsubset. You have to write \not\subset.
Note \not\subseteq will work as well, however, the special
form \nsubseteq is provided to make the struck out symbol
more beautiful.

Do not confuse \not with \lnot. The latter is boolean negation operator,
it applies to expressions, and it has looks like logic not symbol,
this has a different precedence than "not" applied to an expression.

Do not write \not expr, it will neither work, nor typeset correctly.
use \not to cancel comparison operators.

john skaller
(Continue reading)

john skaller | 30 Mar 13:01 2015

Re: [felix] fix patterns

On 30/03/2015, at 7:18 PM, Martin DeMello wrote:
>> This won't work as you might expect because in that pattern NIL is a variable.
>> Easy to miss. You have to write
>>        | A (_, #NIL) => ..
>> to tell the parser the NIL is a type constructor.
> Would it be hard to make this an actual warning, viz
> warning: variable NIL has same name as constructor. did you mean #NIL?

I don't know, it could be quite hard. It would mean at least doing a lookup
on EVERY variable name used. The problem is that the "desugaring"
of pattern matching is done long before it is possible to do any lookup.
So by the time the compiler comes to bind the code, it has no idea
it's dealing with a pattern match variable.

Pattern matches are basically macros. You can, in fact, invent your own.
It basically goes:

	if  (!_match_ctor_CTOR (arg)) goto next_case;
	var arg1, arg2 = _ctor_arg_CTOR (arg);

	// do handler

	goto endmatch;
(Continue reading)

john skaller | 30 Mar 04:14 2015

fix patterns

Using a nice tool, flx_batch_replace:

/usr/local/lib/felix/felix-latest/host/bin/flx_batch_replace -v src '.*\.(flx|fdoc)'
'fixup/src/${0}' '\?([A-Za-z][A-Za-z0-9_]*)' '\1'
cp -r fixup/src/* src

Be interested how to do that with standard unix tools.

I do note the inconsistent replacement notation ${group} for files,
but \group for strings. Internally these search and replace operations
are in fact similar. I put the ${group} in because its so hard to type \1
inside a string and get the quoting right. OTOH in bash ${1} is the first
command line argument so that needs to be quoted right too.

john skaller

Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now.
john skaller | 29 Mar 20:42 2015

review of brackets

Apart from in structs, unions, and function/procedure definitions,
here is a possibly incomplete catalogue:

	{ expr } --> function returning expr
	{ stmt; stmt; expr } -> function returning expr
	{ stmt; stmt; return expr; } -> function returning expr
	{ stmt; stmt; } --> procedure
	{ pattern : type | expr } --> set form

	( expr ) --> expr
	( expr, expr ) --> tuple
	(x=expr, y=expr) -> record
	struct {x = expr; y=expr; } -> record (deprecated)
        ( stmt; stmt; expr ) --> expr // means #{ stmt; stmt; expr }
	() --> unit tuple
	( var expr ) --> expr (eagerly evaluated, same as (let x = expr in x))

I am looking at some more short forms, such as


like C. It's not clear these will work, since they're ambiguous.
An alternative (from COBOL):

	if x perform stmt;
	while x perform stmt;

Note these are already allowed:

(Continue reading)

john skaller | 28 Mar 14:20 2015

Re: [felix] Set form

Sometimes it's hard to believe Felix is SOO dang good!
This works now:

var squares = \{ (?x,?y) : int * int | y == x * x \} ;
println$ (3,9) \in squares;

The general form is

	\{ pattern : type | predicate \}

A predicate is any expression evaluating to bool, which may use
the variables in the pattern match. We have to specify the type
of the pattern, and our patterns have ugly ? in them sometimes,
but otherwise this is similar to a mathematical set.

A set_form is a real object. Any object with a method

	has_elt: T -> bool

acts like a set courtesy of these definition in std/algebra/set.flx:

interface set_form[T] { has_elt: T -> bool; }
fun \in[T] (elt:T, s:set_form[T]) => s.has_elt elt;

If your object has other methods you can "cast" them away
with a coercion to meet the specification.

It's easy to define the union now:

	fun \cup[T] (A:set_form[T], B:set_form[T]) => \{ ?x : T | x \in A or x \in B \};
(Continue reading)

john skaller | 25 Mar 22:40 2015

Re: [felix] Design issue

On 26/03/2015, at 5:30 AM, Martin DeMello wrote:

>>> I believe this is where you went wrong - a set isn't a container with
>>> a membership operator, it's a container with a membership operator and
>>> the property that all elements are distinct.
>> How would you specify that property?
> You can't without dependent types, certainly. (I'm not even sure you
> can *with* dependent types; I know very little about them). In actual
> code you could always maintain the invariant by simply removing
> duplicates on all set-modifying operations.

I think you're missing the point.

	class Set[C,T] { virtual fun \in : T * C -> bool; }

defines a Set as a data type with a member operator.

Now, an array, even with duplicate values stored,
can satisfy the axiom, because there's no requirement
about duplication of elements in an array. 

Note there's also no requirement for an equality operator,
and no size. This is not your usual mathematical set.
I actually defined a Container as a Set with a length operator.

Consider a regexp: RE2 "(a|b)*abb". That's a set, using the obvious
rule that a string that matches the regexp is in the set.
(Continue reading)