- 
                Notifications
    You must be signed in to change notification settings 
- Fork 12
Add some sketches of Fathom’s core in Idris 2 #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            brendanzab
  wants to merge
  47
  commits into
  yeslogic:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
brendanzab:expriments/idris
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            47 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      5bc6398
              
                Add some sketches of Fathom’s core in Idris 2
              
              
                brendanzab 02bace9
              
                Experiment with custom IR universes
              
              
                brendanzab 315acb1
              
                Spelling fixes
              
              
                brendanzab 6d0dc18
              
                Begin reproducing Opentype difficulties
              
              
                brendanzab 6be0640
              
                Add rlwrap to devShell
              
              
                brendanzab 711c66a
              
                Adjust multiplicities on singleton types
              
              
                brendanzab 6487cea
              
                Switch to using records for singletons
              
              
                brendanzab 9f0cd55
              
                Move refinement and singleton types into modules
              
              
                brendanzab 6511e44
              
                Add some more notes about the Idris sketch
              
              
                brendanzab 01a2852
              
                Add name hints for streams
              
              
                brendanzab 93055a4
              
                Implement a simpler singleton type
              
              
                brendanzab 505f286
              
                Simplify the encoder type
              
              
                brendanzab 6e8660a
              
                Add support for do notation
              
              
                brendanzab 53d50c6
              
                Make Decode type match Source more closely
              
              
                brendanzab 7289176
              
                Try a different FormatOf representation
              
              
                brendanzab 9e15d99
              
                Add more type aliases for encoder/decoder pairs
              
              
                brendanzab e0c52d7
              
                Add more encoding targets
              
              
                brendanzab 305a0ce
              
                Various cleanups
              
              
                brendanzab 0d5fa66
              
                Experiment with bit and word definitions
              
              
                brendanzab 41832aa
              
                Add some more formats
              
              
                brendanzab 9bf5144
              
                Try out the opentype issue with indexed types
              
              
                brendanzab 7299eaa
              
                More work on styles of format description
              
              
                brendanzab 6ea96ed
              
                Define isomorphisms between Formats and FormatOfs
              
              
                brendanzab b5b4439
              
                Get OpenType examples to work for all approaches
              
              
                brendanzab ed44595
              
                Use the Subset type from the Idris base library
              
              
                brendanzab 11d58fc
              
                Add constructors for building non-indexed formats
              
              
                brendanzab ef54092
              
                Begin constructors for wrapper index formats
              
              
                brendanzab 098bbf7
              
                Try to construct OpenType things using wrappers
              
              
                brendanzab e961d7f
              
                Move experiments into a different module
              
              
                brendanzab acabfc1
              
                Make better use of namespaces
              
              
                brendanzab 152fb8a
              
                Forwar encode/decode functions via wrapper formats
              
              
                brendanzab aaa95c9
              
                Clean up some state passing with do notation
              
              
                brendanzab 8f5a720
              
                Rename ‘skip’ formats to ‘ignore’
              
              
                brendanzab 457f25f
              
                Move typeOf function
              
              
                brendanzab e129a83
              
                Ponder compilation a bit
              
              
                brendanzab 4e01ddc
              
                Encode/decode cleanups
              
              
                brendanzab 8c0dc84
              
                Add field syntax for inductive-recursive reps
              
              
                brendanzab 0d01ee2
              
                Move format descriptions under single path
              
              
                brendanzab 0f475d3
              
                Add pair formats
              
              
                brendanzab d11c5d4
              
                Enable applicative notation for decoders
              
              
                brendanzab a082638
              
                Add u32 formats
              
              
                brendanzab 1599762
              
                Play around with heterogeneous sequences
              
              
                brendanzab 1c37a1c
              
                Create a hetrogeneous repetition format with HVect
              
              
                brendanzab 9fbf359
              
                Rename to `hrepeat` to `tuple`
              
              
                brendanzab 348f7fd
              
                Move compiler experiments
              
              
                brendanzab 1cc1602
              
                Implement tuple in other format decription styles
              
              
                brendanzab bac5789
              
                Choice formats
              
              
                brendanzab File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| build | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Core language experiments in Idris 2 | ||
