Skip to content

Instantly share code, notes, and snippets.

@inariksit
inariksit / Dockerfile
Last active January 29, 2024 13:45
Dockerfile for using pgf2 library in Haskell
FROM haskell:9.2.8
RUN apt-get update \
&& apt-get install -y \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install the C runtime of GF from source
WORKDIR /tmp
ARG GF_VERSION=c2182274df0a0f730b4d4a41ea4a537cdb388ecd
@inariksit
inariksit / Metavariables.hs
Last active March 1, 2024 12:10
To handle metavariables in GF, don't use `fg` and `gf`
import PGF
import Debug.Trace (trace)
import Data.Foldable (asum)
import Test
getMeta :: Expr -> Int
getMeta expr = case go expr of
Just i -> trace (unwords ["expression", showExpr [] expr, "contains the metavariable", show i, ", returning it"]) i
Nothing -> trace (unwords ["expression", showExpr [] expr, "doesn't contain a metavariable, defaulting to 42"]) 42
where
@inariksit
inariksit / FoodsList.gf
Created March 24, 2023 14:13
How to use GF lists in Foods grammar
abstract FoodsList = {
flags coding = utf8 ;
flags startcat = Comment ;
cat
Comment ; Item ; Kind ; Quality ;
[Comment]{2} ; -- List of Comments
fun
-- BaseComment and ConsComment are added automatically
-- We only have to define a function from list to a single comment
abstract Worker = Numeral -- We extend the Numeral abstract syntax to get Digits
** {
flags startcat = Clause ;
cat
Kind ;
Clause ;
-- The categories Int, Float and String are present in all grammars
-- The categories Dig and Digits come from Numeral.
@inariksit
inariksit / OmitParticles.gf
Created February 16, 2023 23:32
Accepting variants in omitting particles
abstract OmitParticles =
Grammar [ -- "Embedded DSL" on top of some RGL categories
N, CN, NP, UseN, MassNP
, V2
, S, Cl, Temp, Pol, Tense, Ant
, UseCl, PPos, PNeg, TTAnt, TPres, TPast, ASimul
]
, Lexicon -- to get some lexicon
** {
flags startcat = S ;
@inariksit
inariksit / Fields.gf
Last active March 4, 2022 03:00
GF linrefs and the significance of the s field
abstract Fields = {
flags startcat = S ;
cat
S ; CN ; Adv ;
fun
ThereIs : CN -> S ; -- there is a house
ThereAre : CN -> S ; -- there are houses
ModCN : CN -> Adv -> CN ; -- house on a hill
house_CN : CN ;
@inariksit
inariksit / Tables.gf
Last active September 29, 2023 14:54
Explanation on table syntax in GF for beginners
resource Tables = {
param
Number = Sg | Pl ;
Gender = Fem | Masc ;
oper
-- Single tables, no nesting
numTable : Number => Str = table {
Sg => "singular form" ;
Pl => "plural form"
@inariksit
inariksit / Conjunctions.gf
Created January 27, 2022 23:55
Lists in the PGF API
abstract Conjunctions = {
cat
S ; NP ; AP ; Conj ;
[NP]{2} ;
[AP]{2} ;
fun
Pred : NP -> AP -> S ; -- the pizza is Italian
ConjNP : Conj -> [NP] -> NP ; -- pizza, beer and sandwich
@inariksit
inariksit / TestConj.gf
Last active March 31, 2022 02:54
Different strategies of getting around the lack of ConjUtt. The first pair (TestConj, TestConjEng) adds a list instance of our own custom type, with lincat Utt. The second pair (TestConjHack, TestConjHackEng) shows two different workarounds, if you can't be bothered with adding a list instance for your cats. The hacky version relies on the fact …
abstract TestConj = {
-- Same as the hacky version until the list instance
flags startcat = MyUtt ;
cat
MyUtt ; MyCl ;
fun
testUtt : MyUtt ;
testCl : MyCl ;
-- Mini grammar for demo purposes, only has one sentence
abstract PeopleThink = {
cat
S ;
fun
IWantPeopleToThink : S ;
}