def FAToGrail(f, fa):
    """Saves a finite automatom definition to an open file using Grail format

    :arg f: opended file
    :type f: file
    :arg fa: the FA
    :type fa: FA"""
    for s in fa.initialSet():
        f.write('(START) |- %s\n' % s)
    for s in range(len(fa.States)):
        if s in fa.delta:
            for a in list(fa.delta[s].keys()):
                if isinstance(fa.delta[s][a], set):
                    for s1 in fa.delta[s][a]:
                        f.write('%s %s %s\n' % (s, a, s1))
                else:
                    f.write('%s %s %s\n' % (s, a, fa.delta[s][a]))
    for s in fa.Final:
        f.write('%s -| (FINAL)\n' % s)