|  | ||
| Some sketches of Fathom’s core language using Idris as a logical framework. | ||
|  | ||
| ```command | ||
| $ idris2 --repl experiments/idris/fathom.ipkg | ||
| Main> :load "src/Playground.idr" | ||
| ``` | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package fathom | ||
| -- version = | ||
| -- authors = | ||
| -- maintainers = | ||
| -- license = | ||
| -- brief = | ||
| -- readme = | ||
| -- homepage = | ||
| -- sourceloc = | ||
| -- bugtracker = | ||
|  | ||
| -- packages to add to search path | ||
| -- depends = | ||
|  | ||
| -- modules to install | ||
| modules = Fathom | ||
| , Fathom.Base | ||
| , Fathom.Closed.IndexedInductive | ||
| , Fathom.Closed.InductiveRecursive | ||
| , Fathom.Closed.InductiveRecursiveCustom | ||
| , Fathom.Open.Record | ||
|  | ||
| -- main file (i.e. file to load at REPL) | ||
| -- main = | ||
|  | ||
| -- name of executable | ||
| -- executable = | ||
| -- opts = | ||
| sourcedir = "src" | ||
| builddir = "build" | ||
| outputdir = "build/exec" | ||
|  | ||
| -- script to run before building | ||
| -- prebuild = | ||
|  | ||
| -- script to run after building | ||
| -- postbuild = | ||
|  | ||
| -- script to run after building, before installing | ||
| -- preinstall = | ||
|  | ||
| -- script to run after installing | ||
| -- postinstall = | ||
|  | ||
| -- script to run before cleaning | ||
| -- preclean = | ||
|  | ||
| -- script to run after cleaning | ||
| -- postclean = | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ||| A sketch of core Fathom in Idris 2 | ||
|  | ||
| import public Fathom.Base | ||
| import public Fathom.Closed.IndexedInductive | ||
| import public Fathom.Closed.InductiveRecursive | ||
| import public Fathom.Closed.InductiveRecursiveCustom | ||
| import public Fathom.Open.Record | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| module Fathom.Base | ||
|  | ||
|  | ||
| import Data.Colist | ||
| import Data.List | ||
|  | ||
|  | ||
| ------------------ | ||
| -- USEFUL TYPES -- | ||
| ------------------ | ||
|  | ||
|  | ||
| ||| A value that is refined by a proposition. | ||
| ||| | ||
| ||| This is a bit like `(x : A ** B)`, but with the second element erased. | ||
| public export | ||
| record Refine (0 A : Type) (0 P : A -> Type) where | ||
| constructor MkRefine | ||
| ||| The wrapped value | ||
| value : A | ||
| ||| The proof of the proposition | ||
| 0 prf : P value | ||
|  | ||
| ||| Refine a value with a proposition | ||
| public export | ||
| refine : {0 A : Type} -> {0 P : A -> Type} -> (value : A) -> {auto 0 prf : P value} -> Refine A P | ||
| refine value {prf} = MkRefine { value, prf } | ||
|  | ||
|  | ||
| ||| Singleton types | ||
| ||| | ||
| ||| Inspired by [this type](https://agda.readthedocs.io/en/v2.5.4.1/language/with-abstraction.html#the-inspect-idiom) | ||
| ||| from the Agda docs. | ||
| public export | ||
| data Sing : {0 A : Type} -> (x : A) -> Type where | ||
| MkSing : {0 A : Type} -> {0 x : A} -> (y : A) -> {auto prf : x = y} -> Sing x | ||
|  | ||
| ||| Convert a singleton back to its underlying value | ||
| public export | ||
| value : {0 Val : Type} -> {0 x : Val} -> Sing x -> Val | ||
| value (MkSing y) = y | ||
|  | ||
|  | ||
| --------------------------- | ||
| -- ENCODER/DECODER PAIRS -- | ||
| --------------------------- | ||
|  | ||
| -- Inspiration taken from Narcissus: | ||
| -- | ||
| -- * [Narcissus: Correct-by-Construction Derivation of Decoders and Encoders from Binary Formats](https://dl.acm.org/doi/10.1145/3341686) | ||
| -- by Delaware et. al. | ||
| -- * [`Narcissus/Common/Specs.v`](https://github.com/mit-plv/fiat/blob/master/src/Narcissus/Common/Specs.v) | ||
| -- | ||
| -- TODO: Add support for [Narcissus-style stores](https://github.com/mit-plv/fiat/tree/master/src/Narcissus/Stores) | ||
|  | ||
| parameters (Source : Type, Target : Type) | ||
|  | ||
| ||| Decoders consume a _target value_ and produce either: | ||
| ||| | ||
| ||| - a _source value_ and _remaining target value_ | ||
| ||| - or nothing if in error occurred | ||
| ||| | ||
| ||| @ Source The type of source values (usually an in-memory data structure) | ||
| ||| @ Target The type of target values (usually a byte-stream) | ||
| public export | ||
| Decode : Type | ||
| Decode = Target -> Maybe (Source, Target) | ||
|  | ||
| ||| Encoders take a _source value_ and _remaining target value_ and produce either: | ||
| ||| | ||
| ||| - an _updated target value_ | ||
| ||| - or nothing if in error occurred | ||
| ||| | ||
| ||| @ Source The type of source values (usually an in-memory data structure) | ||
| ||| @ Target The type of target values (usually a byte-stream) | ||
| public export | ||
| Encode : Type | ||
| Encode = Source -> Target -> Maybe Target | ||
|  | ||
|  | ||
| ---------------------- | ||
| -- ENCODING TARGETS -- | ||
| ---------------------- | ||
|  | ||
|  | ||
| ||| A possibly infinite stream of bits | ||
| public export | ||
| BitStream : Type | ||
| BitStream = Colist Bool | ||
|  | ||
|  | ||
| ||| A possibly infinite stream of bytes | ||
| public export | ||
| ByteStream : Type | ||
| ByteStream = Colist Bits8 | ||
|  | ||
|  | ||
| ||| A finite bit buffer | ||
| public export | ||
| BitBuffer : Type | ||
| BitBuffer = List Bool | ||
|  | ||
|  | ||
| ||| A finite byte buffer | ||
| public export | ||
| ByteBuffer : Type | ||
| ByteBuffer = List Bits8 | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| ||| A closed universe of format descriptions as an inductive type, where the | ||
| ||| in-memory representation is tracked as an index on the type. | ||
|  | ||
| module Fathom.Closed.IndexedInductive | ||
|  | ||
|  | ||
| import Data.Colist | ||
| import Data.Vect | ||
|  | ||
| import Fathom.Base | ||
|  | ||
|  | ||
| ------------------------- | ||
| -- FORMAT DESCRIPTIONS -- | ||
| ------------------------- | ||
|  | ||
|  | ||
| ||| Universe of format descriptions indexed by their machine representations | ||
| public export | ||
| data FormatOf : (0 Rep : Type) -> Type where | ||
| End : FormatOf Unit | ||
| Fail : FormatOf Void | ||
| Pure : {0 A : Type} -> (x : A) -> FormatOf (Sing x) | ||
| Skip : {0 A : Type} -> (f : FormatOf A) -> (def : A) -> FormatOf Unit | ||
| Repeat : {0 A : Type} -> (len : Nat) -> FormatOf A -> FormatOf (Vect len A) | ||
| Bind : {0 A : Type} -> {0 B : A -> Type} -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x) | ||
|  | ||
|  | ||
|  | ||
| --------------------------- | ||
| -- ENCODER/DECODER PAIRS -- | ||
| --------------------------- | ||
|  | ||
| export | ||
| decode : {0 Rep : Type} -> (f : FormatOf Rep) -> Decode Rep (Colist a) | ||
| decode End [] = Just ((), []) | ||
| decode End (_::_) = Nothing | ||
| decode Fail _ = Nothing | ||
| decode (Pure x) buffer = | ||
| Just (MkSing x, buffer) | ||
| decode (Skip f _) buffer = do | ||
| (x, buffer') <- decode f buffer | ||
| Just ((), buffer') | ||
| decode (Repeat 0 f) buffer = | ||
| Just ([], buffer) | ||
| decode (Repeat (S len) f) buffer = do | ||
| (x, buffer') <- decode f buffer | ||
| (xs, buffer'') <- decode (Repeat len f) buffer' | ||
| Just (x :: xs, buffer'') | ||
| decode (Bind f1 f2) buffer = do | ||
| (x, buffer') <- decode f1 buffer | ||
| (y, buffer'') <- decode (f2 x) buffer' | ||
| Just ((x ** y), buffer'') | ||
|  | ||
|  | ||
| export | ||
| encode : {0 Rep : Type} -> (f : FormatOf Rep) -> Encode Rep (Colist a) | ||
| encode End () _ = Just [] | ||
| encode (Pure x) (MkSing _) buffer = Just buffer | ||
| encode (Skip f def) () buffer = do | ||
| encode f def buffer | ||
| encode (Repeat Z f) [] buffer = Just buffer | ||
| encode (Repeat (S len) f) (x :: xs) buffer = do | ||
| buffer' <- encode (Repeat len f) xs buffer | ||
| encode f x buffer' | ||
| encode (Bind f1 f2) (x ** y) buffer = do | ||
| buffer' <- encode (f2 x) y buffer | ||
| encode f1 x buffer' | ||
|  | ||
|  | ||
| ----------------- | ||
| -- EXPERIMENTS -- | ||
| ----------------- | ||
|  | ||
|  | ||
| either : (cond : Bool) -> FormatOf a -> FormatOf b -> FormatOf (if cond then a else b) | ||
| either True f1 _ = f1 | ||
| either False _ f2 = f2 | ||
|  | ||
| orPure : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def) | ||
| orPure True f _ = f | ||
| orPure False _ def = Pure def | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.